Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
result = response.json()
|
| 22 |
-
if isinstance(result, list) and len(result) > 0:
|
| 23 |
-
return result[0].get("generated_text", "")
|
| 24 |
-
elif isinstance(result, dict) and "generated_text" in result:
|
| 25 |
-
return result["generated_text"]
|
| 26 |
-
else:
|
| 27 |
-
return str(result)
|
| 28 |
-
|
| 29 |
-
gr.ChatInterface(fn=chat, title="Chat Gradio con HF").launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
+
# 🔑 Tu modelo gratuito y cliente HF
|
| 5 |
+
MODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 6 |
+
client = InferenceClient(model=MODEL_ID)
|
| 7 |
|
| 8 |
+
def responder(mensaje, historial):
|
| 9 |
+
try:
|
| 10 |
+
respuesta = ""
|
| 11 |
+
for salida in client.chat_completion(
|
| 12 |
+
model=MODEL_ID,
|
| 13 |
+
messages=[{"role": "user", "content": mensaje}],
|
| 14 |
+
max_tokens=200,
|
| 15 |
+
stream=True,
|
| 16 |
+
):
|
| 17 |
+
if salida.choices[0].delta.content:
|
| 18 |
+
respuesta += salida.choices[0].delta.content
|
| 19 |
+
historial.append((mensaje, respuesta))
|
| 20 |
+
return "", historial
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return f"⚠️ Error interno: {e}", historial
|
| 23 |
|
| 24 |
+
# 🎨 Interfaz de Gradio
|
| 25 |
+
demo = gr.ChatInterface(
|
| 26 |
+
responder,
|
| 27 |
+
title="Chat Gradio con HF (TinyLlama)",
|
| 28 |
+
description="Modelo gratuito usando TinyLlama desde Hugging Face Inference API",
|
| 29 |
+
theme="soft",
|
| 30 |
+
)
|
| 31 |
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|