felipecaspol commited on
Commit
39b5a26
·
1 Parent(s): d4b01d0

Subiendo archivos de la API a Hugging Face

Browse files
Files changed (3) hide show
  1. Dockerfile +10 -0
  2. api.py +28 -0
  3. requirements.txt +8 -0
Dockerfile ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /app
4
+ COPY . /app
5
+
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ EXPOSE 7860
9
+
10
+ CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "7860"]
api.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ # Inicializar FastAPI
6
+ app = FastAPI()
7
+
8
+ # Cargar modelo y tokenizer
9
+ MODEL_PATH = "models/phi2_finetuned_full"
10
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
11
+ model = AutoModelForCausalLM.from_pretrained(MODEL_PATH, torch_dtype=torch.float16, device_map="cpu")
12
+
13
+ @app.get("/")
14
+ def home():
15
+ return {"message": "API de Chatbot con Phi-2 está en funcionamiento"}
16
+
17
+ @app.post("/predict/")
18
+ def predict(input_text: str):
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)
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ torch
4
+ transformers
5
+ fastapi
6
+ uvicorn
7
+ torch
8
+ transformers