byinab commited on
Commit
f9c20ec
Β·
verified Β·
1 Parent(s): 55f7d56

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +118 -40
src/streamlit_app.py CHANGED
@@ -1,40 +1,118 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
- import streamlit as st
5
-
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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
+ ]))