Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +39 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# ---------- Load models ----------
|
| 5 |
+
sentiment = pipeline("sentiment-analysis") # DistilBERT SST-2
|
| 6 |
+
classifier = pipeline("zero-shot-classification",
|
| 7 |
+
model="facebook/bart-large-mnli") # Zero-shot
|
| 8 |
+
|
| 9 |
+
def analyze_email(subject, body):
|
| 10 |
+
text = subject + "\n" + (body or "")
|
| 11 |
+
|
| 12 |
+
# Sentiment
|
| 13 |
+
s_res = sentiment(text)[0]
|
| 14 |
+
s_label = s_res["label"]
|
| 15 |
+
s_score = s_res["score"]
|
| 16 |
+
|
| 17 |
+
# Zero-shot custom labels
|
| 18 |
+
labels = ["engaging", "spammy", "informative", "boring", "urgent"]
|
| 19 |
+
z_res = classifier(text, labels)
|
| 20 |
+
z_scores = {l: f"{s:.2f}" for l, s in zip(z_res["labels"], z_res["scores"])}
|
| 21 |
+
|
| 22 |
+
# ---------- format output ----------
|
| 23 |
+
out = f"### Sentiment\n**{s_label}** (confidence {s_score:.2f})\n\n"
|
| 24 |
+
out += "### Quality scores\n"
|
| 25 |
+
for l, s in z_scores.items():
|
| 26 |
+
out += f"- **{l}** : {s}\n"
|
| 27 |
+
return out
|
| 28 |
+
|
| 29 |
+
demo = gr.Interface(
|
| 30 |
+
fn = analyze_email,
|
| 31 |
+
inputs = [gr.Textbox(label="Subject line"),
|
| 32 |
+
gr.Textbox(lines=6, label="Email body (optional)")],
|
| 33 |
+
outputs = gr.Markdown(),
|
| 34 |
+
title = "Email Quality & Sentiment Analyzer",
|
| 35 |
+
description = "Combines a sentiment pipeline + zero-shot classification"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|