JeCabrera commited on
Commit
b7da667
verified
1 Parent(s): 82f8b61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -21
app.py CHANGED
@@ -6,6 +6,8 @@ from styles import get_custom_css
6
  from formulas import offer_formulas
7
  import PyPDF2
8
  import docx
 
 
9
 
10
  # Set page to wide mode to use full width
11
  st.set_page_config(layout="wide")
@@ -15,7 +17,7 @@ load_dotenv()
15
 
16
  # Configure Google Gemini API
17
  genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
18
- model = genai.GenerativeModel('gemini-2.0-flash')
19
 
20
  # Initialize session state variables if they don't exist
21
  if 'submitted' not in st.session_state:
@@ -45,7 +47,7 @@ col1, col2 = st.columns([4, 6])
45
  # Main input section in left column
46
  with col1:
47
  # Add tabs for different input methods
48
- input_tab1, input_tab2 = st.tabs(["Entrada Manual", "Subir Archivo"])
49
 
50
  with input_tab1:
51
  skills = st.text_area('馃挭 Tus Habilidades', height=70,
@@ -90,6 +92,23 @@ with col1:
90
  with st.expander("Vista previa del contenido"):
91
  st.write(file_content[:500] + "..." if len(file_content) > 500 else file_content)
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  # Accordion for additional settings
94
  with st.expander('鈿欙笍 Configuraci贸n Avanzada'):
95
  target_audience = st.text_area('馃懃 P煤blico Objetivo', height=70,
@@ -110,11 +129,12 @@ with col1:
110
  # Generate button with callback
111
  def generate_offer():
112
  has_manual_input = bool(skills and product_service)
113
- has_file_input = bool(uploaded_file is not None)
 
114
 
115
- # Handle all three scenarios
116
- if not has_manual_input and not has_file_input:
117
- st.error('Por favor ingresa texto o sube un archivo')
118
  return
119
 
120
  st.session_state.submitted = True
@@ -126,14 +146,27 @@ with col1:
126
 
127
  if has_file_input:
128
  st.session_state.file_content = file_content
 
 
 
129
 
130
  # Set input type
131
- if has_manual_input and has_file_input:
132
- st.session_state.input_type = "both"
133
- elif has_manual_input:
134
- st.session_state.input_type = "manual"
 
 
 
 
 
135
  else:
136
- st.session_state.input_type = "file"
 
 
 
 
 
137
 
138
  # Store common settings
139
  st.session_state.target_audience = target_audience
@@ -147,33 +180,67 @@ with col2:
147
  # Check if form has been submitted
148
  if st.session_state.submitted:
149
  with st.spinner('Creando tu oferta perfecta...'):
150
- # Determine which input source to use
 
 
 
 
 
 
151
  if st.session_state.input_type == "manual":
152
- prompt = f"""Based on the following information, create a compelling offer using the {st.session_state.formula_type} formula:
 
153
  Skills: {st.session_state.skills}
154
  Product/Service: {st.session_state.product_service}
155
- Target Audience: {st.session_state.target_audience if st.session_state.target_audience else 'General audience'}
156
  """
157
  elif st.session_state.input_type == "file":
158
- prompt = f"""Based on the following information from the uploaded file, create a compelling offer using the {st.session_state.formula_type} formula:
 
159
  File Content: {st.session_state.file_content}
160
- Target Audience: {st.session_state.target_audience if st.session_state.target_audience else 'General audience'}
161
  """
162
- else: # both inputs
163
- prompt = f"""Based on the following combined information, create a compelling offer using the {st.session_state.formula_type} formula:
 
 
 
 
 
164
  Skills: {st.session_state.skills}
165
  Product/Service: {st.session_state.product_service}
166
  Additional Information from File: {st.session_state.file_content}
167
- Target Audience: {st.session_state.target_audience if st.session_state.target_audience else 'General audience'}
168
 
169
  Please consider both the manual input and file content to create a comprehensive offer.
170
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
  prompt += f"""
173
  Formula Description:
174
  {offer_formulas[st.session_state.formula_type]["description"]}
175
 
176
- Please create a professional and engaging offer that highlights the value proposition.
177
 
178
  IMPORTANT: Provide ONLY the final offer text. Do not include any explanations, labels, formatting instructions, brackets, or call to action at the end."""
179
 
@@ -182,7 +249,11 @@ with col2:
182
  generation_config = genai.GenerationConfig(temperature=st.session_state.temperature)
183
 
184
  # Pass the generation config to generate_content
185
- response = model.generate_content(prompt, generation_config=generation_config)
 
 
 
 
186
  st.session_state.offer_result = response.text
187
 
188
  # Display result - without emoji
 
6
  from formulas import offer_formulas
7
  import PyPDF2
8
  import docx
9
+ from PIL import Image
10
+ import io
11
 
12
  # Set page to wide mode to use full width
13
  st.set_page_config(layout="wide")
 
17
 
18
  # Configure Google Gemini API
19
  genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
20
+ model = genai.GenerativeModel('gemini-1.5-flash') # Updated to model that supports images
21
 
22
  # Initialize session state variables if they don't exist
23
  if 'submitted' not in st.session_state:
 
47
  # Main input section in left column
48
  with col1:
49
  # Add tabs for different input methods
50
+ input_tab1, input_tab2, input_tab3 = st.tabs(["Entrada Manual", "Subir Archivo", "Subir Imagen"])
51
 
52
  with input_tab1:
53
  skills = st.text_area('馃挭 Tus Habilidades', height=70,
 
92
  with st.expander("Vista previa del contenido"):
93
  st.write(file_content[:500] + "..." if len(file_content) > 500 else file_content)
94
 
95
+ with input_tab3:
96
+ uploaded_image = st.file_uploader("Sube una imagen de tu producto o servicio", type=['jpg', 'jpeg', 'png'])
97
+ if uploaded_image is not None:
98
+ # Display the uploaded image
99
+ image = Image.open(uploaded_image)
100
+ st.image(image, caption="Imagen cargada", use_column_width=True)
101
+
102
+ # Convert to format for Gemini
103
+ image_bytes = uploaded_image.getvalue()
104
+ image_parts = [
105
+ {
106
+ "mime_type": uploaded_image.type,
107
+ "data": image_bytes
108
+ }
109
+ ]
110
+ st.success(f"Imagen cargada correctamente: {uploaded_image.name}")
111
+
112
  # Accordion for additional settings
113
  with st.expander('鈿欙笍 Configuraci贸n Avanzada'):
114
  target_audience = st.text_area('馃懃 P煤blico Objetivo', height=70,
 
129
  # Generate button with callback
130
  def generate_offer():
131
  has_manual_input = bool(skills and product_service)
132
+ has_file_input = bool('uploaded_file' in locals() and uploaded_file is not None)
133
+ has_image_input = bool('uploaded_image' in locals() and uploaded_image is not None)
134
 
135
+ # Handle all scenarios
136
+ if not (has_manual_input or has_file_input or has_image_input):
137
+ st.error('Por favor ingresa texto, sube un archivo o una imagen')
138
  return
139
 
140
  st.session_state.submitted = True
 
146
 
147
  if has_file_input:
148
  st.session_state.file_content = file_content
149
+
150
+ if has_image_input:
151
+ st.session_state.image_parts = image_parts
152
 
153
  # Set input type
154
+ if has_image_input:
155
+ if has_manual_input and has_file_input:
156
+ st.session_state.input_type = "all"
157
+ elif has_manual_input:
158
+ st.session_state.input_type = "manual_image"
159
+ elif has_file_input:
160
+ st.session_state.input_type = "file_image"
161
+ else:
162
+ st.session_state.input_type = "image"
163
  else:
164
+ if has_manual_input and has_file_input:
165
+ st.session_state.input_type = "both"
166
+ elif has_manual_input:
167
+ st.session_state.input_type = "manual"
168
+ else:
169
+ st.session_state.input_type = "file"
170
 
171
  # Store common settings
172
  st.session_state.target_audience = target_audience
 
180
  # Check if form has been submitted
181
  if st.session_state.submitted:
182
  with st.spinner('Creando tu oferta perfecta...'):
183
+ # Determine which input source to use and create appropriate prompt
184
+ base_prompt = f"""You are a professional copywriter specializing in creating irresistible offers.
185
+ Create a compelling and persuasive offer using the {st.session_state.formula_type} formula.
186
+
187
+ Target Audience: {st.session_state.target_audience if hasattr(st.session_state, 'target_audience') and st.session_state.target_audience else 'General audience'}
188
+ """
189
+
190
  if st.session_state.input_type == "manual":
191
+ prompt = base_prompt + f"""
192
+ Based on the following information:
193
  Skills: {st.session_state.skills}
194
  Product/Service: {st.session_state.product_service}
 
195
  """
196
  elif st.session_state.input_type == "file":
197
+ prompt = base_prompt + f"""
198
+ Based on the following information from the uploaded file:
199
  File Content: {st.session_state.file_content}
 
200
  """
201
+ elif st.session_state.input_type == "image":
202
+ prompt = base_prompt + f"""
203
+ Based on the image provided, create an offer that highlights the visual elements and appeals to the target audience.
204
+ """
205
+ elif st.session_state.input_type == "both":
206
+ prompt = base_prompt + f"""
207
+ Based on the following combined information:
208
  Skills: {st.session_state.skills}
209
  Product/Service: {st.session_state.product_service}
210
  Additional Information from File: {st.session_state.file_content}
 
211
 
212
  Please consider both the manual input and file content to create a comprehensive offer.
213
  """
214
+ elif st.session_state.input_type == "manual_image":
215
+ prompt = base_prompt + f"""
216
+ Based on the following information and the image provided:
217
+ Skills: {st.session_state.skills}
218
+ Product/Service: {st.session_state.product_service}
219
+
220
+ Please analyze both the text information and the visual elements in the image to create a comprehensive offer.
221
+ """
222
+ elif st.session_state.input_type == "file_image":
223
+ prompt = base_prompt + f"""
224
+ Based on the following information from the uploaded file and the image provided:
225
+ File Content: {st.session_state.file_content}
226
+
227
+ Please analyze both the file content and the visual elements in the image to create a comprehensive offer.
228
+ """
229
+ else: # all inputs
230
+ prompt = base_prompt + f"""
231
+ Based on all the following information:
232
+ Skills: {st.session_state.skills}
233
+ Product/Service: {st.session_state.product_service}
234
+ Additional Information from File: {st.session_state.file_content}
235
+
236
+ Please analyze the text information, file content, and the visual elements in the image to create the most comprehensive offer.
237
+ """
238
 
239
  prompt += f"""
240
  Formula Description:
241
  {offer_formulas[st.session_state.formula_type]["description"]}
242
 
243
+ Please create a professional, engaging, and irresistible offer that highlights the value proposition and creates urgency.
244
 
245
  IMPORTANT: Provide ONLY the final offer text. Do not include any explanations, labels, formatting instructions, brackets, or call to action at the end."""
246
 
 
249
  generation_config = genai.GenerationConfig(temperature=st.session_state.temperature)
250
 
251
  # Pass the generation config to generate_content
252
+ if "image" in st.session_state.input_type:
253
+ response = model.generate_content([prompt, st.session_state.image_parts[0]], generation_config=generation_config)
254
+ else:
255
+ response = model.generate_content(prompt, generation_config=generation_config)
256
+
257
  st.session_state.offer_result = response.text
258
 
259
  # Display result - without emoji