Shazaaly commited on
Commit
b448eb1
ยท
1 Parent(s): af41e3e

Add FastAPI intent classifier app

Browse files
Files changed (3) hide show
  1. README.md +7 -11
  2. app.py +24 -21
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,12 +1,8 @@
1
  ---
2
- title: Suplyd Intent Api
3
- emoji: ๐Ÿ’ฌ
4
- colorFrom: yellow
5
- colorTo: purple
6
- sdk: gradio
7
- sdk_version: 5.0.1
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
1
  ---
2
+ title: SYPLYD MARBERT API
3
+ emoji: ๐ŸŒ
4
+ colorFrom: green
5
+ colorTo: blue
6
+ sdk: docker
7
+ app_port: 8000
8
+ ---
 
 
 
 
app.py CHANGED
@@ -1,29 +1,32 @@
1
- from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
3
  from transformers import pipeline
4
 
5
- app = FastAPI()
 
 
6
 
7
- classifier = pipeline("text-classification", model="ShazaAly/suplyd-intent-classifier")
 
8
 
9
- class IntentRequest(BaseModel):
10
- text: str
 
 
 
 
 
 
11
 
12
- class IntentResponse(BaseModel):
13
- label: str
14
- confidence: float
15
 
16
- @app.post("/classify", response_model=IntentResponse)
17
- def classify_intent(req: IntentRequest):
18
- if not req.text.strip():
19
- return {"label": "ุบูŠุฑ ุฐู„ูƒ", "confidence": 1.0}
20
 
21
- try:
22
- results = classifier(req.text)
23
- top = results[0]
24
- return {
25
- "label": top["label"],
26
- "confidence": top["score"]
27
- }
28
- except Exception as e:
29
- raise HTTPException(status_code=500, detail=str(e))
 
1
+ from fastapi import FastAPI
2
  from pydantic import BaseModel
3
  from transformers import pipeline
4
 
5
+ # Define the data model for the request body
6
+ class RequestModel(BaseModel):
7
+ text: str
8
 
9
+ # Initialize the FastAPI app
10
+ app = FastAPI()
11
 
12
+ # Load the model pipeline once when the app starts
13
+ # This is efficient as it doesn't reload the model on every request
14
+ try:
15
+ classifier = pipeline("text-classification", model="ShazaAly/syplyd-marbert-1")
16
+ print("Model loaded successfully!")
17
+ except Exception as e:
18
+ classifier = None
19
+ print(f"Error loading model: {e}")
20
 
21
+ @app.get("/")
22
+ def read_root():
23
+ return {"status": "online", "model": "ShazaAly/syplyd-marbert-1"}
24
 
25
+ @app.post("/classify")
26
+ def classify_intent(request: RequestModel):
27
+ if not classifier:
28
+ return {"error": "Model could not be loaded."}, 500
29
 
30
+ # The text is in request.text
31
+ results = classifier(request.text)
32
+ return results[0] # Return the first (and only) result dictionary
 
 
 
 
 
 
requirements.txt CHANGED
@@ -2,3 +2,6 @@ fastapi
2
  uvicorn
3
  transformers
4
  torch
 
 
 
 
2
  uvicorn
3
  transformers
4
  torch
5
+ torchvision
6
+ torchaudio
7
+ sentencepiece # MARBERT dependency