File size: 1,986 Bytes
a214e5d
51c5e9c
981848a
a214e5d
 
981848a
3d88e9e
 
 
 
a214e5d
3d88e9e
 
a214e5d
3d88e9e
a214e5d
 
 
 
609d588
a214e5d
 
 
 
 
 
3d88e9e
 
 
609d588
3d88e9e
 
 
 
a214e5d
3d88e9e
 
 
609d588
3d88e9e
 
 
a214e5d
ceef248
a214e5d
 
609d588
3d88e9e
 
609d588
 
3d88e9e
 
 
609d588
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# app.py
import os

os.environ["HF_HOME"] = "/tmp/huggingface"
os.makedirs("/tmp/huggingface", exist_ok=True)

from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
from PIL import Image
import torch
from transformers import AutoImageProcessor, SiglipForImageClassification
from io import BytesIO

app = FastAPI(title="Alphabet Sign Language Detection API")

# Load model and processor once (to speed up inference)
model_name = "prithivMLmods/Alphabet-Sign-Language-Detection"
model = SiglipForImageClassification.from_pretrained(model_name)
processor = AutoImageProcessor.from_pretrained(model_name)

# Label map
labels = {
    "0": "A", "1": "B", "2": "C", "3": "D", "4": "E", "5": "F", "6": "G", "7": "H", "8": "I", "9": "J",
    "10": "K", "11": "L", "12": "M", "13": "N", "14": "O", "15": "P", "16": "Q", "17": "R", "18": "S", "19": "T",
    "20": "U", "21": "V", "22": "W", "23": "X", "24": "Y", "25": "Z"
}

@app.get("/")
def read_root():
    return {"message": "Welcome to the Sign Language Detection API!"}

@app.post("/predict")
async def predict(file: UploadFile = File(...)):
    try:
        # Read image
        image_bytes = await file.read()
        image = Image.open(BytesIO(image_bytes)).convert("RGB")

        # Process and predict
        inputs = processor(images=image, return_tensors="pt")
        with torch.no_grad():
            outputs = model(**inputs)
            probs = torch.nn.functional.softmax(outputs.logits, dim=1).squeeze().tolist()

        # Build predictions dict
        predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
        top_prediction = max(predictions, key=predictions.get)

        return JSONResponse(content={
            "predicted_letter": top_prediction,
            "confidence": predictions[top_prediction],
            "all_predictions": predictions
        })
    except Exception as e:
        return JSONResponse(content={"error": str(e)}, status_code=500)