Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,10 +6,6 @@ from langchain.chains import LLMChain
|
|
6 |
from langchain.prompts import PromptTemplate
|
7 |
from transformers import pipeline
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
# 2. Model Initialization
|
13 |
# Initialize sentiment analyzer
|
14 |
sentiment_analyzer = pipeline(
|
15 |
"sentiment-analysis",
|
@@ -20,10 +16,10 @@ sentiment_analyzer = pipeline(
|
|
20 |
# Initialize LLM
|
21 |
llm = HuggingFaceHub(
|
22 |
repo_id="deepseek-ai/deepseek-coder-33b-instruct",
|
23 |
-
model_kwargs={"temperature": 0.7}
|
|
|
24 |
)
|
25 |
|
26 |
-
# 3. Templates
|
27 |
email_template = PromptTemplate(
|
28 |
input_variables=["previous_interaction", "situation_type", "tone", "urgency"],
|
29 |
template="""Based on these details, generate a professional follow-up email:
|
@@ -41,39 +37,8 @@ Generate a personalized email that:
|
|
41 |
"""
|
42 |
)
|
43 |
|
44 |
-
scoring_template = """
|
45 |
-
Analyze this follow-up email carefully and provide scores on a scale of 1-10 for each category:
|
46 |
-
|
47 |
-
Email to analyze:
|
48 |
-
{email_text}
|
49 |
-
|
50 |
-
Please provide numerical scores and explanations in this exact format:
|
51 |
-
|
52 |
-
CLARITY SCORE: [1-10]
|
53 |
-
Explanation: [Why this score]
|
54 |
-
|
55 |
-
PROFESSIONALISM SCORE: [1-10]
|
56 |
-
Explanation: [Why this score]
|
57 |
-
|
58 |
-
ACTION ITEMS SCORE: [1-10]
|
59 |
-
Explanation: [Why this score]
|
60 |
-
|
61 |
-
PERSONALIZATION SCORE: [1-10]
|
62 |
-
Explanation: [Why this score]
|
63 |
-
|
64 |
-
OVERALL EFFECTIVENESS SCORE: [1-10]
|
65 |
-
Explanation: [Why this score]
|
66 |
-
|
67 |
-
IMPROVEMENT SUGGESTIONS:
|
68 |
-
1. [First suggestion]
|
69 |
-
2. [Second suggestion]
|
70 |
-
3. [Third suggestion]
|
71 |
-
"""
|
72 |
-
|
73 |
-
# 4. Create LangChain
|
74 |
email_chain = LLMChain(llm=llm, prompt=email_template)
|
75 |
|
76 |
-
# 5. Helper Functions
|
77 |
def analyze_sentiment(text):
|
78 |
try:
|
79 |
result = sentiment_analyzer(text)[0]
|
@@ -86,85 +51,41 @@ def analyze_sentiment(text):
|
|
86 |
except Exception as e:
|
87 |
return 'Professional'
|
88 |
|
89 |
-
# 6. Main Generation Function
|
90 |
def generate_followup_email(previous_interaction, situation_type, tone, urgency):
|
91 |
try:
|
92 |
if not tone:
|
93 |
tone = analyze_sentiment(previous_interaction)
|
94 |
-
|
95 |
-
# Generate email
|
96 |
-
email_result = email_chain.run({
|
97 |
"previous_interaction": previous_interaction,
|
98 |
"situation_type": situation_type,
|
99 |
"tone": tone,
|
100 |
"urgency": urgency
|
101 |
})
|
102 |
-
|
103 |
-
# Generate score
|
104 |
-
score_result = llm(scoring_template.format(email_text=email_result))
|
105 |
-
return email_result, score_result
|
106 |
except Exception as e:
|
107 |
-
return f"Error generating email: {str(e)}"
|
108 |
|
109 |
-
# 7. Gradio Interface
|
110 |
demo = gr.Interface(
|
111 |
fn=generate_followup_email,
|
112 |
inputs=[
|
113 |
-
gr.Textbox(
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
gr.Dropdown(
|
119 |
-
|
120 |
-
|
121 |
-
"Complaint Resolution",
|
122 |
-
"Service Issue",
|
123 |
-
"Payment Dispute",
|
124 |
-
"Product Query",
|
125 |
-
"General Follow-up"
|
126 |
-
]
|
127 |
-
),
|
128 |
-
gr.Dropdown(
|
129 |
-
label="Tone (Optional - will be automatically detected if not specified)",
|
130 |
-
choices=[
|
131 |
-
"",
|
132 |
-
"Professional",
|
133 |
-
"Apologetic",
|
134 |
-
"Friendly",
|
135 |
-
"Formal",
|
136 |
-
"Empathetic"
|
137 |
-
]
|
138 |
-
),
|
139 |
-
gr.Dropdown(
|
140 |
-
label="Urgency",
|
141 |
-
choices=["High", "Medium", "Low"]
|
142 |
-
)
|
143 |
-
],
|
144 |
-
outputs=[
|
145 |
-
gr.Textbox(label="Generated Email"),
|
146 |
-
gr.Textbox(label="Email Score and Suggestions")
|
147 |
],
|
148 |
-
|
149 |
-
|
|
|
150 |
examples=[
|
151 |
-
[
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
"High"
|
156 |
-
],
|
157 |
-
[
|
158 |
-
"Client requested information about premium features and pricing",
|
159 |
-
"Product Query",
|
160 |
-
"Professional",
|
161 |
-
"Medium"
|
162 |
-
]
|
163 |
]
|
164 |
)
|
165 |
|
166 |
-
# 8. Launch App
|
167 |
if __name__ == "__main__":
|
168 |
-
demo.launch()
|
169 |
-
|
170 |
-
|
|
|
6 |
from langchain.prompts import PromptTemplate
|
7 |
from transformers import pipeline
|
8 |
|
|
|
|
|
|
|
|
|
9 |
# Initialize sentiment analyzer
|
10 |
sentiment_analyzer = pipeline(
|
11 |
"sentiment-analysis",
|
|
|
16 |
# Initialize LLM
|
17 |
llm = HuggingFaceHub(
|
18 |
repo_id="deepseek-ai/deepseek-coder-33b-instruct",
|
19 |
+
model_kwargs={"temperature": 0.7},
|
20 |
+
huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
21 |
)
|
22 |
|
|
|
23 |
email_template = PromptTemplate(
|
24 |
input_variables=["previous_interaction", "situation_type", "tone", "urgency"],
|
25 |
template="""Based on these details, generate a professional follow-up email:
|
|
|
37 |
"""
|
38 |
)
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
email_chain = LLMChain(llm=llm, prompt=email_template)
|
41 |
|
|
|
42 |
def analyze_sentiment(text):
|
43 |
try:
|
44 |
result = sentiment_analyzer(text)[0]
|
|
|
51 |
except Exception as e:
|
52 |
return 'Professional'
|
53 |
|
|
|
54 |
def generate_followup_email(previous_interaction, situation_type, tone, urgency):
|
55 |
try:
|
56 |
if not tone:
|
57 |
tone = analyze_sentiment(previous_interaction)
|
58 |
+
return email_chain.run({
|
|
|
|
|
59 |
"previous_interaction": previous_interaction,
|
60 |
"situation_type": situation_type,
|
61 |
"tone": tone,
|
62 |
"urgency": urgency
|
63 |
})
|
|
|
|
|
|
|
|
|
64 |
except Exception as e:
|
65 |
+
return f"Error generating email: {str(e)}"
|
66 |
|
|
|
67 |
demo = gr.Interface(
|
68 |
fn=generate_followup_email,
|
69 |
inputs=[
|
70 |
+
gr.Textbox(label="Previous Interaction", lines=5,
|
71 |
+
placeholder="Describe the previous interaction with the customer..."),
|
72 |
+
gr.Dropdown(label="Situation Type",
|
73 |
+
choices=["Complaint Resolution", "Service Issue",
|
74 |
+
"Payment Dispute", "Product Query", "General Follow-up"]),
|
75 |
+
gr.Dropdown(label="Tone (Optional - will be automatically detected if not specified)",
|
76 |
+
choices=["", "Professional", "Apologetic", "Friendly", "Formal", "Empathetic"]),
|
77 |
+
gr.Dropdown(label="Urgency", choices=["High", "Medium", "Low"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
],
|
79 |
+
outputs=gr.Textbox(label="Generated Email"),
|
80 |
+
title="Smart Sales Email Generator",
|
81 |
+
description="Generate personalized follow-up emails based on previous interactions",
|
82 |
examples=[
|
83 |
+
["Customer complained about slow website loading times and threatened to cancel subscription",
|
84 |
+
"Complaint Resolution", "Apologetic", "High"],
|
85 |
+
["Client requested information about premium features and pricing",
|
86 |
+
"Product Query", "Professional", "Medium"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
]
|
88 |
)
|
89 |
|
|
|
90 |
if __name__ == "__main__":
|
91 |
+
demo.launch()
|
|
|
|