Spaces:
Running
Running
Upload 2 files
Browse files- app.py +37 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from transformers import pipeline
|
4 |
+
from datetime import datetime
|
5 |
+
import os
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# Load model
|
10 |
+
classifier = pipeline(
|
11 |
+
"zero-shot-classification",
|
12 |
+
model="MoritzLaurer/DeBERTa-v3-base-mnli-fever-anli"
|
13 |
+
)
|
14 |
+
|
15 |
+
# Pydantic input model
|
16 |
+
class EmailRequest(BaseModel):
|
17 |
+
email: str
|
18 |
+
|
19 |
+
# Endpoint to classify email
|
20 |
+
@app.post("/classify")
|
21 |
+
def classify_email(request: EmailRequest):
|
22 |
+
labels = ["Approved", "Rejected", "Unclear"]
|
23 |
+
result = classifier(request.email, candidate_labels=labels)
|
24 |
+
top_label = result['labels'][0]
|
25 |
+
confidence = round(result['scores'][0], 2)
|
26 |
+
|
27 |
+
# Logging
|
28 |
+
log_line = f"{datetime.now()} | {top_label:<9} ({confidence}) | {request.email}\n"
|
29 |
+
os.makedirs("logs", exist_ok=True)
|
30 |
+
with open("logs/inputs.log", "a", encoding="utf-8") as f:
|
31 |
+
f.write(log_line)
|
32 |
+
|
33 |
+
return {"label": top_label, "confidence": confidence}
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
import uvicorn
|
37 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
transformers
|
4 |
+
torch
|