KarthickAdopleAI commited on
Commit
0441352
·
verified ·
1 Parent(s): ef837ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +166 -62
app.py CHANGED
@@ -1,63 +1,167 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import random
3
+ import time
4
+ import os
5
+ from sales_helper import SalesGPT
6
+ from langchain_openai import AzureChatOpenAI
7
+ from openai import AzureOpenAI
8
+ llm = AzureChatOpenAI(temperature=0,deployment_name="GPT-3")
9
+ from time import sleep
10
+ sales_agent = SalesGPT.from_llm(llm, verbose=False)
11
+ # init sales agent
12
+ sales_agent.seed_agent()
13
+ stage = "\n"
14
+ bot_conversation = ""
15
+ customer_conversation = ""
16
+ convo_history = sales_agent.conversation_history
17
+ client = AzureOpenAI()
18
+ def user(user_message, history):
19
+ if user_message:
20
+ sales_agent.human_step(user_message)
21
+ return "", history + [[user_message, None]]
22
+
23
+ def stages():
24
+ global stage
25
+ stage += "\n\n"+sales_agent.determine_conversation_stage()
26
+ return stage
27
+
28
+ def download_report():
29
+ global convo_history
30
+ sales_evaluation_criteria = {
31
+ "understanding": "Does the salesperson understand the customer pain points and challenges?",
32
+ "opening_effectiveness": "Was the opening of the pitch effective?",
33
+ "focus_on_benefits": "Was there sufficient focus and emphasis on customer benefits of the products/features pitched?",
34
+ "trust_building": "Did the salesperson establish trust and credibility by sharing testimonials, case studies, references, success stories of other satisfied customers?",
35
+ "urgency_creation": "Did the salesperson create urgency, such as through time-sensitive offers or consequences of not taking the decision?",
36
+ "objection_handling": "Did the salesperson handle objections well and proactively address/prepared for the objections?",
37
+ "engagement": "Was the conversation engaging?",
38
+ "balance_of_talk_and_listen": "Was there a balance between the salesperson talking and listening to the customer?",
39
+ "closing_strategy": "Was there a clear call to action, summarization, and reiteration of the value proposition in the close strategy?",
40
+ "purposefulness": "Throughout the pitch/conversation, was the conversation purposeful, and did it end with clear next steps?"
41
+ }
42
+ client = AzureOpenAI()
43
+
44
+ conversation = [
45
+ {"role": "system", "content": f"You Are Context verification Reporter.using these condition {sales_evaluation_criteria} to verify following context to Give me a Report Form of the context Scoring and Reason for Scoring."},
46
+ {"role": "user", "content": f""" this is the Context:{convo_history}.
47
+ """}
48
+ ]
49
+ response = client.chat.completions.create(
50
+ model="GPT-3",
51
+ messages=conversation,
52
+ temperature=0,
53
+ max_tokens=1000
54
+ )
55
+
56
+ message = response.choices[0].message.content
57
+ report_file_path = f"report.txt"
58
+ with open(report_file_path,"w") as file:
59
+ file.write(message)
60
+
61
+ return message
62
+
63
+ def bot(history):
64
+ bot_message = sales_agent._call({})
65
+ history[-1][1] = ""
66
+ for character in bot_message:
67
+ history[-1][1] += character
68
+ time.sleep(0.05)
69
+ yield history
70
+ summarizer = Summarizer()
71
+ sentiment = SentimentAnalyzer()
72
+
73
+ def history_of_both(convo_history):
74
+ # Initialize lists to store messages from customer and bot
75
+ customer_messages = []
76
+ bot_messages = []
77
+
78
+ # Iterate through the input list
79
+ for message in input_list:
80
+ if message.endswith('<END_OF_TURN>'):
81
+ # Customer message
82
+ if len(customer_messages) == len(bot_messages):
83
+ customer_messages.append(message[:-13])
84
+ else:
85
+ bot_messages.append(message[:-13])
86
+ else:
87
+ # Bot message
88
+ if len(customer_messages) == len(bot_messages):
89
+ bot_messages.append(message)
90
+ else:
91
+ customer_messages.append(message)
92
+
93
+ bot_conversation = " ".join(bot_messages)
94
+ customer_conversation = " ".join(customer_messages)
95
+
96
+ return bot_conversation, customer_conversation
97
+
98
+ def generate_convo_summary():
99
+ global convo_history
100
+ summary=summarizer.generate_summary(convo_history)
101
+ return summary
102
+
103
+ def sentiment_analysis():
104
+ global convo_history
105
+ bot_conversation, customer_conversation = history_of_both(convo_history)
106
+ customer_conversation_sentiment_scores = sentiment.analyze_sentiment(customer_conversation)
107
+ bot_conversation_sentiment_scores = sentiment.analyze_sentiment(bot_conversation)
108
+ return "Sentiment Scores for customer_conversation:\n"+customer_conversation_sentiment_scores+"\nSentiment Scores for sales_agent_conversation:\n"+bot_conversation_sentiment_scores
109
+
110
+ def emotion_analysis():
111
+ global convo_history,bot_conversation,customer_conversation
112
+ bot_conversation, customer_conversation = history_of_both(convo_history)
113
+ customer_emotion=sentiment.emotion_analysis(customer_conversation)
114
+ bot_emotion=sentiment.emotion_analysis(bot_conversation)
115
+ return "Emotions for customer_conversation:\n"+customer_emotion+"\nEmotions for sales_agent_conversation:\n"+bot_emotion
116
+
117
+ with gr.Blocks(theme="Taithrah/Minimal") as demo:
118
+ gr.HTML("""<center><h1>Sales Persona Chatbot</h1></center>""")
119
+
120
+ with gr.Row():
121
+ with gr.Column():
122
+ chatbot = gr.Chatbot()
123
+ with gr.Column():
124
+ show_stages = gr.Textbox(label="Stages",lines=18,container=False)
125
+ with gr.Row():
126
+ with gr.Column(scale=0.70):
127
+ msg = gr.Textbox(show_label=False,container=False)
128
+ with gr.Column(scale=0.30):
129
+ clear = gr.Button("Clear")
130
+ with gr.Row():
131
+ with gr.Column(scale=0.50):
132
+ with gr.Row():
133
+ gen_report_view = gr.Textbox(label="Generated Report",container=False)
134
+ with gr.Row():
135
+ gen_report_btn = gr.Button("Generate Report")
136
+ report_down_btn = gr.DownloadButton(label="Download Report",value="report.txt")
137
+ with gr.Column(scale=0.50):
138
+ with gr.Row():
139
+ summary_view = gr.Textbox(label="Summary",container=False)
140
+ with gr.Row():
141
+ summary_btn = gr.Button("Generate Summary")
142
+ with gr.Row():
143
+ with gr.Column(scale=0.50):
144
+ with gr.Row():
145
+ sentiment_view = gr.Textbox(label="Sentiment",container=False)
146
+ with gr.Row():
147
+ sentiment_btn = gr.Button("Sentiment")
148
+ with gr.Column(scale=0.50):
149
+ with gr.Row():
150
+ emotion_view = gr.Textbox(label="Emotion",container=False)
151
+ with gr.Row():
152
+ emotion_btn = gr.Button("Emotion")
153
+
154
+
155
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
156
+ bot, chatbot, chatbot
157
+ )
158
+ msg.submit(stages,[],show_stages)
159
+ gen_report_btn.click(download_report,[],gen_report_view,queue=False)
160
+ summary_btn.click(generate_convo_summary,[],summary_view)
161
+ sentiment_btn.click(sentiment_analysis,[],sentiment_view)
162
+ emotion_btn.click(emotion_analysis,[],emotion_view)
163
+ clear.click(lambda: None, None, chatbot, queue=False)
164
+ clear.click(lambda: None, None, show_stages, queue=False)
165
+
166
+ demo.queue()
167
+ demo.launch()