Spaces:
Running
Running
from dotenv import load_dotenv | |
import streamlit as st | |
import os | |
import google.generativeai as genai | |
import datetime | |
from streamlit import session_state as state | |
# Cargar las variables de entorno | |
load_dotenv() | |
# Configurar la API de Google | |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) | |
# Funci贸n para inicializar el modelo | |
def initialize_model(temperature=1.0): | |
return genai.GenerativeModel( | |
model_name="gemini-2.0-flash", | |
generation_config={"temperature": temperature} | |
) | |
# Funci贸n para generar contenido | |
def generate_content(prompt, temperature=1.0): | |
model = initialize_model(temperature) | |
response = model.generate_content(prompt) | |
return response.text | |
# Funci贸n auxiliar para mostrar el contenido generado y los botones de descarga | |
def display_generated_content(col, generated_content, content_type): | |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") | |
# Determinar el tipo de contenido para personalizar los botones y t铆tulos | |
if content_type == "sales_page_section": | |
download_label = "DESCARGAR SECCI脫N DE CARTA DE VENTAS 鈻垛柖" | |
file_name = f"seccion_carta_ventas_{timestamp}.txt" | |
subheader_text = "Tu secci贸n de carta de ventas:" | |
# Mostrar bot贸n de descarga superior | |
col.download_button( | |
label=download_label, | |
data=generated_content, | |
file_name=file_name, | |
mime="text/plain", | |
key=f"download_top_{content_type}" | |
) | |
# Mostrar el contenido generado | |
col.subheader(subheader_text) | |
col.markdown(generated_content) | |
# Mostrar bot贸n de descarga inferior | |
col.download_button( | |
label=download_label, | |
data=generated_content, | |
file_name=file_name, | |
mime="text/plain", | |
key=f"download_bottom_{content_type}" | |
) | |
# Add this function near the top of your file | |
def load_prompt_from_file(file_path): | |
try: | |
with open(file_path, "r", encoding="utf-8") as file: | |
return file.read() | |
except Exception as e: | |
st.error(f"Error al cargar el prompt desde {file_path}: {str(e)}") | |
return "" | |
# Funci贸n para generar la secci贸n "Above the Fold" | |
def generate_above_the_fold(audience, product, temperature, offer=None, **kwargs): | |
# Cargar el prompt desde el archivo | |
system_prompt = load_prompt_from_file("prompts/above_the_fold.txt") | |
# Instrucciones espec铆ficas para la tarea | |
above_fold_instruction = ( | |
f"{system_prompt}\n\n" | |
f"Your task is to create the ABOVE THE FOLD section of a sales page IN SPANISH for {audience} " | |
f"about {product}. " | |
f"The section must be compelling, attention-grabbing, and drive conversions." | |
f"\n\n" | |
) | |
# A帽adir informaci贸n sobre la oferta si se proporciona | |
if offer: | |
above_fold_instruction += f"The specific offer/product is: {offer}\n\n" | |
# Enviar el mensaje al modelo | |
return generate_content(above_fold_instruction, temperature) | |
# Funci贸n para generar la secci贸n "Problem" | |
def generate_problem_section(audience, product, temperature, offer=None, **kwargs): | |
# Cargar el prompt desde el archivo | |
system_prompt = load_prompt_from_file("prompts/problem.txt") | |
# Instrucciones espec铆ficas para la tarea | |
problem_instruction = ( | |
f"{system_prompt}\n\n" | |
f"Your task is to create the PROBLEM section of a sales page IN SPANISH for {audience} " | |
f"about {product}. " | |
f"The section must clearly identify and agitate the pain points that your product/service solves." | |
f"\n\n" | |
) | |
# A帽adir informaci贸n sobre la oferta si se proporciona | |
if offer: | |
problem_instruction += f"The specific offer/product is: {offer}\n\n" | |
# Enviar el mensaje al modelo | |
return generate_content(problem_instruction, temperature) | |
# Funci贸n para generar la secci贸n "Solution & Benefits" | |
def generate_solution_section(audience, product, temperature, offer=None, **kwargs): | |
# Cargar el prompt desde el archivo | |
system_prompt = load_prompt_from_file("prompts/solution_benefits.txt") | |
# Instrucciones espec铆ficas para la tarea | |
solution_instruction = ( | |
f"{system_prompt}\n\n" | |
f"Your task is to create the SOLUTION & BENEFITS section of a sales page IN SPANISH for {audience} " | |
f"about {product}. " | |
f"The section must present your product/service as the ideal solution to the problems described earlier." | |
f"\n\n" | |
) | |
# A帽adir informaci贸n sobre la oferta si se proporciona | |
if offer: | |
solution_instruction += f"The specific offer/product is: {offer}\n\n" | |
# Enviar el mensaje al modelo | |
return generate_content(solution_instruction, temperature) | |
# Funci贸n para generar la secci贸n "Authority" | |
def generate_authority_section(audience, product, temperature, offer=None, **kwargs): | |
# Cargar el prompt desde el archivo | |
system_prompt = load_prompt_from_file("prompts/authority.txt") | |
# Instrucciones espec铆ficas para la tarea | |
authority_instruction = ( | |
f"{system_prompt}\n\n" | |
f"Your task is to create the AUTHORITY section of a sales page IN SPANISH for {audience} " | |
f"about {product}. " | |
f"The section must establish credibility and trust with the audience." | |
f"\n\n" | |
) | |
# A帽adir informaci贸n sobre la oferta si se proporciona | |
if offer: | |
authority_instruction += f"The specific offer/product is: {offer}\n\n" | |
# Enviar el mensaje al modelo | |
return generate_content(authority_instruction, temperature) | |
# Funci贸n para generar la secci贸n "Offer & Bonus" | |
def generate_offer_section(audience, product, temperature, offer=None, **kwargs): | |
# Cargar el prompt desde el archivo | |
system_prompt = load_prompt_from_file("prompts/offer_bonus.txt") | |
# Instrucciones espec铆ficas para la tarea | |
offer_instruction = ( | |
f"{system_prompt}\n\n" | |
f"Your task is to create the OFFER & BONUS section of a sales page IN SPANISH for {audience} " | |
f"about {product}. " | |
f"The section must present the offer in an irresistible way and highlight any bonuses." | |
f"\n\n" | |
) | |
# A帽adir informaci贸n sobre la oferta si se proporciona | |
if offer: | |
offer_instruction += f"The specific offer/product is: {offer}\n\n" | |
# Enviar el mensaje al modelo | |
return generate_content(offer_instruction, temperature) | |
# Funci贸n para generar la secci贸n "Social Proof" | |
def generate_social_proof_section(audience, product, temperature, offer=None, **kwargs): | |
# Cargar el prompt desde el archivo | |
system_prompt = load_prompt_from_file("prompts/social_proof.txt") | |
# Instrucciones espec铆ficas para la tarea | |
social_proof_instruction = ( | |
f"{system_prompt}\n\n" | |
f"Your task is to create the SOCIAL PROOF section of a sales page IN SPANISH for {audience} " | |
f"about {product}. " | |
f"The section must build trust through testimonials and social proof." | |
f"\n\n" | |
) | |
# A帽adir informaci贸n sobre la oferta si se proporciona | |
if offer: | |
social_proof_instruction += f"The specific offer/product is: {offer}\n\n" | |
# Enviar el mensaje al modelo | |
return generate_content(social_proof_instruction, temperature) | |
# Funci贸n para generar la secci贸n "Guarantees" | |
def generate_guarantees_section(audience, product, temperature, offer=None, **kwargs): | |
# Cargar el prompt desde el archivo | |
system_prompt = load_prompt_from_file("prompts/guarantees.txt") | |
# Instrucciones espec铆ficas para la tarea | |
guarantees_instruction = ( | |
f"{system_prompt}\n\n" | |
f"Your task is to create the GUARANTEES section of a sales page IN SPANISH for {audience} " | |
f"about {product}. " | |
f"The section must remove risk for the customer through strong guarantees." | |
f"\n\n" | |
) | |
# A帽adir informaci贸n sobre la oferta si se proporciona | |
if offer: | |
guarantees_instruction += f"The specific offer/product is: {offer}\n\n" | |
# Enviar el mensaje al modelo | |
return generate_content(guarantees_instruction, temperature) | |
# Funci贸n para generar la secci贸n "Call to Action" | |
def generate_cta_section(audience, product, temperature, offer=None, **kwargs): | |
# Cargar el prompt desde el archivo | |
system_prompt = load_prompt_from_file("prompts/cta.txt") | |
# Instrucciones espec铆ficas para la tarea | |
cta_instruction = ( | |
f"{system_prompt}\n\n" | |
f"Your task is to create the CALL TO ACTION section of a sales page IN SPANISH for {audience} " | |
f"about {product}. " | |
f"The section must drive conversions with a clear and compelling call to action." | |
f"\n\n" | |
) | |
# A帽adir informaci贸n sobre la oferta si se proporciona | |
if offer: | |
cta_instruction += f"The specific offer/product is: {offer}\n\n" | |
# Enviar el mensaje al modelo | |
return generate_content(cta_instruction, temperature) | |
# Funci贸n para generar la secci贸n "P.S." | |
def generate_ps_section(audience, product, temperature, offer=None, **kwargs): | |
# Cargar el prompt desde el archivo | |
system_prompt = load_prompt_from_file("prompts/ps.txt") | |
# Instrucciones espec铆ficas para la tarea | |
ps_instruction = ( | |
f"{system_prompt}\n\n" | |
f"Your task is to create the P.S. (POSTSCRIPT) section of a sales page IN SPANISH for {audience} " | |
f"about {product}. " | |
f"The section must add a final persuasive element that drives action." | |
f"\n\n" | |
) | |
# A帽adir informaci贸n sobre la oferta si se proporciona | |
if offer: | |
ps_instruction += f"The specific offer/product is: {offer}\n\n" | |
# Enviar el mensaje al modelo | |
return generate_content(ps_instruction, temperature) | |
# Funci贸n para generar la secci贸n "Final Call to Action" | |
def generate_final_cta_section(audience, product, temperature, offer=None, **kwargs): | |
# Cargar el prompt desde el archivo | |
system_prompt = load_prompt_from_file("prompts/final_cta.txt") | |
# Instrucciones espec铆ficas para la tarea | |
final_cta_instruction = ( | |
f"{system_prompt}\n\n" | |
f"Your task is to create the FINAL CALL TO ACTION section of a sales page IN SPANISH for {audience} " | |
f"about {product}. " | |
f"The section must drive conversions with a final, powerful call to action." | |
f"\n\n" | |
) | |
# A帽adir informaci贸n sobre la oferta si se proporciona | |
if offer: | |
final_cta_instruction += f"The specific offer/product is: {offer}\n\n" | |
# Enviar el mensaje al modelo | |
return generate_content(final_cta_instruction, temperature) | |
# Funci贸n para validar entradas | |
def validate_inputs(audience, product): | |
has_audience = audience.strip() != "" | |
has_product = product.strip() != "" | |
return has_audience and has_product | |
# Funci贸n para cargar CSS | |
def load_css(): | |
css_path = "styles/styles.css" | |
if os.path.exists(css_path): | |
try: | |
with open(css_path, "r") as f: | |
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True) | |
except Exception as e: | |
st.warning(f"Error al cargar el archivo CSS: {str(e)}") | |
else: | |
st.warning(f"No se encontr贸 el archivo CSS en {css_path}") | |
# Configuraci贸n de la p谩gina | |
st.set_page_config( | |
page_title="CopyXpert - Generador de P谩ginas de Ventas", | |
layout="wide", | |
initial_sidebar_state="expanded", | |
menu_items=None | |
) | |
load_css() | |
# Leer el contenido del archivo manual.md | |
try: | |
with open("manual.md", "r", encoding="utf-8") as file: | |
manual_content = file.read() | |
# Mostrar el contenido del manual en el sidebar | |
st.sidebar.markdown(manual_content) | |
except: | |
st.sidebar.warning("No se pudo cargar el manual. Aseg煤rate de que el archivo manual.md existe.") | |
# Agregar t铆tulo y subt铆tulo usando HTML | |
st.markdown("<h1 style='text-align: center;'>CopyXpert</h1>", unsafe_allow_html=True) | |
st.markdown("<h3 style='text-align: center;'>Generador de P谩ginas de Ventas Persuasivas</h3>", unsafe_allow_html=True) | |
# Interfaz principal para el generador de p谩ginas de ventas | |
st.subheader("Generador de Secciones de P谩ginas de Ventas") | |
# Crear columnas para la interfaz | |
col1, col2 = st.columns([1, 2]) | |
# Columna de entrada | |
with col1: | |
# Inputs b谩sicos | |
sales_page_audience = st.text_input("驴Qui茅n es tu p煤blico objetivo?", placeholder="Ejemplo: Emprendedores digitales", key="sales_page_audience") | |
sales_page_product = st.text_input("驴Sobre qu茅 producto/servicio es tu p谩gina?", placeholder="Ejemplo: Curso de marketing", key="sales_page_product") | |
sales_page_offer = st.text_input("驴Cu谩l es tu oferta espec铆fica?", placeholder="Ejemplo: Curso de marketing por $197", key="sales_page_offer") | |
# Selector de secci贸n | |
sales_page_section = st.selectbox( | |
"Selecciona la secci贸n a generar", | |
options=[ | |
"Above the Fold (Encabezado)", | |
"Problem (Problema)", | |
"Solution & Benefits (Soluci贸n y Beneficios)", | |
"Authority (Autoridad)", | |
"Offer & Bonus (Oferta y Bonus)", | |
"Social Proof (Prueba Social)", | |
"Guarantees (Garant铆as)", | |
"Call to Action (Llamada a la Acci贸n)", | |
"P.S. (Post-Data)", | |
"Final Call to Action (Llamada Final)" | |
], | |
key="sales_page_section" | |
) | |
# Bot贸n de generaci贸n | |
submit_sales_page = st.button("GENERAR SECCI脫N DE P脕GINA DE VENTAS 鈻垛柖", key="generate_sales_page") | |
# Opciones avanzadas en el acorde贸n | |
with st.expander("Personaliza tu secci贸n"): | |
# Slider de creatividad | |
sales_page_temperature = st.slider("Creatividad", min_value=0.0, max_value=2.0, value=1.0, step=0.1, key="sales_page_temp") | |
# Generar y mostrar la secci贸n de la p谩gina de ventas | |
if submit_sales_page: | |
# Validar entradas | |
if validate_inputs(sales_page_audience, sales_page_product): | |
try: | |
# Crear un contenedor para el spinner en la columna 2 | |
with col2: | |
with st.spinner("Generando secci贸n de p谩gina de ventas...", show_time=True): | |
# Mapear la selecci贸n a la funci贸n correspondiente | |
section_functions = { | |
"Above the Fold (Encabezado)": generate_above_the_fold, | |
"Problem (Problema)": generate_problem_section, | |
"Solution & Benefits (Soluci贸n y Beneficios)": generate_solution_section, | |
"Authority (Autoridad)": generate_authority_section, | |
"Offer & Bonus (Oferta y Bonus)": generate_offer_section, | |
"Social Proof (Prueba Social)": generate_social_proof_section, | |
"Guarantees (Garant铆as)": generate_guarantees_section, | |
"Call to Action (Llamada a la Acci贸n)": generate_cta_section, | |
"P.S. (Post-Data)": generate_ps_section, | |
"Final Call to Action (Llamada Final)": generate_final_cta_section | |
} | |
# Obtener la funci贸n correspondiente | |
generator_func = section_functions[sales_page_section] | |
# Generar el contenido | |
generated_content = generator_func( | |
audience=sales_page_audience, | |
product=sales_page_product, | |
temperature=sales_page_temperature, | |
offer=sales_page_offer if sales_page_offer.strip() else None | |
) | |
# Mostrar el contenido generado fuera del bloque del spinner | |
display_generated_content(col2, generated_content, "sales_page_section") | |
except Exception as e: | |
st.error(f"Error al generar la secci贸n: {str(e)}") | |
else: | |
st.warning("Por favor, completa los campos de p煤blico objetivo y producto/servicio.") |