Spaces:
Sleeping
Sleeping
Alex Vega
commited on
Commit
·
212f1ad
1
Parent(s):
4fa7efe
up
Browse files- Dockerfile +13 -0
- main.py +48 -0
- requirements.txt +7 -0
Dockerfile
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.12-slim
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
COPY ./requirements.txt .
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
EXPOSE 7860
|
12 |
+
|
13 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile
|
2 |
+
from transformers import ViTImageProcessor, ViTForImageClassification
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
import io
|
6 |
+
|
7 |
+
MODEL_NAME = "ahmed-masoud/sign_language_translator"
|
8 |
+
|
9 |
+
|
10 |
+
try:
|
11 |
+
processor = ViTImageProcessor.from_pretrained(MODEL_NAME)
|
12 |
+
|
13 |
+
model = ViTForImageClassification.from_pretrained(MODEL_NAME)
|
14 |
+
|
15 |
+
print(f"Modelo '{MODEL_NAME}' cargado")
|
16 |
+
|
17 |
+
except Exception as e:
|
18 |
+
print(f"Error al cargar el modelo {e}")
|
19 |
+
model = None
|
20 |
+
processor = None
|
21 |
+
|
22 |
+
app = FastAPI(title="API de ASL con modelo de HF")
|
23 |
+
|
24 |
+
|
25 |
+
@app.post("/predict/")
|
26 |
+
async def translate_sign(file: UploadFile = File(...)):
|
27 |
+
if not model or not processor:
|
28 |
+
return {"error": "Modelo no disponible."}
|
29 |
+
|
30 |
+
image_bytes = await file.read()
|
31 |
+
image = Image.open(io.BytesIO(image_bytes))
|
32 |
+
|
33 |
+
inputs = processor(images=image, return_tensors="pt")
|
34 |
+
|
35 |
+
with torch.no_grad():
|
36 |
+
outputs = model(**inputs)
|
37 |
+
logits = outputs.logits
|
38 |
+
|
39 |
+
predicted_class_idx = logits.argmax(-1).item()
|
40 |
+
|
41 |
+
predicted_label = model.config.id2label[predicted_class_idx]
|
42 |
+
|
43 |
+
return {"prediction": predicted_label}
|
44 |
+
|
45 |
+
|
46 |
+
@app.get("/")
|
47 |
+
def read_root():
|
48 |
+
return {"message": "API ok. Usa el endpoint /predict/ para predecir."}
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn[standard]
|
3 |
+
python-multipart
|
4 |
+
Pillow
|
5 |
+
transformers[torch]
|
6 |
+
torch
|
7 |
+
torchvision
|