gcmarian commited on
Commit
4c4ec3c
·
verified ·
1 Parent(s): d76f986

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -191,7 +191,7 @@ def buscar_por_genero(genero, k=5):
191
  # Formatear las recomendaciones
192
  recommendations = []
193
  for title in recommended_titles:
194
- recommendations.append(f"### {title}")
195
 
196
  return "\n".join(recommendations) if recommendations else f"No se encontraron K-Dramas del género '{genero}'."
197
 
@@ -203,7 +203,7 @@ def recomendar_kdramas_chat(entrada_usuario):
203
  if preferencia_traducida:
204
  return buscar_por_genero(preferencia_traducida, k=5)
205
  else:
206
- return "Por favor, dime qué tipo de K-Drama te gusta (romántico, acción, misterio, etc.)."
207
 
208
  # Función para generar respuestas del chatbot
209
  def generar_respuesta(entrada_usuario, historial_chat=""):
@@ -214,20 +214,27 @@ def generar_respuesta(entrada_usuario, historial_chat=""):
214
 
215
  # Función para manejar la interacción del chatbot
216
  def chat(entrada_usuario, historial_chat=""):
217
- recomendacion = ""
218
- # Check if input contains preference keywords
219
- if any(palabra in entrada_usuario.lower() for palabra in mapeo_generos.keys()):
220
- recomendacion = recomendar_kdramas_chat(entrada_usuario)
221
- respuesta = recomendacion # Update response to include recommendations (if any)
 
222
  else:
223
  respuesta = generar_respuesta(entrada_usuario, historial_chat)
224
- # Pass chatbot's response to preference check (in case it suggests a genre)
225
- recomendacion = recomendar_kdramas_chat(respuesta)
226
- # Add recommendations to chatbot response only if found
227
- if recomendacion != "Por favor, dime qué tipo de K-Drama te gusta (romántico, acción, misterio, etc.).":
228
- respuesta = f"{respuesta}\n\n{recomendacion}"
229
-
230
- return respuesta, gr.update(value="") # update with empty string
 
 
 
 
 
 
231
 
232
  # ==================================================
233
  # Interfaz de Gradio
 
191
  # Formatear las recomendaciones
192
  recommendations = []
193
  for title in recommended_titles:
194
+ recommendations.append(f"- {title}")
195
 
196
  return "\n".join(recommendations) if recommendations else f"No se encontraron K-Dramas del género '{genero}'."
197
 
 
203
  if preferencia_traducida:
204
  return buscar_por_genero(preferencia_traducida, k=5)
205
  else:
206
+ return None # No se encontró una preferencia válida
207
 
208
  # Función para generar respuestas del chatbot
209
  def generar_respuesta(entrada_usuario, historial_chat=""):
 
214
 
215
  # Función para manejar la interacción del chatbot
216
  def chat(entrada_usuario, historial_chat=""):
217
+ # Agregar el mensaje del usuario al historial
218
+ historial_chat += f"Usuario: {entrada_usuario}\n"
219
+
220
+ # Generar una respuesta natural del chatbot
221
+ if not historial_chat.strip(): # Si es la primera interacción
222
+ respuesta = "¡Hola! Soy tu asistente para recomendar K-Dramas. ¿Qué tipo de K-Drama te gustaría ver hoy?"
223
  else:
224
  respuesta = generar_respuesta(entrada_usuario, historial_chat)
225
+
226
+ # Verificar si el usuario mencionó un género
227
+ recomendacion = recomendar_kdramas_chat(entrada_usuario)
228
+ if recomendacion:
229
+ respuesta = f"¡Genial! Aquí tienes algunas recomendaciones de K-Dramas que podrían gustarte:\n{recomendacion}"
230
+ else:
231
+ respuesta = "¿Qué tipo de K-Drama te gustaría ver? Puedo recomendarte dramas de acción, romance, misterio y más."
232
+
233
+ # Agregar la respuesta del chatbot al historial
234
+ historial_chat += f"Chatbot: {respuesta}\n"
235
+
236
+ # Devolver la respuesta y el historial actualizado
237
+ return respuesta, historial_chat, "" # Limpiar el cuadro de texto
238
 
239
  # ==================================================
240
  # Interfaz de Gradio