File size: 1,273 Bytes
32854a5
80490de
84f1ee8
 
80490de
1e9ac73
80490de
1e9ac73
80490de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84f1ee8
84f505f
32854a5
80490de
 
 
 
 
 
 
 
 
 
 
 
 
 
84f505f
80490de
 
 
 
 
 
 
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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from onnxruntime import InferenceSession
import numpy as np
import os

app = FastAPI(title="ONNX Model API")

# CORS configuration
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

# Load ONNX model
model_path = os.path.join(os.getcwd(), "model.onnx")
session = InferenceSession(model_path)

@app.get("/")
def health_check():
    return {"status": "healthy", "message": "ONNX model is ready"}

@app.post("/predict")
async def predict(inputs: dict):
    """Expects {'input_ids': [], 'attention_mask': []}"""
    try:
        input_ids = np.array(inputs["input_ids"], dtype=np.int64).reshape(1, -1)
        attention_mask = np.array(inputs["attention_mask"], dtype=np.int64).reshape(1, -1)
        
        outputs = session.run(
            None,
            {
                "input_ids": input_ids,
                "attention_mask": attention_mask
            }
        )
        
        return {"embedding": outputs[0].tolist()}
    
    except Exception as e:
        return {"error": str(e)}

# Required for Hugging Face Spaces
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)