JeCabrera commited on
Commit
51322b9
·
verified ·
1 Parent(s): 861677e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +256 -329
app.py CHANGED
@@ -1,335 +1,262 @@
1
- import random
2
-
3
- # Add this function at the beginning of the file
4
- def create_offer_instruction(avatar_description, product_name, selected_formula_name):
5
- """
6
- Creates instructions for generating an offer based on the selected formula.
7
-
8
- Args:
9
- avatar_description: Description of the target audience
10
- product_name: Name of the product or service
11
- selected_formula_name: Name of the formula to use ("Fórmula Sueño-Obstáculo" or "Oferta Dorada")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- Returns:
14
- str: Complete instruction for generating the offer
15
- """
16
- # Get the selected formula
17
- selected_formula = offer_formulas[selected_formula_name]
18
-
19
- # Get random examples (1-3 examples)
20
- num_examples = min(3, len(selected_formula["examples"]))
21
- random_examples = random.sample(selected_formula["examples"], num_examples)
22
-
23
- # Format examples
24
- examples_text = "\n\n".join([f"Example {i+1}:\n{example}" for i, example in enumerate(random_examples)])
25
-
26
- # Add specific instructions for each formula
27
- additional_instructions = ""
28
- if selected_formula_name == "Fórmula Sueño-Obstáculo":
29
- additional_instructions = """
30
- SPECIFIC INSTRUCTIONS FOR THIS FORMULA:
31
- 1. PRODUCT/SERVICE NAME HANDLING:
32
- - If product_name is provided and not empty, use it EXACTLY as written
33
- - If product_name is empty, generic (like "Producto/Servicio"), or contains placeholders, CREATE a compelling name that:
34
- * Reflects the target audience's desires and challenges
35
- * Communicates the main benefit or transformation
36
- * Sounds professional and memorable
37
- * Is specific to the niche or industry mentioned in avatar_description
38
- - If product_name contains a full phrase like "Un curso llamado Inglés sin problemas", extract only the real name ("Inglés sin problemas")
39
-
40
- 2. Analyze ALL available information:
41
- - Product/service name (product_name variable) or create one if needed
42
- - Target audience description (avatar_description)
43
- - Content from uploaded files (if any)
44
-
45
- 3. Determine the most appropriate type (curso, webinar, entrenamiento, etc.) based on:
46
- - Any type mentioned in product_name
47
- - The nature of the solution described in avatar_description
48
- - The most suitable format for the target audience's needs
49
-
50
- 4. Create a comprehensive offer by combining:
51
- - The appropriate type (determined in step 3)
52
- - The exact product name (if provided) or your created name (if needed)
53
- - A compelling dream based on avatar_description
54
- - A relevant obstacle based on avatar_description
55
-
56
- 5. The dream should be ambitious but believable, incorporating:
57
- - Target audience desires from avatar_description
58
- - Explicit goals mentioned in uploaded content (if any)
59
-
60
- 6. The obstacle should reflect:
61
- - Real problems mentioned in avatar_description
62
- - Challenges that would normally prevent achieving the dream
63
-
64
- 7. IMPORTANT: Vary the way you start the phrase. Instead of always using "Se trata de un...", use different openings such as:
65
- - "Presentamos un..."
66
- - "Te ofrecemos un..."
67
- - "Descubre nuestro..."
68
- - "Conoce el..."
69
- - "Hemos creado un..."
70
- - "Imagina tener acceso a un..."
71
- - "Por fin existe un..."
72
- - "Ahora puedes acceder a un..."
73
- - "Tenemos para ti un..."
74
- - "Disfruta de un..."
75
-
76
- 8. CRITICAL: Output ONLY the offer itself with NO introductory text, explanations, or additional commentary.
77
- - DO NOT include phrases like "Aquí tienes una oferta convincente" or "Esta es tu oferta"
78
- - DO NOT include any text before or after the offer
79
- - Start directly with one of the opening phrases from point 7
80
- - The entire response should be ONLY the offer itself
81
- """
82
-
83
- elif selected_formula_name == "Oferta Dorada":
84
- additional_instructions = """
85
- SPECIFIC INSTRUCTIONS FOR THIS FORMULA:
86
- 1. ATTENTION HOOK HANDLING:
87
- - Analyze the avatar_description DEEPLY to understand their specific pain points, frustrations, and desires
88
- - Select a powerful attention hook that DIRECTLY connects with the avatar's current reality
89
- - DO NOT use questions as hooks - use statements, statistics, or shocking revelations instead
90
- - CUSTOMIZE the hook specifically for this avatar - don't use generic examples
91
- - The hook MUST address the SAME problem that your promise will solve
92
-
93
- Choose randomly from these statement hooks and CUSTOMIZE for your avatar:
94
- - "El 83% de los [avatar's profession/role] pierden [specific resource] en [specific activity] que nadie aprovecha."
95
- - "9 de cada 10 [avatar's field] fracasan en sus primeros 6 meses por este error."
96
- - "Lo que nadie te dice sobre [avatar's challenge] es que la mayoría fracasa en los primeros 3 meses."
97
- - "El secreto que [competitors/industry] no quieren que sepas sobre [avatar's goal]."
98
- - "Tu [avatar's current strategy/approach] está ahuyentando a tus [avatar's desired outcome]."
99
- - "Mientras algunos [positive outcome], tú sigues [negative current situation]."
100
- - "La mayoría de [current solutions] son una pérdida total de [resources]."
101
- - "Hace 6 meses estaba exactamente donde tú estás: [avatar's current struggle]."
102
-
103
- 2. MAINTAIN THEMATIC CONSISTENCY:
104
- - The attention hook, quantifiable promise, and benefit statement MUST all address the SAME problem
105
- - Create a LOGICAL PROGRESSION from problem (hook) to solution (promise) to implementation (benefit)
106
-
107
- 3. Create a compelling QUANTIFIABLE PROMISE that:
108
- - Is written COMPLETELY IN CAPITAL LETTERS
109
- - Includes concrete numbers (money, time, results)
110
- - Uses powerful action verbs (EARN, MULTIPLY, ACHIEVE, MASTER)
111
- - Specifies the exact result they will obtain
112
- - Optionally includes time or effort required
113
- - NEVER uses exclamation marks (!)
114
- - DIRECTLY addresses the same problem mentioned in the hook
115
-
116
- 4. Craft a benefit statement that:
117
- - Clearly explains the result they will obtain
118
- - Includes an authority element (proven method, studies, experience)
119
- - Establishes a realistic timeframe or effort needed
120
- - CONTINUES the same theme established in the hook and promise
121
-
122
- 5. CRITICAL: Output ONLY the offer itself with NO introductory text, explanations, or additional commentary.
123
- - Start directly with the attention hook
124
- - The entire response should be ONLY the offer itself following the formula structure
125
- """
126
-
127
- # Create the instruction
128
- instruction = f"""
129
- You are a world-class expert copywriter, experienced in creating compelling offers that connect emotionally with the target audience.
130
-
131
- OBJECTIVE:
132
- - Generate a convincing offer in Spanish using the {selected_formula_name}
133
- - Connect emotionally with the audience: {avatar_description}
134
- - Address real desires, problems, and motivations
135
- - Maintain natural and conversational language
136
-
137
- FORMULA TO USE:
138
- {selected_formula["description"]}
139
-
140
- {additional_instructions}
141
-
142
- EXAMPLES (Use these as inspiration but create something unique):
143
- {examples_text}
144
-
145
- PRODUCT/SERVICE:
146
- {product_name}
147
-
148
- TARGET AUDIENCE:
149
- {avatar_description}
150
-
151
- Create a compelling offer following the formula structure exactly.
152
- """
153
-
154
- return instruction
155
-
156
- # The rest of your offer_formulas dictionary remains unchanged
157
- offer_formulas = {
158
- "Oferta Dorada": {
159
- "description": """
160
- Formula: [Attention Hook + QUANTIFIABLE PROMISE IN ALL CAPS + Benefit + Authority + Time or Effort]
161
-
162
- This formula is designed to speak directly to the avatar, capturing their attention immediately, reflecting their current situation, and showing the transformation they desire.
163
-
164
- ### **How to apply it?**
165
-
166
- #### 1 **Attention Hook**
167
- The first step is to capture the avatar's attention with a powerful hook that can be a shocking revelation, an unexpected question, or a dramatic fact. IMPORTANT: CUSTOMIZE THE HOOK BASED ON THE AVATAR AND THEIR SPECIFIC PROBLEMS. Don't use generic examples, but adapt them to the client's situation.
168
-
169
- Analyze first:
170
- - What is the avatar's biggest pain or frustration?
171
- - What are they trying to achieve without success?
172
- - What limiting beliefs do they have?
173
-
174
- Then, randomly choose one of the following formats and CUSTOMIZE it for the specific avatar:
175
-
176
- **Correct examples:**
177
- "El 83% de los emprendedores pierden dinero en anuncios que nadie convierte."
178
- "9 de cada 10 negocios online fracasan en sus primeros 6 meses por este error."
179
- "Lo que nadie te dice sobre el marketing digital es que la mayoría fracasa en los primeros 3 meses."
180
- "El secreto que las agencias de marketing no quieren que sepas sobre tu tráfico web."
181
- "Ah, otro día más tirando dinero en anuncios que no convierten... ¡Qué divertido!"
182
- "Felicidades, acabas de unirte al club de 'Invertí miles en mi web y nadie la visita'."
183
- "¿Has pasado horas escribiendo emails y nadie los abre?"
184
- "Tu lista de email está tan muerta que hasta los mensajes de spam tienen más aperturas."
185
- "Tu página de ventas convierte tan poco que hasta tu mamá cerró la pestaña sin comprar."
186
- "Mientras algunos facturan $10,000 al mes, tú sigues preguntándote por qué nadie compra."
187
- "Tus competidores están cerrando ventas mientras tú sigues 'perfeccionando' tu oferta."
188
- "La mayoría de cursos de marketing digital son una pérdida total de tiempo y dinero."
189
- "Tu estrategia actual de contenido está ahuyentando a tus clientes ideales."
190
- "Hace 6 meses estaba exactamente donde tú estás: creando contenido que nadie veía."
191
- "Recuerdo cuando mi negocio estaba al borde del colapso por no tener un sistema de ventas."
192
-
193
- The important thing is that it connects directly with the avatar's current reality and frustration, USING A VARIETY OF FORMATS AND CUSTOMIZING TO THE SPECIFIC AVATAR.
194
-
195
- ---
196
-
197
- #### 2 **QUANTIFIABLE PROMISE IN ALL CAPS**
198
- This is the most important part. You must create a specific, quantifiable promise written COMPLETELY IN CAPITAL LETTERS that immediately captures attention. It must include:
199
- - Concrete numbers (money, time, results)
200
- - Powerful action verbs (EARN, MULTIPLY, ACHIEVE, MASTER)
201
- - The specific result they will obtain
202
- - Optionally, the time or effort required
203
- - IMPORTANT: DO NOT USE EXCLAMATION MARKS (!) IN THIS SECTION UNDER ANY CIRCUMSTANCES
204
-
205
- **Incorrect example:**
206
- "Improve your sales with our system." (Vague, no numbers, no impact).
207
- "¡MULTIPLY YOUR SALES IN RECORD TIME!" (Uses exclamation marks, NEVER use them).
208
-
209
- **Correct example:**
210
- "FACTURA MAS DE $1.000 USD USANDO 15 EMAILS ESCRITOS EN 15 MINUTOS CADA UNO" (Specific, quantifiable, impactful).
211
- "MULTIPLICA POR 3 TUS INTERACCIONES EN REDES SOCIALES EN SOLO 2 SEMANAS" (Clear, measurable, with defined time).
212
-
213
- ---
214
-
215
- #### 3 **Benefit + Authority + Time or Effort**
216
- In this part, we explain the result they will obtain, supported by an authority factor (proven method, studies, experience, validations) and establishing a time frame or necessary effort to achieve it.
217
-
218
- **Incorrect example:**
219
- "Grow your business with our strategy." (Doesn't say how long it will take or how reliable the strategy is).
220
-
221
- **Correct example:**
222
- "Generate responses and sales with our strategy used by more than 500 entrepreneurs, with just 15 minutes a day."
223
-
224
- This format clearly states the benefit, backs up the solution with authority, and establishes a realistic effort to achieve it.
225
-
226
- ---
227
-
228
- ### **Fixed structure:**
229
- "[Varied Attention Hook]
230
-
231
- [QUANTIFIABLE PROMISE IN ALL CAPS]
232
-
233
- [Benefit] with [Authority element] in [Time or effort]"
234
-
235
- ---
236
-
237
- ### **Examples of the applied formula:**
238
- """,
239
- "examples": [
240
- """El 83% de los emprendedores pierden dinero en anuncios que nadie convierte.
241
-
242
- CONVIERTE EL 30% DE TUS VISITANTES EN COMPRADORES Y REDUCE TU COSTO DE ADQUISICIÓN A LA MITAD EN SOLO 14 DÍAS.
243
-
244
- Convierte más visitas en ventas con una estructura de copy validada en solo 5 días.""",
245
-
246
- """Tu lista de email está tan muerta que hasta los mensajes de spam tienen más aperturas.
247
-
248
- AUMENTA TU TASA DE APERTURA AL 35% Y GENERA $2.500 EN VENTAS CON CADA CAMPAÑA DE EMAIL QUE ENVIES.
249
-
250
- Haz que tus correos se lean con una estrategia usada por expertos en solo 30 minutos por campaña.""",
251
-
252
- """Mientras algunos facturan $10,000 al mes, tú sigues preguntándote por qué nadie compra.
253
-
254
- FACTURA EL DOBLE SIN BAJAR TUS PRECIOS Y CONVIERTE EL 80% DE TUS PROPUESTAS EN CLIENTES PAGANDO.
255
-
256
- Cierra más ventas con un método probado por 300 freelancers sin necesidad de descuentos en solo 7 días.""",
257
-
258
- """Lo que nadie te dice sobre el marketing de contenidos es que el 95% nunca genera un solo cliente.
259
-
260
- MULTIPLICA POR 5 TUS COMENTARIOS Y CONSIGUE 3 CLIENTES NUEVOS CADA SEMANA CON SOLO 20 MINUTOS DE TRABAJO DIARIO.
261
-
262
- Consigue comentarios y clientes con una estrategia de contenido validada en solo 10 minutos al día.""",
263
-
264
- """Ah, otro día más publicando en redes sociales y hablándole a las paredes... Qué divertido.
265
-
266
- CONSIGUE 100 NUEVOS SEGUIDORES CUALIFICADOS POR SEMANA Y CONVIERTE EL 10% EN LEADS INTERESADOS EN TUS SERVICIOS.
267
-
268
- Crea contenido irresistible con una estrategia validada en solo 15 minutos al día."""
269
- ]
270
- },
271
- "Fórmula Sueño-Obstáculo": {
272
- "description": """
273
- Formula: [Type + Name + Dream + Obstacle]
274
-
275
- This formula connects directly with the client's desires and concerns:
276
-
277
- 1. Type: The type of solution (training, product, or service)
278
- 2. Name: The name of your solution
279
- 3. Dream: The big dream or result that the client wants to achieve
280
- 4. Obstacle: The obstacle that would normally prevent achieving that dream
281
-
282
- **Suggested solution types:**
283
- - Online course
284
- - Webinar
285
- - Training
286
- - Program
287
- - Workshop
288
- - Mentorship
289
- - Consulting
290
- - Membership
291
- - System
292
- - Method
293
- - Service
294
- - Product
295
- - Application
296
- - Community
297
- - Masterclass
298
-
299
- **Suggested opening variations:**
300
- - "Se trata de un..."
301
- - "Presentamos un..."
302
- - "Te ofrecemos un..."
303
- - "Descubre nuestro..."
304
- - "Conoce el..."
305
- - "Hemos creado un..."
306
- - "Imagina tener acceso a un..."
307
- - "Por fin existe un..."
308
- - "Ahora puedes acceder a un..."
309
- - "Tenemos para ti un..."
310
- - "Disfruta de un..."
311
-
312
- **Structure Format (Classic example):**
313
- "Se trata de un (training, product or service) llamado ("name of your solution") que te va a permitir (big dream) aún y cuando (big obstacle)"
314
- """,
315
- "examples": [
316
- "Presentamos un programa llamado \"Desbloqueo Creativo Total\" que te va a permitir generar ideas brillantes a demanda aún y cuando tu cerebro esté más seco que el desierto de Atacama.",
317
-
318
- "Te ofrecemos un curso online llamado \"Máquina de Ventas Imparable\" que te va a permitir vender hasta a tu peor enemigo aún y cuando antes no podrías vender agua en el infierno.",
319
-
320
- "Imagina tener acceso a un sistema llamado \"Productividad Sobrehumana\" que te va a permitir hacer en 2 horas lo que otros hacen en 2 días aún y cuando ahora mismo tu relación con la procrastinación sea más estable que tu último noviazgo.",
321
-
322
- "Por fin existe una mentoría llamada \"Libertad Financiera Express\" que te va a permitir generar ingresos pasivos mientras duermes aún y cuando ahora mismo tu cuenta bancaria esté tan vacía que haga eco.",
323
 
324
- "Hemos creado un método llamado \"Conquista Digital\" que te va a permitir posicionar tu marca en el top 1% de tu industria aún y cuando ahora mismo seas más invisible que un ninja en la oscuridad.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325
 
326
- "Conoce el taller llamado \"Oratoria Magnética\" que te va a permitir cautivar a cualquier audiencia aún y cuando ahora mismo hablar en público te cause más terror que una película de Stephen King.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
 
328
- "Disfruta de una comunidad llamada \"Conexiones Estratégicas VIP\" que te va a permitir rodearte de personas que catapulten tu negocio aún y cuando tu red de contactos actual sea más pequeña que la lista de personas que han visitado Marte.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
329
 
330
- "Ahora puedes acceder a un webinar llamado \"Dominio del Tiempo\" que te va a permitir recuperar 10 horas productivas a la semana aún y cuando ahora mismo tu agenda esté más saturada que el metro en hora punta.",
 
 
 
 
 
 
 
 
 
 
 
331
 
332
- "Tenemos para ti una consultoría llamada \"Transformación de Marca 360°\" que te va a permitir destacar en un océano de competidores aún y cuando ahora mismo tu negocio pase más desapercibido que un camaleón en la selva."
333
- ]
334
- }
335
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import google.generativeai as genai
3
+ import os
4
+ import time
5
+ from dotenv import load_dotenv
6
+ from styles import get_custom_css, get_response_html_wrapper
7
+ from formulas import offer_formulas
8
+ import PyPDF2
9
+ import docx
10
+ from PIL import Image
11
+ import io
12
+
13
+ # Set page to wide mode to use full width
14
+ st.set_page_config(layout="wide")
15
+
16
+ # Load environment variables
17
+ load_dotenv()
18
+
19
+ # Configure Google Gemini API
20
+ genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
21
+ model = genai.GenerativeModel('gemini-2.0-flash')
22
+
23
+ # Import the create_offer_instruction function from formulas
24
+ from formulas import create_offer_instruction, offer_formulas
25
+
26
+ # Initialize session state variables if they don't exist
27
+ if 'submitted' not in st.session_state:
28
+ st.session_state.submitted = False
29
+ if 'offer_result' not in st.session_state:
30
+ st.session_state.offer_result = ""
31
+ if 'generated' not in st.session_state:
32
+ st.session_state.generated = False
33
+
34
+ # Hide Streamlit menu and footer
35
+ st.markdown("""
36
+ <style>
37
+ #MainMenu {visibility: hidden;}
38
+ footer {visibility: hidden;}
39
+ header {visibility: hidden;}
40
+ </style>
41
+ """, unsafe_allow_html=True)
42
+
43
+ # Custom CSS
44
+ st.markdown(get_custom_css(), unsafe_allow_html=True)
45
+
46
+ # App title and description
47
+ st.markdown('<h1 style="text-align: center;">Great Offer Generator</h1>', unsafe_allow_html=True)
48
+ st.markdown('<h3 style="text-align: center;">Transform your skills into compelling offers!</h3>', unsafe_allow_html=True)
49
+
50
+ # Create two columns for layout - left column 40%, right column 60%
51
+ col1, col2 = st.columns([4, 6])
52
+
53
+ # Main input section in left column
54
+ with col1:
55
+ # Define the generate_offer function first
56
+ def handle_generate_button(): # Renamed to avoid conflict
57
+ has_manual_input = bool(skills or product_service)
58
+ has_file_input = bool(uploaded_file is not None and not is_image)
59
+ has_image_input = bool(uploaded_file is not None and is_image)
60
+
61
+ # Simple validation - check if we have at least one input type
62
+ if not (has_manual_input or has_file_input or has_image_input):
63
+ st.error('Por favor ingresa texto o sube un archivo/imagen')
64
+ return
65
 
66
+ st.session_state.submitted = True
67
+ st.session_state.generated = False # Reset generated flag
68
+
69
+ # Store inputs based on what's available
70
+ if has_manual_input:
71
+ st.session_state.skills = skills if skills else ""
72
+ st.session_state.product_service = product_service if product_service else ""
73
+
74
+ if has_file_input:
75
+ st.session_state.file_content = file_content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
+ if has_image_input:
78
+ st.session_state.image_parts = image_parts
79
+
80
+ # Set input type based on what's available
81
+ if has_image_input:
82
+ if has_manual_input:
83
+ st.session_state.input_type = "manual_image"
84
+ else:
85
+ st.session_state.input_type = "image"
86
+ else:
87
+ if has_manual_input and has_file_input:
88
+ st.session_state.input_type = "both"
89
+ elif has_file_input:
90
+ st.session_state.input_type = "file"
91
+ elif has_manual_input:
92
+ st.session_state.input_type = "manual"
93
+
94
+ # Store common settings
95
+ st.session_state.target_audience = target_audience
96
+ st.session_state.temperature = temperature
97
+ st.session_state.formula_type = formula_type
98
+
99
+ # Keep only the manual input tab
100
+ with st.container():
101
+ skills = st.text_area('💪 Tus Habilidades', height=70,
102
+ help='Lista tus habilidades y experiencia clave')
103
+ product_service = st.text_area('🎯 Producto/Servicio', height=70,
104
+ help='Describe tu producto o servicio')
105
+
106
+ # Generate button moved here - right after product/service
107
+ st.button('Generar Oferta 🎉', on_click=handle_generate_button) # Updated function name
108
+
109
+ # Accordion for additional settings
110
+ with st.expander('⚙️ Configuración Avanzada'):
111
+ target_audience = st.text_area('👥 Público Objetivo', height=70,
112
+ help='Describe tu cliente o público ideal')
113
+
114
+ # Add file/image uploader here
115
+ uploaded_file = st.file_uploader("📄 Sube un archivo o imagen",
116
+ type=['txt', 'pdf', 'docx', 'jpg', 'jpeg', 'png'])
117
+
118
+ if uploaded_file is not None:
119
+ file_type = uploaded_file.name.split('.')[-1].lower()
120
 
121
+ # Handle text files
122
+ if file_type in ['txt', 'pdf', 'docx']:
123
+ if file_type == 'txt':
124
+ try:
125
+ file_content = uploaded_file.read().decode('utf-8')
126
+ except Exception as e:
127
+ st.error(f"Error al leer el archivo TXT: {str(e)}")
128
+ file_content = ""
129
+
130
+ elif file_type == 'pdf':
131
+ try:
132
+ import PyPDF2
133
+ pdf_reader = PyPDF2.PdfReader(uploaded_file)
134
+ file_content = ""
135
+ for page in pdf_reader.pages:
136
+ file_content += page.extract_text() + "\n"
137
+ except Exception as e:
138
+ st.error(f"Error al leer el archivo PDF: {str(e)}")
139
+ file_content = ""
140
+
141
+ elif file_type == 'docx':
142
+ try:
143
+ import docx
144
+ doc = docx.Document(uploaded_file)
145
+ file_content = "\n".join([para.text for para in doc.paragraphs])
146
+ except Exception as e:
147
+ st.error(f"Error al leer el archivo DOCX: {str(e)}")
148
+ file_content = ""
149
+
150
+ # Remove success message - no notification shown
151
+
152
+ # Set file type flag
153
+ is_image = False
154
 
155
+ # Handle image files
156
+ elif file_type in ['jpg', 'jpeg', 'png']:
157
+ try:
158
+ image = Image.open(uploaded_file)
159
+ st.image(image, caption="Imagen cargada", use_container_width=True)
160
+
161
+ image_bytes = uploaded_file.getvalue()
162
+ image_parts = [
163
+ {
164
+ "mime_type": uploaded_file.type,
165
+ "data": image_bytes
166
+ }
167
+ ]
168
+
169
+ # Set file type flag
170
+ is_image = True
171
+ except Exception as e:
172
+ st.error(f"Error al procesar la imagen: {str(e)}")
173
+ is_image = False
174
+
175
+ # Selector de fórmula
176
+ formula_type = st.selectbox(
177
+ '📋 Tipo de Fórmula',
178
+ options=list(offer_formulas.keys()),
179
+ help='Selecciona el tipo de fórmula para tu oferta'
180
+ )
181
+
182
+ temperature = st.slider('🌡️ Nivel de Creatividad', min_value=0.0, max_value=2.0, value=0.7,
183
+ help='Valores más altos hacen que el resultado sea más creativo pero menos enfocado')
184
+
185
+ # Results column
186
+ with col2:
187
+ if st.session_state.submitted and not st.session_state.generated:
188
+ with st.spinner('Creando tu oferta perfecta...'):
189
+ # Use the create_offer_instruction function to generate the prompt
190
+ avatar_description = st.session_state.target_audience if hasattr(st.session_state, 'target_audience') and st.session_state.target_audience else 'General audience'
191
 
192
+ # Determine product name based on input type
193
+ if hasattr(st.session_state, 'product_service') and st.session_state.product_service:
194
+ product_name = st.session_state.product_service
195
+ else:
196
+ product_name = "Producto/Servicio"
197
+
198
+ # Get the instruction using the formula
199
+ instruction = create_offer_instruction(
200
+ avatar_description=avatar_description,
201
+ product_name=product_name,
202
+ selected_formula_name=st.session_state.formula_type
203
+ )
204
 
205
+ # Add additional context based on input type
206
+ if st.session_state.input_type == "manual":
207
+ additional_context = f"""
208
+ Additional information:
209
+ Skills: {st.session_state.skills}
210
+ """
211
+ instruction += additional_context
212
+ elif st.session_state.input_type == "file":
213
+ additional_context = f"""
214
+ Additional information from file:
215
+ {st.session_state.file_content}
216
+ """
217
+ instruction += additional_context
218
+ elif st.session_state.input_type == "both":
219
+ additional_context = f"""
220
+ Additional information:
221
+ Skills: {st.session_state.skills}
222
+ File content: {st.session_state.file_content}
223
+ """
224
+ instruction += additional_context
225
+
226
+ try:
227
+ generation_config = genai.GenerationConfig(temperature=st.session_state.temperature)
228
+
229
+ if "image" in st.session_state.input_type:
230
+ response = model.generate_content([instruction, st.session_state.image_parts[0]], generation_config=generation_config)
231
+ else:
232
+ response = model.generate_content(instruction, generation_config=generation_config)
233
+
234
+ st.session_state.offer_result = response.text
235
+ st.session_state.generated = True # Mark as generated
236
+
237
+ except Exception as e:
238
+ st.error(f'Ocurrió un error: {str(e)}')
239
+ st.session_state.submitted = False
240
+
241
+ # Display results if we have an offer result
242
+ if st.session_state.generated:
243
+ # With this line that uses the wrapper:
244
+ st.markdown(get_response_html_wrapper(st.session_state.offer_result), unsafe_allow_html=True)
245
+
246
+ # Add a small space
247
+ st.markdown('<div style="height: 15px;"></div>', unsafe_allow_html=True)
248
+
249
+ col_download, col_empty = st.columns([8, 2])
250
+ with col_download:
251
+ st.download_button(
252
+ label="Descargar Oferta",
253
+ data=st.session_state.offer_result,
254
+ file_name="oferta_generada.txt",
255
+ mime="text/plain"
256
+ )
257
+
258
+ # Footer
259
+ st.markdown('---')
260
+ st.markdown('Made with ❤️ by Jesús Cabrera')
261
+
262
+ # Remove the duplicate functions at the bottom