File size: 6,834 Bytes
2a3cc9a
0441352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import gradio as gr
import random
import time
import os
from sales_helper import SalesGPT
from langchain_openai import AzureChatOpenAI
from openai import AzureOpenAI
llm = AzureChatOpenAI(temperature=0,deployment_name="GPT-3")
from time import sleep
sales_agent = SalesGPT.from_llm(llm, verbose=False)
# init sales agent
sales_agent.seed_agent()
stage = "\n"
bot_conversation = ""
customer_conversation = ""
convo_history = sales_agent.conversation_history
client = AzureOpenAI()
def user(user_message, history):
    if user_message:
        sales_agent.human_step(user_message)
    return "", history + [[user_message, None]]

def stages():
  global stage
  stage += "\n\n"+sales_agent.determine_conversation_stage()
  return stage

def download_report():
  global convo_history
  sales_evaluation_criteria = {
      "understanding": "Does the salesperson understand the customer pain points and challenges?",
      "opening_effectiveness": "Was the opening of the pitch effective?",
      "focus_on_benefits": "Was there sufficient focus and emphasis on customer benefits of the products/features pitched?",
      "trust_building": "Did the salesperson establish trust and credibility by sharing testimonials, case studies, references, success stories of other satisfied customers?",
      "urgency_creation": "Did the salesperson create urgency, such as through time-sensitive offers or consequences of not taking the decision?",
      "objection_handling": "Did the salesperson handle objections well and proactively address/prepared for the objections?",
      "engagement": "Was the conversation engaging?",
      "balance_of_talk_and_listen": "Was there a balance between the salesperson talking and listening to the customer?",
      "closing_strategy": "Was there a clear call to action, summarization, and reiteration of the value proposition in the close strategy?",
      "purposefulness": "Throughout the pitch/conversation, was the conversation purposeful, and did it end with clear next steps?"
  }
  client = AzureOpenAI()

  conversation = [
    {"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."},
    {"role": "user", "content": f""" this is the Context:{convo_history}.
      """}
      ]
  response = client.chat.completions.create(
      model="GPT-3",
      messages=conversation,
      temperature=0,
      max_tokens=1000
  )

  message = response.choices[0].message.content
  report_file_path = f"report.txt"
  with open(report_file_path,"w") as file:
      file.write(message)  

  return message

def bot(history):
    bot_message = sales_agent._call({})
    history[-1][1] = ""
    for character in bot_message:
        history[-1][1] += character
        time.sleep(0.05)
        yield history
summarizer = Summarizer()
sentiment = SentimentAnalyzer()

def history_of_both(convo_history):
    # Initialize lists to store messages from customer and bot
    customer_messages = []
    bot_messages = []

    # Iterate through the input list
    for message in input_list:
        if message.endswith('<END_OF_TURN>'):
            # Customer message
            if len(customer_messages) == len(bot_messages):
                customer_messages.append(message[:-13])
            else:
                bot_messages.append(message[:-13])
        else:
            # Bot message
            if len(customer_messages) == len(bot_messages):
                bot_messages.append(message)
            else:
                customer_messages.append(message)

    bot_conversation = " ".join(bot_messages)
    customer_conversation = " ".join(customer_messages)

    return bot_conversation, customer_conversation

def generate_convo_summary():
    global convo_history
    summary=summarizer.generate_summary(convo_history)
    return summary

def sentiment_analysis():
    global convo_history
    bot_conversation, customer_conversation = history_of_both(convo_history)
    customer_conversation_sentiment_scores = sentiment.analyze_sentiment(customer_conversation)
    bot_conversation_sentiment_scores = sentiment.analyze_sentiment(bot_conversation)
    return "Sentiment Scores for customer_conversation:\n"+customer_conversation_sentiment_scores+"\nSentiment Scores for sales_agent_conversation:\n"+bot_conversation_sentiment_scores

def emotion_analysis():
    global convo_history,bot_conversation,customer_conversation
    bot_conversation, customer_conversation = history_of_both(convo_history)
    customer_emotion=sentiment.emotion_analysis(customer_conversation)
    bot_emotion=sentiment.emotion_analysis(bot_conversation)
    return "Emotions for customer_conversation:\n"+customer_emotion+"\nEmotions for sales_agent_conversation:\n"+bot_emotion

with gr.Blocks(theme="Taithrah/Minimal") as demo:
    gr.HTML("""<center><h1>Sales Persona Chatbot</h1></center>""")
    
    with gr.Row():
        with gr.Column():
            chatbot = gr.Chatbot()
        with gr.Column():
            show_stages = gr.Textbox(label="Stages",lines=18,container=False)
    with gr.Row():
      with gr.Column(scale=0.70):
        msg = gr.Textbox(show_label=False,container=False)
      with gr.Column(scale=0.30):
        clear = gr.Button("Clear")
    with gr.Row():
      with gr.Column(scale=0.50):
        with gr.Row():
          gen_report_view = gr.Textbox(label="Generated Report",container=False)
        with gr.Row():
          gen_report_btn = gr.Button("Generate Report")
          report_down_btn = gr.DownloadButton(label="Download Report",value="report.txt")
      with gr.Column(scale=0.50):
        with gr.Row():
          summary_view = gr.Textbox(label="Summary",container=False)
        with gr.Row():
          summary_btn = gr.Button("Generate Summary")
    with gr.Row():
      with gr.Column(scale=0.50):
        with gr.Row():
          sentiment_view = gr.Textbox(label="Sentiment",container=False)
        with gr.Row():
          sentiment_btn = gr.Button("Sentiment")
      with gr.Column(scale=0.50):
        with gr.Row():
          emotion_view = gr.Textbox(label="Emotion",container=False)
        with gr.Row():
          emotion_btn = gr.Button("Emotion")


    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, chatbot, chatbot
    )
    msg.submit(stages,[],show_stages)
    gen_report_btn.click(download_report,[],gen_report_view,queue=False)
    summary_btn.click(generate_convo_summary,[],summary_view)
    sentiment_btn.click(sentiment_analysis,[],sentiment_view)
    emotion_btn.click(emotion_analysis,[],emotion_view)
    clear.click(lambda: None, None, chatbot, queue=False)
    clear.click(lambda: None, None, show_stages, queue=False)
    
demo.queue()
demo.launch()