Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import anthropic
|
| 3 |
import os # Importa el módulo os para manejar variables de entorno
|
|
@@ -5,72 +6,67 @@ 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 14 |
-
|
| 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=
|
| 34 |
messages=[
|
| 35 |
{
|
| 36 |
"role": "user",
|
| 37 |
"content": [
|
| 38 |
{
|
| 39 |
"type": "text",
|
| 40 |
-
"text":
|
| 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
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
gr.Markdown(
|
| 54 |
-
"""
|
| 55 |
-
<
|
| 56 |
-
|
| 57 |
-
</div>
|
| 58 |
-
<div style="text-align: center;">
|
| 59 |
-
<p>Generate compelling headlines for your marketing needs.</p>
|
| 60 |
-
</div>
|
| 61 |
"""
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
inputs=[number_of_headlines, target_audience, product],
|
| 73 |
outputs=output
|
| 74 |
)
|
| 75 |
-
|
|
|
|
| 76 |
demo.launch()
|
|
|
|
| 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 |
# Obtén la clave de API desde la variable de entorno
|
| 7 |
api_key = os.getenv("ANTHROPIC_API_KEY")
|
| 8 |
|
| 9 |
+
# Verifica si la clave de API está configurada
|
| 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 |
+
# Llama a la API de Claude para generar titulares
|
| 18 |
+
message = client.messages.create(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
model="claude-3-5-sonnet-20240620",
|
| 20 |
max_tokens=1000,
|
| 21 |
temperature=0,
|
| 22 |
+
system="You are a world-class copywriter, with experience in creating hooks, headlines, and subject lines that immediately capture attention. Your skill lies in deeply understanding the emotions, desires, and challenges of a specific audience, allowing you to design personalized marketing strategies that resonate and motivate action. You know how to use proven structures to attract your target audience, generating interest and achieving a powerful connection that drives desired results in advertising and content campaigns.\n\nAnswer in Spanish.",
|
| 23 |
messages=[
|
| 24 |
{
|
| 25 |
"role": "user",
|
| 26 |
"content": [
|
| 27 |
{
|
| 28 |
"type": "text",
|
| 29 |
+
"text": f"Generate {number_of_headlines} attention-grabbing headlines designed for {target_audience} to generate interest in {product}. Each headline should be crafted to encourage a specific action, such as making a purchase, signing up, or downloading. Use a variety of formats (questions, bold statements, intriguing facts) to test different approaches."
|
| 30 |
}
|
| 31 |
]
|
| 32 |
}
|
| 33 |
]
|
| 34 |
)
|
| 35 |
+
return message.content
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# Configura la interfaz de usuario con Gradio
|
| 38 |
+
def gradio_generate_headlines(number_of_headlines, target_audience, product):
|
| 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 |
+
with gr.Blocks(css=".gradio-container { background-color: " + logo_colors["background"] + "; }") as demo:
|
| 49 |
gr.Markdown(
|
| 50 |
+
f"""
|
| 51 |
+
<h1 style="color: {logo_colors['primary']}; text-align: center;">Generador de Titulares</h1>
|
| 52 |
+
<p style="color: {logo_colors['text_color']}; text-align: center;">Usa el poder de Claude AI para crear titulares atractivos</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
"""
|
| 54 |
)
|
| 55 |
|
| 56 |
+
with gr.Row():
|
| 57 |
+
with gr.Column():
|
| 58 |
+
number_of_headlines = gr.Number(label="Número de Titulares", placeholder="Ejemplo: 5")
|
| 59 |
+
target_audience = gr.Textbox(label="Público Objetivo", placeholder="Ejemplo: Estudiantes Universitarios")
|
| 60 |
+
product = gr.Textbox(label="Producto", placeholder="Ejemplo: Curso de Inglés")
|
| 61 |
+
submit_btn = gr.Button("Generar Titulares", elem_id="submit-btn")
|
| 62 |
|
| 63 |
+
output = gr.Textbox(label="Titulares Generados", lines=40)
|
| 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()
|