Spaces:
Sleeping
Sleeping
Download app.py from omgkyng/email-intent-api: direct link, hf CLI and curl.
- Browser
- Download file 1.17 kB
-
https://huggingface.co/spaces/omgkyng/email-intent-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/omgkyng/email-intent-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/omgkyng/email-intent-api/resolve/main/app.py
1.17 kB
| import gradio as gr | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| # Load model and tokenizer from Hugging Face | |
| model_name = "omgkyng/email-intent-classifier" # Your model path on the Hub | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForSequenceClassification.from_pretrained(model_name) | |
| # Label index to name mapping | |
| label_map = { | |
| 0: "create", | |
| 1: "edit", | |
| 2: "query" | |
| } | |
| def classify_intent(text): | |
| inputs = tokenizer(text, return_tensors="pt") | |
| outputs = model(**inputs) | |
| probs = torch.nn.functional.softmax(outputs.logits, dim=-1) | |
| confidence, predicted_class = torch.max(probs, dim=1) | |
| label_name = label_map[predicted_class.item()] | |
| return f"Intent: {label_name}, Confidence: {confidence.item():.2f}" | |
| # Gradio Interface (visible UI + API-ready) | |
| demo = gr.Interface( | |
| fn=classify_intent, | |
| inputs=gr.Textbox(label="Enter email message"), | |
| outputs=gr.Textbox(label="Intent Prediction"), | |
| title="Email Intent Classifier", | |
| description="Classifies whether a message is a 'create', 'edit', or 'query' intent." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |