Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
MODEL_ID = "
|
| 6 |
client = InferenceClient(model=MODEL_ID)
|
| 7 |
|
| 8 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
-
|
| 11 |
-
for
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 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}",
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
description="Modelo gratuito usando TinyLlama desde Hugging Face Inference API",
|
| 29 |
-
theme="soft",
|
| 30 |
)
|
| 31 |
|
| 32 |
if __name__ == "__main__":
|
| 33 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Modelo gratuito y público
|
| 5 |
+
MODEL_ID = "google/gemma-2b-it"
|
| 6 |
client = InferenceClient(model=MODEL_ID)
|
| 7 |
|
| 8 |
+
def chat(message, history):
|
| 9 |
+
history = history or []
|
| 10 |
+
# Convierte el historial a formato texto
|
| 11 |
+
prompt = ""
|
| 12 |
+
for user_msg, bot_msg in history:
|
| 13 |
+
prompt += f"User: {user_msg}\nAssistant: {bot_msg}\n"
|
| 14 |
+
prompt += f"User: {message}\nAssistant:"
|
| 15 |
+
|
| 16 |
try:
|
| 17 |
+
response = ""
|
| 18 |
+
for chunk in client.text_generation(prompt, max_new_tokens=128, stream=True):
|
| 19 |
+
response += chunk.token.text
|
| 20 |
+
history.append((message, response.strip()))
|
| 21 |
+
return history, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
+
return history + [(message, f"⚠️ Error interno: {e}")], history
|
| 24 |
|
| 25 |
+
chatbot = gr.ChatInterface(
|
| 26 |
+
fn=chat,
|
| 27 |
+
title="Chat FastAPI con HF",
|
| 28 |
+
description="Chat simple usando google/gemma-2b-it en Hugging Face"
|
|
|
|
|
|
|
| 29 |
)
|
| 30 |
|
| 31 |
if __name__ == "__main__":
|
| 32 |
+
chatbot.launch()
|