Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import random | |
| import time | |
| import os | |
| from sales_helper import SalesGPT | |
| from analyzer import Summarizer,SentimentAnalyzer | |
| 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"{os.getcwd()}/report.txt" | |
| print('CWD : ',os.getcwd()) | |
| with open(report_file_path,"w") as file: | |
| file.write(message) | |
| return message | |
| def bot(history): | |
| bot_message = sales_agent._call({}) | |
| history[-1][1] = bot_message | |
| return 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 convo_history: | |
| 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 | |
| def clear_stages(): | |
| global stage | |
| stage = "" | |
| sales_agent.conversation_history = [] | |
| return "" | |
| 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=f"{os.getcwd()}/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) | |
| clear.click(clear_stages,[],show_stages) | |
| demo.queue() | |
| demo.launch() | |