lstm_asl / app.py
nesuri's picture
Update app.py
d80b48e verified
from fastapi import FastAPI, Request
from tensorflow.keras.models import load_model
import numpy as np
app = FastAPI()
# Load model and labels
model = load_model("lstm_model.h5")
actions = [
"hello", "fine", "goodbye", "name", "friend", "one", "sick", "help", "deaf", "hearing",
"what", "from", "email", "weather", "rain", "three", "tuesday", "april", "child", "old",
"9oclock", "5dollars", "8hours", "soon", "lastweek"
]
@app.post("/predict")
async def predict(request: Request):
data = await request.json()
try:
input_data = np.array(data["input"])
except (KeyError, TypeError, ValueError):
return {"error": "Invalid input. Expecting JSON with key 'input' and a list of numbers."}
# Expand dims if the model expects (1, timesteps, features)
try:
prediction = model.predict(np.expand_dims(input_data, axis=0))
predicted_class_idx = int(np.argmax(prediction[0]))
predicted_label = actions[predicted_class_idx]
except Exception as e:
return {"error": str(e)}
return {
"class_index": predicted_class_idx,
"label": predicted_label,
"confidence": float(np.max(prediction[0]))
}
@app.get("/")
async def root():
return {"message": "Your ASL LSTM model API is up and running!"}