Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom-560m"
|
| 6 |
headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
|
| 7 |
|
| 8 |
def query(payload):
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def respond(message, history):
|
| 13 |
if not message:
|
| 14 |
-
return "
|
| 15 |
|
| 16 |
output = query({
|
| 17 |
"inputs": "Responde en español y explica de forma sencilla: " + message
|
| 18 |
})
|
| 19 |
|
| 20 |
-
# 🔥 MANEJO DE ERRORES BIEN HECHO
|
| 21 |
if isinstance(output, list) and "generated_text" in output[0]:
|
| 22 |
return output[0]["generated_text"]
|
| 23 |
-
elif isinstance(output, dict) and "error" in output:
|
| 24 |
-
return "La IA está cargando o hubo un error 😭 intenta otra vez"
|
| 25 |
else:
|
| 26 |
-
return
|
| 27 |
|
| 28 |
demo = gr.ChatInterface(fn=respond)
|
| 29 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom-560m"
|
| 7 |
headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
|
| 8 |
|
| 9 |
def query(payload):
|
| 10 |
+
for _ in range(3): # intenta 3 veces
|
| 11 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 12 |
+
result = response.json()
|
| 13 |
+
|
| 14 |
+
# Si el modelo está cargando, espera y reintenta
|
| 15 |
+
if isinstance(result, dict) and "error" in result:
|
| 16 |
+
time.sleep(5)
|
| 17 |
+
continue
|
| 18 |
+
|
| 19 |
+
return result
|
| 20 |
+
|
| 21 |
+
return {"error": "No response"}
|
| 22 |
|
| 23 |
def respond(message, history):
|
| 24 |
if not message:
|
| 25 |
+
return "Pregúntame algo 😊"
|
| 26 |
|
| 27 |
output = query({
|
| 28 |
"inputs": "Responde en español y explica de forma sencilla: " + message
|
| 29 |
})
|
| 30 |
|
|
|
|
| 31 |
if isinstance(output, list) and "generated_text" in output[0]:
|
| 32 |
return output[0]["generated_text"]
|
|
|
|
|
|
|
| 33 |
else:
|
| 34 |
+
return "La IA está ocupada 😭 intenta otra vez en unos segundos"
|
| 35 |
|
| 36 |
demo = gr.ChatInterface(fn=respond)
|
| 37 |
|