Spaces:
Paused
Paused
Commit
·
84ab11b
1
Parent(s):
39b5a26
Forzando actualización de api.py en Hugging Face
Browse files
api.py
CHANGED
|
@@ -1,28 +1,62 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
# Inicializar FastAPI
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@app.get("/")
|
| 14 |
def home():
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
@app.post("/predict/")
|
| 18 |
-
def predict(
|
| 19 |
"""Genera una respuesta basada en el input del usuario."""
|
| 20 |
-
inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
|
|
|
| 21 |
with torch.no_grad():
|
| 22 |
outputs = model.generate(**inputs, max_length=150)
|
|
|
|
| 23 |
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 24 |
return {"response": response_text}
|
| 25 |
|
|
|
|
|
|
|
| 26 |
if __name__ == "__main__":
|
| 27 |
import uvicorn
|
| 28 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Actualización forzada para subir a Hugging Face..
|
| 2 |
from fastapi import FastAPI
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
import torch
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
+
# ✅ Inicializar FastAPI
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
# ✅ Nombre del modelo en Hugging Face Hub
|
| 12 |
+
HUGGING_FACE_REPO = "fcp2207/Phi-2" # Asegúrate de que sea el nombre correcto en Hugging Face
|
| 13 |
+
MODEL_FILENAME = "phi2_finetuned.pth" # Nombre del archivo en Hugging Face
|
| 14 |
+
|
| 15 |
+
# ✅ Descargar el modelo desde Hugging Face (sin autenticación)
|
| 16 |
+
model_path = hf_hub_download(
|
| 17 |
+
repo_id=HUGGING_FACE_REPO,
|
| 18 |
+
filename=MODEL_FILENAME
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# ✅ Cargar el tokenizer y el modelo base
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2")
|
| 23 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2")
|
| 24 |
+
|
| 25 |
+
# ✅ Cargar los pesos del modelo
|
| 26 |
+
model.load_state_dict(torch.load(model_path, map_location="cpu"))
|
| 27 |
+
model.eval() # Poner el modelo en modo inferencia
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# ✅ Definir la estructura de la solicitud para la API
|
| 31 |
+
class InputText(BaseModel):
|
| 32 |
+
input_text: str
|
| 33 |
+
|
| 34 |
|
| 35 |
@app.get("/")
|
| 36 |
def home():
|
| 37 |
+
"""Endpoint de prueba para verificar que la API está activa"""
|
| 38 |
+
return {"message": "API de Chatbot con Phi-2 está en funcionamiento 🚀"}
|
| 39 |
+
|
| 40 |
|
| 41 |
@app.post("/predict/")
|
| 42 |
+
def predict(request: InputText):
|
| 43 |
"""Genera una respuesta basada en el input del usuario."""
|
| 44 |
+
inputs = tokenizer(request.input_text, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
| 45 |
+
|
| 46 |
with torch.no_grad():
|
| 47 |
outputs = model.generate(**inputs, max_length=150)
|
| 48 |
+
|
| 49 |
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 50 |
return {"response": response_text}
|
| 51 |
|
| 52 |
+
|
| 53 |
+
# ✅ Ejecución en modo local (opcional, no necesario en Hugging Face)
|
| 54 |
if __name__ == "__main__":
|
| 55 |
import uvicorn
|
| 56 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|