Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,28 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
MODEL_ID = "google/gemma-2b-it"
|
| 6 |
client = InferenceClient(model=MODEL_ID)
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 22 |
except Exception as e:
|
| 23 |
-
return
|
| 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()
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
app = FastAPI()
|
| 6 |
MODEL_ID = "google/gemma-2b-it"
|
| 7 |
client = InferenceClient(model=MODEL_ID)
|
| 8 |
|
| 9 |
+
class ChatRequest(BaseModel):
|
| 10 |
+
message: str
|
| 11 |
+
history: list[list[str]] | None = None
|
| 12 |
+
|
| 13 |
+
@app.post("/chat")
|
| 14 |
+
def chat_endpoint(request: ChatRequest):
|
| 15 |
+
history = request.history or []
|
| 16 |
prompt = ""
|
| 17 |
for user_msg, bot_msg in history:
|
| 18 |
prompt += f"User: {user_msg}\nAssistant: {bot_msg}\n"
|
| 19 |
+
prompt += f"User: {request.message}\nAssistant:"
|
| 20 |
|
| 21 |
try:
|
| 22 |
response = ""
|
| 23 |
for chunk in client.text_generation(prompt, max_new_tokens=128, stream=True):
|
| 24 |
response += chunk.token.text
|
| 25 |
+
history.append((request.message, response.strip()))
|
| 26 |
+
return {"response": response.strip(), "history": history}
|
| 27 |
except Exception as e:
|
| 28 |
+
return {"response": f"⚠️ Error interno: {e}", "history": history}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|