JeCabrera commited on
Commit
27732bf
·
verified ·
1 Parent(s): e43ed24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -3
app.py CHANGED
@@ -4,6 +4,9 @@ import os
4
  import google.generativeai as genai
5
  from puv_formulas import puv_formulas
6
  from styles import apply_styles
 
 
 
7
 
8
  # Cargar variables de entorno
9
  load_dotenv()
@@ -12,7 +15,7 @@ load_dotenv()
12
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
13
 
14
  # Función para obtener la respuesta del modelo Gemini
15
- def get_gemini_response(product_service, target_audience, skills, formula_type, temperature):
16
  # Check if at least target audience is provided
17
  if not target_audience:
18
  return "El campo de público objetivo es obligatorio."
@@ -32,6 +35,11 @@ def get_gemini_response(product_service, target_audience, skills, formula_type,
32
  if skills:
33
  business_info += f"My Skills/Expertise: {skills}\n"
34
 
 
 
 
 
 
35
  model = genai.GenerativeModel('gemini-2.0-flash')
36
  full_prompt = f"""
37
  You are a UVP (Unique Value Proposition) expert. Analyze (internally only, do not output the analysis) the following information:
@@ -39,6 +47,7 @@ def get_gemini_response(product_service, target_audience, skills, formula_type,
39
  {business_info}
40
  Formula Type: {formula_type}
41
  {formula["description"]}
 
42
 
43
  EXAMPLE TO FOLLOW:
44
  {formula["examples"]}
@@ -81,7 +90,12 @@ def get_gemini_response(product_service, target_audience, skills, formula_type,
81
  3. [Third UVP]
82
  """
83
 
84
- response = model.generate_content([full_prompt], generation_config={"temperature": temperature})
 
 
 
 
 
85
  return response.parts[0].text if response and response.parts else "Error generating content."
86
 
87
  # Configurar la aplicación Streamlit
@@ -119,6 +133,58 @@ with col1:
119
  placeholder="Ejemplo: Experiencia en marketing digital, certificación en SEO..."
120
  )
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  with st.expander("Opciones avanzadas"):
123
  formula_type = st.selectbox(
124
  "Fórmula PUV:",
@@ -142,7 +208,9 @@ with col2:
142
  target_audience,
143
  skills,
144
  formula_type,
145
- temperature
 
 
146
  )
147
  st.write("### Propuestas Únicas de Valor")
148
  st.write(response)
 
4
  import google.generativeai as genai
5
  from puv_formulas import puv_formulas
6
  from styles import apply_styles
7
+ import PyPDF2
8
+ import docx
9
+ from PIL import Image
10
 
11
  # Cargar variables de entorno
12
  load_dotenv()
 
15
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
16
 
17
  # Función para obtener la respuesta del modelo Gemini
18
+ def get_gemini_response(product_service, target_audience, skills, formula_type, temperature, file_content="", image_parts=None):
19
  # Check if at least target audience is provided
20
  if not target_audience:
21
  return "El campo de público objetivo es obligatorio."
 
35
  if skills:
36
  business_info += f"My Skills/Expertise: {skills}\n"
37
 
38
+ # Add file content if available
39
+ reference_info = ""
40
+ if file_content:
41
+ reference_info = f"\nREFERENCE MATERIAL:\n{file_content}\n"
42
+
43
  model = genai.GenerativeModel('gemini-2.0-flash')
44
  full_prompt = f"""
45
  You are a UVP (Unique Value Proposition) expert. Analyze (internally only, do not output the analysis) the following information:
 
47
  {business_info}
48
  Formula Type: {formula_type}
49
  {formula["description"]}
50
+ {reference_info}
51
 
52
  EXAMPLE TO FOLLOW:
53
  {formula["examples"]}
 
90
  3. [Third UVP]
91
  """
92
 
93
+ # Handle text-only or text+image requests
94
+ if image_parts:
95
+ response = model.generate_content([full_prompt, image_parts], generation_config={"temperature": temperature})
96
+ else:
97
+ response = model.generate_content([full_prompt], generation_config={"temperature": temperature})
98
+
99
  return response.parts[0].text if response and response.parts else "Error generating content."
100
 
101
  # Configurar la aplicación Streamlit
 
133
  placeholder="Ejemplo: Experiencia en marketing digital, certificación en SEO..."
134
  )
135
 
136
+ # Añadir cargador de archivos
137
+ uploaded_file = st.file_uploader("📄 Archivo o imagen de referencia",
138
+ type=['txt', 'pdf', 'docx', 'jpg', 'jpeg', 'png'])
139
+
140
+ file_content = ""
141
+ is_image = False
142
+ image_parts = None
143
+
144
+ if uploaded_file is not None:
145
+ file_type = uploaded_file.name.split('.')[-1].lower()
146
+
147
+ # Manejar archivos de texto
148
+ if file_type in ['txt', 'pdf', 'docx']:
149
+ if file_type == 'txt':
150
+ try:
151
+ file_content = uploaded_file.read().decode('utf-8')
152
+ except Exception as e:
153
+ st.error(f"Error al leer el archivo TXT: {str(e)}")
154
+ file_content = ""
155
+
156
+ elif file_type == 'pdf':
157
+ try:
158
+ pdf_reader = PyPDF2.PdfReader(uploaded_file)
159
+ file_content = ""
160
+ for page in pdf_reader.pages:
161
+ file_content += page.extract_text() + "\n"
162
+ except Exception as e:
163
+ st.error(f"Error al leer el archivo PDF: {str(e)}")
164
+ file_content = ""
165
+
166
+ elif file_type == 'docx':
167
+ try:
168
+ doc = docx.Document(uploaded_file)
169
+ file_content = "\n".join([para.text for para in doc.paragraphs])
170
+ except Exception as e:
171
+ st.error(f"Error al leer el archivo DOCX: {str(e)}")
172
+ file_content = ""
173
+
174
+ # Manejar archivos de imagen
175
+ elif file_type in ['jpg', 'jpeg', 'png']:
176
+ try:
177
+ image = Image.open(uploaded_file)
178
+ image_bytes = uploaded_file.getvalue()
179
+ image_parts = {
180
+ "mime_type": uploaded_file.type,
181
+ "data": image_bytes
182
+ }
183
+ is_image = True
184
+ except Exception as e:
185
+ st.error(f"Error al procesar la imagen: {str(e)}")
186
+ is_image = False
187
+
188
  with st.expander("Opciones avanzadas"):
189
  formula_type = st.selectbox(
190
  "Fórmula PUV:",
 
208
  target_audience,
209
  skills,
210
  formula_type,
211
+ temperature,
212
+ file_content,
213
+ image_parts
214
  )
215
  st.write("### Propuestas Únicas de Valor")
216
  st.write(response)