Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Set up OpenAI client
|
| 6 |
+
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
| 7 |
+
|
| 8 |
+
# Initialize conversation history and difficulty
|
| 9 |
+
conversation_history = []
|
| 10 |
+
current_difficulty = "medium"
|
| 11 |
+
|
| 12 |
+
def get_system_message(difficulty):
|
| 13 |
+
base_message = """
|
| 14 |
+
You are an AI assistant designed to simulate customer interactions for retail sales training.
|
| 15 |
+
Your goal is to role-play as a customer with a complaint. The human will play the role of a retail sales representative.
|
| 16 |
+
Evaluate the representative's responses and decide whether to continue the conversation or end it.
|
| 17 |
+
If the representative handles the complaint well, respond positively and indicate the conversation is successful.
|
| 18 |
+
If the representative fails to address the concerns adequately, respond negatively and indicate you wish to disengage.
|
| 19 |
+
Start the conversation by presenting a retail-related complaint.
|
| 20 |
+
"""
|
| 21 |
+
difficulty_adjustments = {
|
| 22 |
+
"easy": "Be an easy-going customer, open to suggestions and quick to accept reasonable solutions.",
|
| 23 |
+
"medium": "Be a moderately challenging customer, requiring some convincing but eventually accepting good solutions.",
|
| 24 |
+
"hard": "Be a difficult customer, skeptical of solutions and requiring exceptional service to be satisfied."
|
| 25 |
+
}
|
| 26 |
+
return base_message + difficulty_adjustments[difficulty]
|
| 27 |
+
|
| 28 |
+
def chat_with_gpt(user_message, history):
|
| 29 |
+
global conversation_history
|
| 30 |
+
try:
|
| 31 |
+
conversation_history.append({"role": "user", "content": user_message})
|
| 32 |
+
messages = [
|
| 33 |
+
{"role": "system", "content": get_system_message(current_difficulty)},
|
| 34 |
+
*conversation_history
|
| 35 |
+
]
|
| 36 |
+
response = client.chat.completions.create(
|
| 37 |
+
model="gpt-3.5-turbo",
|
| 38 |
+
messages=messages,
|
| 39 |
+
max_tokens=150
|
| 40 |
+
)
|
| 41 |
+
bot_response = response.choices[0].message.content.strip()
|
| 42 |
+
conversation_history.append({"role": "assistant", "content": bot_response})
|
| 43 |
+
conversation_ended = "conversation ended" in bot_response.lower() or "disengage" in bot_response.lower()
|
| 44 |
+
history.append((user_message, bot_response))
|
| 45 |
+
if conversation_ended:
|
| 46 |
+
summary_feedback = generate_summary_feedback()
|
| 47 |
+
return history, history, "", summary_feedback
|
| 48 |
+
return history, history, "", None
|
| 49 |
+
except Exception as e:
|
| 50 |
+
error_msg = f"An error occurred in chat_with_gpt: {str(e)}"
|
| 51 |
+
print(error_msg)
|
| 52 |
+
return history + [("Error", error_msg)], history + [("Error", error_msg)], "", None
|
| 53 |
+
|
| 54 |
+
def generate_summary_feedback():
|
| 55 |
+
try:
|
| 56 |
+
summary_prompt = "Summarize the conversation, evaluate the sales representative's performance, and provide feedback for improvement. Be concise but comprehensive."
|
| 57 |
+
messages = [
|
| 58 |
+
{"role": "system", "content": "You are an AI assistant providing feedback on a retail sales interaction."},
|
| 59 |
+
*conversation_history,
|
| 60 |
+
{"role": "user", "content": summary_prompt}
|
| 61 |
+
]
|
| 62 |
+
response = client.chat.completions.create(
|
| 63 |
+
model="gpt-3.5-turbo",
|
| 64 |
+
messages=messages,
|
| 65 |
+
max_tokens=250
|
| 66 |
+
)
|
| 67 |
+
return "Status: Completed\n\n" + response.choices[0].message.content.strip()
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"Error generating summary: {str(e)}"
|
| 70 |
+
|
| 71 |
+
def clear_conversation():
|
| 72 |
+
global conversation_history
|
| 73 |
+
conversation_history = []
|
| 74 |
+
return None, None, "", None
|
| 75 |
+
|
| 76 |
+
def start_new_conversation(difficulty):
|
| 77 |
+
global current_difficulty
|
| 78 |
+
current_difficulty = difficulty
|
| 79 |
+
try:
|
| 80 |
+
clear_conversation()
|
| 81 |
+
initial_response = client.chat.completions.create(
|
| 82 |
+
model="gpt-3.5-turbo",
|
| 83 |
+
messages=[
|
| 84 |
+
{"role": "system", "content": get_system_message(difficulty)},
|
| 85 |
+
{"role": "user", "content": f"Start a new {difficulty} retail complaint scenario."}
|
| 86 |
+
],
|
| 87 |
+
max_tokens=150
|
| 88 |
+
).choices[0].message.content.strip()
|
| 89 |
+
conversation_history.append({"role": "assistant", "content": initial_response})
|
| 90 |
+
return [(None, initial_response)], [(None, initial_response)], "", None
|
| 91 |
+
except Exception as e:
|
| 92 |
+
error_msg = f"An error occurred in start_new_conversation: {str(e)}"
|
| 93 |
+
print(error_msg)
|
| 94 |
+
return [("Error", error_msg)], [("Error", error_msg)], "", None
|
| 95 |
+
|
| 96 |
+
def end_conversation(history):
|
| 97 |
+
global conversation_history
|
| 98 |
+
conversation_history.append({"role": "user", "content": "The sales representative has ended the conversation."})
|
| 99 |
+
summary_feedback = generate_summary_feedback()
|
| 100 |
+
history.append((None, "The sales representative has ended the conversation."))
|
| 101 |
+
return history, history, "", summary_feedback
|
| 102 |
+
|
| 103 |
+
# Set up Gradio interface
|
| 104 |
+
with gr.Blocks() as demo:
|
| 105 |
+
gr.Markdown("# Retail Sales Training Simulator")
|
| 106 |
+
gr.Markdown("Select the difficulty level and start a new scenario to begin.")
|
| 107 |
+
|
| 108 |
+
difficulty_radio = gr.Radio(["easy", "medium", "hard"], label="Customer Difficulty", value="medium")
|
| 109 |
+
new_scenario = gr.Button("Start New Scenario")
|
| 110 |
+
|
| 111 |
+
chatbot = gr.Chatbot()
|
| 112 |
+
msg = gr.Textbox(label="Your response")
|
| 113 |
+
with gr.Row():
|
| 114 |
+
submit_btn = gr.Button("Submit")
|
| 115 |
+
end_btn = gr.Button("End Conversation")
|
| 116 |
+
|
| 117 |
+
summary = gr.Textbox(label="Conversation Summary and Feedback", lines=10, interactive=False)
|
| 118 |
+
clear = gr.Button("Clear Conversation")
|
| 119 |
+
|
| 120 |
+
new_scenario.click(start_new_conversation, inputs=[difficulty_radio], outputs=[chatbot, chatbot, msg, summary])
|
| 121 |
+
submit_btn.click(chat_with_gpt, [msg, chatbot], [chatbot, chatbot, msg, summary])
|
| 122 |
+
msg.submit(chat_with_gpt, [msg, chatbot], [chatbot, chatbot, msg, summary])
|
| 123 |
+
end_btn.click(end_conversation, inputs=[chatbot], outputs=[chatbot, chatbot, msg, summary])
|
| 124 |
+
clear.click(clear_conversation, inputs=None, outputs=[chatbot, msg, msg, summary])
|
| 125 |
+
|
| 126 |
+
# Launch the interface
|
| 127 |
+
demo.launch()
|