File size: 960 Bytes
b448eb1
7f8eaf1
 
 
b448eb1
 
 
7f8eaf1
b448eb1
b74cf70
b448eb1
7f8eaf1
b448eb1
 
 
 
 
 
 
 
7f8eaf1
b448eb1
 
 
7f8eaf1
b448eb1
 
 
 
7f8eaf1
b448eb1
 
 
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
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline

# Define the data model for the request body
class RequestModel(BaseModel):
    text: str

# Initialize the FastAPI app

app = FastAPI()

# Load the model pipeline once when the app starts
# This is efficient as it doesn't reload the model on every request
try:
    classifier = pipeline("text-classification", model="ShazaAly/syplyd-marbert-1")
    print("Model loaded successfully!")
except Exception as e:
    classifier = None
    print(f"Error loading model: {e}")

@app.get("/")
def read_root():
    return {"status": "online", "model": "ShazaAly/syplyd-marbert-1"}

@app.post("/classify")
def classify_intent(request: RequestModel):
    if not classifier:
        return {"error": "Model could not be loaded."}, 500

    # The text is in request.text
    results = classifier(request.text)
    return results[0] # Return the first (and only) result dictionary