Download app.py from Pulastya0/AI-Agent-Routing-Department: direct link, hf CLI and curl.
- Browser
- Download file 1.71 kB
-
https://huggingface.co/spaces/Pulastya0/AI-Agent-Routing-Department/resolve/main/app.py
- Command line
-
hf download hf://spaces/Pulastya0/AI-Agent-Routing-Department/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Pulastya0/AI-Agent-Routing-Department/resolve/main/app.py
1.71 kB
| import os | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel , Extra | |
| from transformers import pipeline | |
| # ------------------------------- | |
| # Set Hugging Face cache to writable directory | |
| # ------------------------------- | |
| os.environ["HF_HOME"] = "/tmp/huggingface" | |
| os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface" | |
| os.environ["HF_DATASETS_CACHE"] = "/tmp/huggingface" | |
| # ------------------------------- | |
| # FastAPI app | |
| # ------------------------------- | |
| app = FastAPI(title="Routing Service - Space 2") | |
| # ------------------------------- | |
| # Request Model | |
| # ------------------------------- | |
| class RoutingRequest(BaseModel): | |
| text: str | |
| class Config: | |
| extra = Extra.ignore # allow extra fields like user_email | |
| # ------------------------------- | |
| # Load Zero-Shot Classifier | |
| # ------------------------------- | |
| DEPARTMENTS = ['Account', 'Software', 'Network', 'Security', 'Hardware', | |
| 'Infrastructure', 'Licensing', 'Communication', 'RemoteWork', | |
| 'Training', 'Performance'] | |
| classifier = pipeline( | |
| "zero-shot-classification", | |
| model="MoritzLaurer/DeBERTa-v3-base-mnli-fever-anli" | |
| ) | |
| # ------------------------------- | |
| # Routing Endpoint | |
| # ------------------------------- | |
| async def route_ticket(req: RoutingRequest): | |
| text = req.text | |
| if not text: | |
| raise HTTPException(status_code=400, detail="Text cannot be empty") | |
| result = classifier(text, DEPARTMENTS) | |
| department = result["labels"][0] # highest scoring department | |
| return {"department": department} | |
| # ------------------------------- | |
| # Health Check | |
| # ------------------------------- | |
| async def health(): | |
| return {"status": "ok"} | |