adriansanz commited on
Commit
5a8f9f0
·
verified ·
1 Parent(s): 6365a8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -9
app.py CHANGED
@@ -285,23 +285,46 @@ def ask_youtube_video(url: str, objeto: str = "bird") -> str:
285
  from langchain_core.prompts import PromptTemplate
286
  from langchain.chains import LLMChain
287
 
 
 
288
  class BasicAgent:
289
  def __init__(self):
290
- # Aquí GeminiLLM es tu LLM personalizado que usa la API Gemini
291
  self.llm = GeminiLLM()
292
  self.chain = LLMChain(prompt=PromptTemplate.from_template("{question}"), llm=self.llm)
293
-
294
  print("BasicAgent con GeminiLLM inicializado.")
295
 
296
  def __call__(self, question: str) -> str:
297
  print(f"Pregunta recibida: {question[:50]}...")
298
- try:
299
- respuesta = self.chain.run({"question": question})
300
- print(f"Respuesta: {respuesta}")
301
- return respuesta
302
- except Exception as e:
303
- print(f"Error: {e}")
304
- return "Error procesando la pregunta."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
 
306
 
307
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
285
  from langchain_core.prompts import PromptTemplate
286
  from langchain.chains import LLMChain
287
 
288
+ import time
289
+
290
  class BasicAgent:
291
  def __init__(self):
 
292
  self.llm = GeminiLLM()
293
  self.chain = LLMChain(prompt=PromptTemplate.from_template("{question}"), llm=self.llm)
 
294
  print("BasicAgent con GeminiLLM inicializado.")
295
 
296
  def __call__(self, question: str) -> str:
297
  print(f"Pregunta recibida: {question[:50]}...")
298
+
299
+ max_retries = 5
300
+ backoff = 2 # segundos
301
+
302
+ for attempt in range(max_retries):
303
+ try:
304
+ respuesta = self.chain.run({"question": question})
305
+ print(f"Respuesta: {respuesta}")
306
+ return respuesta
307
+ except Exception as e:
308
+ error_str = str(e).lower()
309
+ print(f"Error en intento {attempt + 1}: {e}")
310
+
311
+ # Manejar error 503 específico
312
+ if "503" in error_str or "model is overloaded" in error_str:
313
+ if attempt < max_retries - 1:
314
+ print(f"Modelo saturado, reintentando en {backoff} segundos...")
315
+ time.sleep(backoff)
316
+ backoff *= 2
317
+ continue
318
+ else:
319
+ return "El modelo está saturado. Por favor, inténtalo más tarde."
320
+ # Otros errores que pueden ser manejados
321
+ elif "quota" in error_str:
322
+ return "Se ha superado la cuota de la API. Intenta nuevamente más tarde."
323
+ elif "timeout" in error_str:
324
+ return "La solicitud tardó demasiado. Intenta nuevamente."
325
+ else:
326
+ return "Error procesando la pregunta."
327
+
328
 
329
 
330
  def run_and_submit_all(profile: gr.OAuthProfile | None):