Download app.py from bmarlin96/Meridian: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/bmarlin96/Meridian/resolve/main/app.py
- Command line
-
hf download hf://spaces/bmarlin96/Meridian/app.py
-
curl -L -o app.py https://huggingface.co/spaces/bmarlin96/Meridian/resolve/main/app.py
1.06 kB
| import torch | |
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| from peft import PeftModel | |
| BASE = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" | |
| ADAPTER = "bmarlin96/meridian-router-lora" | |
| LABELS = ["shortage", "delivery", "recall", "billing", "marketing", "confirmation"] | |
| tok = AutoTokenizer.from_pretrained(BASE) | |
| tok.pad_token = tok.eos_token | |
| base = AutoModelForSequenceClassification.from_pretrained(BASE, num_labels=len(LABELS)) | |
| base.config.pad_token_id = tok.pad_token_id | |
| model = PeftModel.from_pretrained(base, ADAPTER) | |
| model.eval() | |
| app = FastAPI() | |
| class Message(BaseModel): | |
| text: str | |
| def health(): | |
| return {"status": "ok"} | |
| def classify(msg: Message): | |
| inputs = tok(msg.text, return_tensors="pt", truncation=True, max_length=96) | |
| with torch.no_grad(): | |
| probs = torch.softmax(model(**inputs).logits, dim=-1)[0] | |
| i = int(probs.argmax()) | |
| return {"label": LABELS[i], "confidence": round(float(probs[i]), 3)} |