Shazaaly
commited on
Commit
·
b448eb1
1
Parent(s):
af41e3e
Add FastAPI intent classifier app
Browse files- README.md +7 -11
- app.py +24 -21
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,12 +1,8 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk:
|
| 7 |
-
|
| 8 |
-
|
| 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
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
@app.post("/classify"
|
| 17 |
-
def classify_intent(
|
| 18 |
-
if not
|
| 19 |
-
return {"
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 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
|