Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
import anthropic
|
| 4 |
import os # Importa el módulo os para manejar variables de entorno
|
|
@@ -6,67 +5,72 @@ import os # Importa el módulo os para manejar variables de entorno
|
|
| 6 |
# Obtén la clave de API desde la variable de entorno
|
| 7 |
api_key = os.getenv("ANTHROPIC_API_KEY")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
if not api_key:
|
| 11 |
-
raise ValueError("Falta la clave de API de Anthropoid. Asegúrate de configurarla en los secretos del repositorio.")
|
| 12 |
-
|
| 13 |
-
# Configura el cliente de la API de Claude (Anthropic)
|
| 14 |
client = anthropic.Anthropic(api_key=api_key)
|
| 15 |
|
|
|
|
| 16 |
def generate_headlines(number_of_headlines, target_audience, product):
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
model="claude-3-5-sonnet-20240620",
|
| 20 |
max_tokens=1000,
|
| 21 |
temperature=0,
|
| 22 |
-
system=
|
| 23 |
messages=[
|
| 24 |
{
|
| 25 |
"role": "user",
|
| 26 |
"content": [
|
| 27 |
{
|
| 28 |
"type": "text",
|
| 29 |
-
"text":
|
| 30 |
}
|
| 31 |
]
|
| 32 |
}
|
| 33 |
]
|
| 34 |
)
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return generate_headlines(number_of_headlines, target_audience, product)
|
| 40 |
-
|
| 41 |
-
# Define los colores de la interfaz según el logo de Anthropic (ejemplo)
|
| 42 |
-
logo_colors = {
|
| 43 |
-
"background": "#f8f8f8",
|
| 44 |
-
"primary": "#2c7be5",
|
| 45 |
-
"text_color": "#212529"
|
| 46 |
-
}
|
| 47 |
|
| 48 |
-
|
|
|
|
| 49 |
gr.Markdown(
|
| 50 |
-
|
| 51 |
-
<
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
"""
|
| 54 |
)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
submit_btn = gr.Button("Generar Titulares", elem_id="submit-btn")
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
submit_btn.click(
|
| 66 |
-
fn=gradio_generate_headlines,
|
| 67 |
inputs=[number_of_headlines, target_audience, product],
|
| 68 |
outputs=output
|
| 69 |
)
|
| 70 |
-
|
| 71 |
-
# Lanza la interfaz
|
| 72 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import anthropic
|
| 3 |
import os # Importa el módulo os para manejar variables de entorno
|
|
|
|
| 5 |
# Obtén la clave de API desde la variable de entorno
|
| 6 |
api_key = os.getenv("ANTHROPIC_API_KEY")
|
| 7 |
|
| 8 |
+
# Configura el cliente de Anthropic
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
client = anthropic.Anthropic(api_key=api_key)
|
| 10 |
|
| 11 |
+
# Define la función que genera los titulares
|
| 12 |
def generate_headlines(number_of_headlines, target_audience, product):
|
| 13 |
+
# Configura el mensaje para la API
|
| 14 |
+
system_message = (
|
| 15 |
+
"You are a world-class copywriter with expertise in creating compelling hooks, headlines, and subject lines. "
|
| 16 |
+
"Your talent lies in understanding the emotions, desires, and challenges of a specific audience, allowing you to craft "
|
| 17 |
+
"personalized marketing strategies that resonate and motivate action. Use proven structures to attract your target audience, "
|
| 18 |
+
"generating interest and achieving a powerful connection that drives desired results in advertising and content campaigns. "
|
| 19 |
+
"Respond in Markdown format."
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
user_message = (
|
| 23 |
+
f"Generate {number_of_headlines} attention-grabbing headlines designed for {target_audience} to generate interest in {product}. "
|
| 24 |
+
"Each headline should be crafted to encourage a specific action, such as making a purchase, signing up, or downloading. "
|
| 25 |
+
"Use a variety of formats (questions, bold statements, intriguing facts) to test different approaches."
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Llama a la API de Claude
|
| 29 |
+
response = client.messages.create(
|
| 30 |
model="claude-3-5-sonnet-20240620",
|
| 31 |
max_tokens=1000,
|
| 32 |
temperature=0,
|
| 33 |
+
system=system_message,
|
| 34 |
messages=[
|
| 35 |
{
|
| 36 |
"role": "user",
|
| 37 |
"content": [
|
| 38 |
{
|
| 39 |
"type": "text",
|
| 40 |
+
"text": user_message
|
| 41 |
}
|
| 42 |
]
|
| 43 |
}
|
| 44 |
]
|
| 45 |
)
|
| 46 |
+
|
| 47 |
+
# Extrae y retorna el contenido de la respuesta
|
| 48 |
+
headlines = response.content
|
| 49 |
+
return headlines
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Configura la interfaz de usuario de Gradio
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
gr.Markdown(
|
| 54 |
+
"""
|
| 55 |
+
<div style="text-align: center; font-size: 32px; font-weight: bold; margin-bottom: 20px;">
|
| 56 |
+
Headline Generator
|
| 57 |
+
</div>
|
| 58 |
+
<div style="text-align: center;">
|
| 59 |
+
<p>Generate compelling headlines for your marketing needs.</p>
|
| 60 |
+
</div>
|
| 61 |
"""
|
| 62 |
)
|
| 63 |
|
| 64 |
+
number_of_headlines = gr.Slider(minimum=1, maximum=10, value=5, label="Number of Headlines")
|
| 65 |
+
target_audience = gr.Textbox(label="Target Audience", placeholder="e.g., tech enthusiasts, busy professionals")
|
| 66 |
+
product = gr.Textbox(label="Product", placeholder="e.g., new smartphone, productivity tool")
|
| 67 |
+
generate_button = gr.Button("Generate Headlines")
|
| 68 |
+
output = gr.Markdown()
|
|
|
|
| 69 |
|
| 70 |
+
generate_button.click(
|
| 71 |
+
generate_headlines,
|
|
|
|
|
|
|
| 72 |
inputs=[number_of_headlines, target_audience, product],
|
| 73 |
outputs=output
|
| 74 |
)
|
| 75 |
+
|
|
|
|
| 76 |
demo.launch()
|