Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,22 @@ from fastapi import FastAPI
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 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 []
|
|
@@ -22,7 +30,7 @@ def chat_endpoint(request: ChatRequest):
|
|
| 22 |
response = ""
|
| 23 |
for chunk in client.text_generation(prompt, max_new_tokens=128, stream=True):
|
| 24 |
response += chunk.token.text
|
| 25 |
-
history.append(
|
| 26 |
return {"response": response.strip(), "history": history}
|
| 27 |
except Exception as e:
|
| 28 |
return {"response": f"⚠️ Error interno: {e}", "history": history}
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# Inicializar FastAPI
|
| 6 |
+
app = FastAPI(title="Chat FastAPI", version="1.0")
|
| 7 |
+
|
| 8 |
+
# Cliente del modelo
|
| 9 |
MODEL_ID = "google/gemma-2b-it"
|
| 10 |
client = InferenceClient(model=MODEL_ID)
|
| 11 |
|
| 12 |
+
# Estructura del body que recibirá el endpoint
|
| 13 |
class ChatRequest(BaseModel):
|
| 14 |
message: str
|
| 15 |
history: list[list[str]] | None = None
|
| 16 |
|
| 17 |
+
@app.get("/")
|
| 18 |
+
def home():
|
| 19 |
+
return {"message": "✅ Chat FastAPI está corriendo correctamente"}
|
| 20 |
+
|
| 21 |
@app.post("/chat")
|
| 22 |
def chat_endpoint(request: ChatRequest):
|
| 23 |
history = request.history or []
|
|
|
|
| 30 |
response = ""
|
| 31 |
for chunk in client.text_generation(prompt, max_new_tokens=128, stream=True):
|
| 32 |
response += chunk.token.text
|
| 33 |
+
history.append([request.message, response.strip()])
|
| 34 |
return {"response": response.strip(), "history": history}
|
| 35 |
except Exception as e:
|
| 36 |
return {"response": f"⚠️ Error interno: {e}", "history": history}
|