Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +118 -40
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,118 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install (run once)
|
| 2 |
+
!pip install -q transformers accelerate torch ipywidgets
|
| 3 |
+
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from IPython.display import display, clear_output
|
| 6 |
+
import ipywidgets as widgets
|
| 7 |
+
|
| 8 |
+
print("Loading WORKING 3 PIPELINES...")
|
| 9 |
+
|
| 10 |
+
# β
WORKING PIPELINES (your other 2 models + reliable classifier)
|
| 11 |
+
classifier = pipeline("text-classification", model="distilbert-base-uncased")
|
| 12 |
+
generator = pipeline("text-generation", model="Kunal7370944861/Email-Writer-AI")
|
| 13 |
+
translator = pipeline("translation", model="DDDSSS/translation_en-zh")
|
| 14 |
+
|
| 15 |
+
print("β
ALL PIPELINES LOADED!")
|
| 16 |
+
|
| 17 |
+
# Helper functions (unchanged)
|
| 18 |
+
def classify_email(text: str):
|
| 19 |
+
result = classifier(text[:512])[0]
|
| 20 |
+
return result["label"], float(result["score"])
|
| 21 |
+
|
| 22 |
+
def build_prompt(email_text: str, category: str) -> str:
|
| 23 |
+
return f"""You are a helpful customer service agent.
|
| 24 |
+
Email category: {category}
|
| 25 |
+
|
| 26 |
+
Customer email:
|
| 27 |
+
{email_text}
|
| 28 |
+
|
| 29 |
+
Write a polite, concise reply template.
|
| 30 |
+
Reply:"""
|
| 31 |
+
|
| 32 |
+
def generate_reply(prompt: str) -> str:
|
| 33 |
+
outputs = generator(prompt, max_length=300, num_return_sequences=1, do_sample=True, temperature=0.7)
|
| 34 |
+
full_text = outputs[0]["generated_text"]
|
| 35 |
+
if "Reply:" in full_text:
|
| 36 |
+
return full_text.split("Reply:", 1)[-1].strip()
|
| 37 |
+
return full_text.replace(prompt, "").strip()
|
| 38 |
+
|
| 39 |
+
def translate_reply(text: str) -> str:
|
| 40 |
+
if not text.strip(): return ""
|
| 41 |
+
outputs = translator(text)
|
| 42 |
+
return outputs[0]["translation_text"].strip()
|
| 43 |
+
|
| 44 |
+
# UI
|
| 45 |
+
print("\n" + "="*70)
|
| 46 |
+
print("π§ EMAIL PROCESSOR - READY!")
|
| 47 |
+
print("="*70)
|
| 48 |
+
|
| 49 |
+
email_input = widgets.Textarea(
|
| 50 |
+
value="",
|
| 51 |
+
placeholder="Paste your email here...",
|
| 52 |
+
layout=widgets.Layout(width='98%', height='120px')
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
process_btn = widgets.Button(
|
| 56 |
+
description="π PROCESS EMAIL",
|
| 57 |
+
button_style='success',
|
| 58 |
+
layout=widgets.Layout(width='220px')
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
clear_btn = widgets.Button(
|
| 62 |
+
description="π§Ή CLEAR",
|
| 63 |
+
button_style='warning',
|
| 64 |
+
layout=widgets.Layout(width='120px')
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
output_box = widgets.Output()
|
| 68 |
+
|
| 69 |
+
def on_process_clicked(b):
|
| 70 |
+
with output_box:
|
| 71 |
+
clear_output(wait=True)
|
| 72 |
+
email_text = email_input.value.strip()
|
| 73 |
+
|
| 74 |
+
if not email_text:
|
| 75 |
+
print("β Paste an email!")
|
| 76 |
+
return
|
| 77 |
+
|
| 78 |
+
print("π§ EMAIL")
|
| 79 |
+
print("-"*60)
|
| 80 |
+
print(email_text)
|
| 81 |
+
print("\n"+"="*60)
|
| 82 |
+
|
| 83 |
+
# PIPELINE 1: WORKING CLASSIFIER
|
| 84 |
+
print("π’ PIPELINE 1: distilbert-base-uncased-finetuned-sst-2-english")
|
| 85 |
+
label, score = classify_email(email_text)
|
| 86 |
+
print(f"β
TAG: **{label}** ({score:.1%})")
|
| 87 |
+
|
| 88 |
+
# PIPELINE 2: YOUR MODEL
|
| 89 |
+
print("\nβοΈ PIPELINE 2: Kunal7370944861/Email-Writer-AI")
|
| 90 |
+
prompt = build_prompt(email_text, label)
|
| 91 |
+
reply_en = generate_reply(prompt)
|
| 92 |
+
print("β
ENGLISH REPLY:")
|
| 93 |
+
print("-"*60)
|
| 94 |
+
print(reply_en)
|
| 95 |
+
|
| 96 |
+
# PIPELINE 3: YOUR MODEL
|
| 97 |
+
print("\nπ¨π³ PIPELINE 3: DDDSSS/translation_en-zh")
|
| 98 |
+
reply_zh = translate_reply(reply_en)
|
| 99 |
+
print("β
δΈζεε€:")
|
| 100 |
+
print("-"*60)
|
| 101 |
+
print(reply_zh)
|
| 102 |
+
|
| 103 |
+
print("\nπ DONE!")
|
| 104 |
+
|
| 105 |
+
def on_clear_clicked(b):
|
| 106 |
+
email_input.value = ""
|
| 107 |
+
output_box.clear_output()
|
| 108 |
+
|
| 109 |
+
process_btn.on_click(on_process_clicked)
|
| 110 |
+
clear_btn.on_click(on_clear_clicked)
|
| 111 |
+
|
| 112 |
+
display(widgets.VBox([
|
| 113 |
+
widgets.HTML("<h3>π¨ Paste Email (Blank Input)</h3>"),
|
| 114 |
+
email_input,
|
| 115 |
+
widgets.HBox([process_btn, clear_btn]),
|
| 116 |
+
widgets.HTML("<br>"),
|
| 117 |
+
output_box
|
| 118 |
+
]))
|