gnosticdev commited on
Commit
97cdbe1
verified
1 Parent(s): 9dd3d1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -15
app.py CHANGED
@@ -14,6 +14,17 @@ st.set_page_config(
14
  initial_sidebar_state="expanded",
15
  )
16
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Archivo para almacenamiento permanente
18
  PROMPTS_FILE = "training_prompts.json"
19
 
@@ -120,27 +131,81 @@ elif selected == "Chatbot":
120
  st.info(current_prompt)
121
 
122
  # Display chat history
123
- for message in st.session_state.chat_session.history:
124
- with st.chat_message(translate_role_to_streamlit(message.role)):
125
- st.markdown(message.parts[0].text)
 
 
126
 
127
  # Chat input
128
  user_prompt = st.chat_input("Preguntame algo...")
129
  if user_prompt:
130
  st.chat_message("user").markdown(user_prompt)
131
- gemini_response = st.session_state.chat_session.send_message(user_prompt)
132
- with st.chat_message("assistant"):
133
- st.markdown(gemini_response.text)
 
 
 
134
 
135
  elif selected == "Image Captioning":
136
  st.title("Image Caption Generation馃摳")
 
 
 
 
 
 
 
 
 
 
137
  upload_image = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
138
- if upload_image and st.button("Generate"):
139
- image = Image.open(upload_image)
140
- col1, col2 = st.columns(2)
141
- with col1:
142
- st.image(image, caption="Uploaded Image", use_column_width=True)
143
- default_prompt = "Write a caption for this image"
144
- caption = gemini_pro_vision_responce(default_prompt, image)
145
- with col2:
146
- st.info(caption)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  initial_sidebar_state="expanded",
15
  )
16
 
17
+ # Add the missing translation function
18
+ def translate_role_to_streamlit(role):
19
+ """
20
+ Translate Gemini message roles to Streamlit chat roles
21
+ """
22
+ role_mapping = {
23
+ 'model': 'assistant',
24
+ 'user': 'user'
25
+ }
26
+ return role_mapping.get(role, 'assistant')
27
+
28
  # Archivo para almacenamiento permanente
29
  PROMPTS_FILE = "training_prompts.json"
30
 
 
131
  st.info(current_prompt)
132
 
133
  # Display chat history
134
+ if hasattr(st.session_state.chat_session, 'history'):
135
+ for message in st.session_state.chat_session.history:
136
+ role = translate_role_to_streamlit(message.role)
137
+ with st.chat_message(role):
138
+ st.markdown(message.parts[0].text)
139
 
140
  # Chat input
141
  user_prompt = st.chat_input("Preguntame algo...")
142
  if user_prompt:
143
  st.chat_message("user").markdown(user_prompt)
144
+ try:
145
+ gemini_response = st.session_state.chat_session.send_message(user_prompt)
146
+ with st.chat_message("assistant"):
147
+ st.markdown(gemini_response.text)
148
+ except Exception as e:
149
+ st.error(f"Error en la respuesta: {str(e)}")
150
 
151
  elif selected == "Image Captioning":
152
  st.title("Image Caption Generation馃摳")
153
+
154
+ # Add custom prompt option
155
+ use_custom_prompt = st.checkbox("Usar prompt personalizado")
156
+ if use_custom_prompt:
157
+ custom_prompt = st.text_area(
158
+ "Escribe tu prompt personalizado",
159
+ value="Write a caption for this image",
160
+ help="Puedes personalizar las instrucciones para el an谩lisis de la imagen"
161
+ )
162
+
163
  upload_image = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
164
+
165
+ if upload_image:
166
+ try:
167
+ image = Image.open(upload_image)
168
+ col1, col2 = st.columns(2)
169
+
170
+ with col1:
171
+ st.image(image, caption="Uploaded Image", use_column_width=True)
172
+ image_info = f"Dimensiones: {image.size[0]}x{image.size[1]}"
173
+ st.text(image_info)
174
+
175
+ if st.button("Generate", key="generate_caption"):
176
+ with st.spinner("Generando descripci贸n..."):
177
+ prompt = custom_prompt if use_custom_prompt else "Write a caption for this image"
178
+ try:
179
+ caption = gemini_pro_vision_responce(prompt, image)
180
+ with col2:
181
+ st.success("隆Descripci贸n generada!")
182
+ st.info(caption)
183
+
184
+ # Opci贸n para guardar la descripci贸n
185
+ if st.button("Guardar descripci贸n"):
186
+ timestamp = time.strftime("%Y%m%d-%H%M%S")
187
+ filename = f"caption_{timestamp}.txt"
188
+ try:
189
+ with open(filename, "w", encoding="utf-8") as f:
190
+ f.write(caption)
191
+ st.success(f"Descripci贸n guardada en {filename}")
192
+ except Exception as e:
193
+ st.error(f"Error al guardar la descripci贸n: {str(e)}")
194
+
195
+ except Exception as e:
196
+ st.error(f"Error al generar la descripci贸n: {str(e)}")
197
+ st.error("Por favor, intenta con otra imagen o revisa tu conexi贸n")
198
+
199
+ except Exception as e:
200
+ st.error(f"Error al procesar la imagen: {str(e)}")
201
+ st.error("Por favor, aseg煤rate de que el archivo es una imagen v谩lida")
202
+
203
+ # A帽adir footer
204
+ st.markdown("---")
205
+ col1, col2, col3 = st.columns(3)
206
+ with col1:
207
+ st.markdown("**GnosticDev AI**")
208
+ with col2:
209
+ st.markdown("Versi贸n 1.0.0")
210
+ with col3:
211
+ st.markdown("Made with 鉂わ笍 by GnosticDev")