Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import DebertaV2Tokenizer, DebertaV2ForSequenceClassification
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
REPO_ID = "Nikpatil/Email_classifier"
|
| 7 |
+
MAX_LENGTH = 256
|
| 8 |
+
|
| 9 |
+
# Define your label mapping
|
| 10 |
+
id2label = {0: "Incident", 1: "Request", 2: "Problem", 3: "Change"}
|
| 11 |
+
labels = list(id2label.values())
|
| 12 |
+
|
| 13 |
+
# Load model & tokenizer
|
| 14 |
+
tokenizer = DebertaV2Tokenizer.from_pretrained(REPO_ID)
|
| 15 |
+
model = DebertaV2ForSequenceClassification.from_pretrained(REPO_ID)
|
| 16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
+
model.to(device)
|
| 18 |
+
model.eval()
|
| 19 |
+
|
| 20 |
+
# Your inference function (adapted from your own)
|
| 21 |
+
def inference(text, model, tokenizer, MAX_LENGTH, id2label):
|
| 22 |
+
inputs = tokenizer(
|
| 23 |
+
text,
|
| 24 |
+
add_special_tokens=True,
|
| 25 |
+
max_length=max_length,
|
| 26 |
+
padding='max_length',
|
| 27 |
+
truncation=True,
|
| 28 |
+
return_tensors='pt'
|
| 29 |
+
)
|
| 30 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 31 |
+
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
outputs = model(**inputs)
|
| 34 |
+
|
| 35 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
|
| 36 |
+
predicted_class_id = torch.argmax(probs).item()
|
| 37 |
+
predicted_class = id2label[predicted_class_id]
|
| 38 |
+
|
| 39 |
+
return f"Predicted: {predicted_class}", {id2label[i]: float(probs[i]) for i in range(len(probs))}
|
| 40 |
+
|
| 41 |
+
# Gradio interface
|
| 42 |
+
demo = gr.Interface(
|
| 43 |
+
fn=lambda text: inference(text, model, tokenizer, MAX_LENGTH, id2label),
|
| 44 |
+
inputs=gr.Textbox(lines=8, placeholder="Paste your email here..."),
|
| 45 |
+
outputs=["text", "label"],
|
| 46 |
+
title="Email Classifier (DeBERTa v2)",
|
| 47 |
+
description="Classifies emails into Incident, Request, Problem, or Change."
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
demo.launch()
|