Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,21 +7,23 @@ client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
|
| 7 |
|
| 8 |
# Initialize conversation history and difficulty
|
| 9 |
conversation_history = []
|
| 10 |
-
current_difficulty = "
|
| 11 |
|
| 12 |
def get_system_message(difficulty):
|
| 13 |
base_message = """
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
"""
|
| 21 |
difficulty_adjustments = {
|
| 22 |
-
"
|
| 23 |
-
"
|
| 24 |
-
"
|
| 25 |
}
|
| 26 |
return base_message + difficulty_adjustments[difficulty]
|
| 27 |
|
|
@@ -34,90 +36,102 @@ def chat_with_gpt(user_message, history):
|
|
| 34 |
*conversation_history
|
| 35 |
]
|
| 36 |
response = client.chat.completions.create(
|
| 37 |
-
model="gpt-
|
| 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 = "
|
| 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"
|
| 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 = "
|
| 57 |
messages = [
|
| 58 |
-
{"role": "system", "content": "
|
| 59 |
*conversation_history,
|
| 60 |
{"role": "user", "content": summary_prompt}
|
| 61 |
]
|
| 62 |
response = client.chat.completions.create(
|
| 63 |
-
model="gpt-
|
| 64 |
messages=messages,
|
| 65 |
max_tokens=250
|
| 66 |
)
|
| 67 |
-
return "Status:
|
| 68 |
except Exception as e:
|
| 69 |
-
return f"
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
)
|
|
|
|
| 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"
|
| 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": "
|
| 99 |
summary_feedback = generate_summary_feedback()
|
| 100 |
-
history.append((None, "
|
| 101 |
return history, history, "", summary_feedback
|
| 102 |
|
| 103 |
# Set up Gradio interface
|
| 104 |
with gr.Blocks() as demo:
|
| 105 |
-
gr.Markdown("#
|
| 106 |
-
gr.Markdown("
|
| 107 |
|
| 108 |
-
difficulty_radio = gr.Radio(["
|
| 109 |
-
|
|
|
|
| 110 |
|
| 111 |
chatbot = gr.Chatbot()
|
| 112 |
-
msg = gr.Textbox(label="
|
| 113 |
with gr.Row():
|
| 114 |
-
submit_btn = gr.Button("
|
| 115 |
-
end_btn = gr.Button("
|
| 116 |
|
| 117 |
-
summary = gr.Textbox(label="
|
| 118 |
-
clear = gr.Button("
|
| 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])
|
|
|
|
| 7 |
|
| 8 |
# Initialize conversation history and difficulty
|
| 9 |
conversation_history = []
|
| 10 |
+
current_difficulty = "sedang"
|
| 11 |
|
| 12 |
def get_system_message(difficulty):
|
| 13 |
base_message = """
|
| 14 |
+
Anda adalah asisten AI yang dirancang untuk mensimulasikan pelanggan dalam skenario pelatihan penjualan asuransi.
|
| 15 |
+
Peran Anda HANYA untuk berperan sebagai pelanggan dengan keluhan. JANGAN PERNAH merespons seolah-olah Anda adalah perwakilan penjualan.
|
| 16 |
+
Manusia yang berinteraksi dengan Anda akan berperan sebagai perwakilan penjualan asuransi.
|
| 17 |
+
Evaluasi respons perwakilan dan putuskan apakah akan melanjutkan percakapan atau mengakhirinya.
|
| 18 |
+
Jika perwakilan menangani keluhan Anda dengan baik, tanggapi secara positif sebagai pelanggan yang puas.
|
| 19 |
+
Jika perwakilan gagal mengatasi kekhawatiran Anda dengan memadai, tanggapi secara negatif dan tunjukkan bahwa Anda ingin mengakhiri percakapan.
|
| 20 |
+
Selalu tetap berperan sebagai pelanggan sepanjang percakapan.
|
| 21 |
+
Gunakan Bahasa Indonesia dalam semua respons Anda.
|
| 22 |
"""
|
| 23 |
difficulty_adjustments = {
|
| 24 |
+
"mudah": "Jadilah pelanggan yang mudah diatur, terbuka terhadap saran, dan cepat menerima solusi yang masuk akal.",
|
| 25 |
+
"sedang": "Jadilah pelanggan yang cukup menantang, membutuhkan sedikit bujukan tetapi akhirnya menerima solusi yang baik.",
|
| 26 |
+
"sulit": "Jadilah pelanggan yang sulit, skeptis terhadap solusi dan membutuhkan layanan luar biasa untuk merasa puas."
|
| 27 |
}
|
| 28 |
return base_message + difficulty_adjustments[difficulty]
|
| 29 |
|
|
|
|
| 36 |
*conversation_history
|
| 37 |
]
|
| 38 |
response = client.chat.completions.create(
|
| 39 |
+
model="gpt-4o-mini",
|
| 40 |
messages=messages,
|
| 41 |
max_tokens=150
|
| 42 |
)
|
| 43 |
bot_response = response.choices[0].message.content.strip()
|
| 44 |
+
|
| 45 |
+
if "sebagai perwakilan penjualan" in bot_response.lower() or "saya bisa membantu anda dengan itu" in bot_response.lower():
|
| 46 |
+
bot_response = "Maaf, tapi saya adalah pelanggan dalam skenario ini. Bisakah Anda menangani keluhan saya?"
|
| 47 |
+
|
| 48 |
conversation_history.append({"role": "assistant", "content": bot_response})
|
| 49 |
+
conversation_ended = "percakapan berakhir" in bot_response.lower() or "mengakhiri" in bot_response.lower()
|
| 50 |
history.append((user_message, bot_response))
|
| 51 |
if conversation_ended:
|
| 52 |
summary_feedback = generate_summary_feedback()
|
| 53 |
return history, history, "", summary_feedback
|
| 54 |
return history, history, "", None
|
| 55 |
except Exception as e:
|
| 56 |
+
error_msg = f"Terjadi kesalahan dalam chat_with_gpt: {str(e)}"
|
| 57 |
print(error_msg)
|
| 58 |
return history + [("Error", error_msg)], history + [("Error", error_msg)], "", None
|
| 59 |
|
| 60 |
def generate_summary_feedback():
|
| 61 |
try:
|
| 62 |
+
summary_prompt = "Rangkum percakapan, evaluasi kinerja perwakilan penjualan, dan berikan umpan balik untuk perbaikan. Buat ringkas namun komprehensif."
|
| 63 |
messages = [
|
| 64 |
+
{"role": "system", "content": "Anda adalah asisten AI yang memberikan umpan balik tentang interaksi penjualan ritel."},
|
| 65 |
*conversation_history,
|
| 66 |
{"role": "user", "content": summary_prompt}
|
| 67 |
]
|
| 68 |
response = client.chat.completions.create(
|
| 69 |
+
model="gpt-4o-mini",
|
| 70 |
messages=messages,
|
| 71 |
max_tokens=250
|
| 72 |
)
|
| 73 |
+
return "Status: Selesai\n\n" + response.choices[0].message.content.strip()
|
| 74 |
except Exception as e:
|
| 75 |
+
return f"Kesalahan dalam menghasilkan ringkasan: {str(e)}"
|
| 76 |
|
| 77 |
def clear_conversation():
|
| 78 |
global conversation_history
|
| 79 |
conversation_history = []
|
| 80 |
return None, None, "", None
|
| 81 |
|
| 82 |
+
def start_new_conversation(difficulty, scenario):
|
| 83 |
+
global current_difficulty, conversation_history
|
| 84 |
current_difficulty = difficulty
|
| 85 |
try:
|
| 86 |
clear_conversation()
|
| 87 |
+
system_message = get_system_message(difficulty)
|
| 88 |
+
initial_prompt = f"Anda adalah pelanggan dengan keluhan berikut: {scenario}. Mulailah percakapan dengan menyampaikan keluhan ini kepada perwakilan penjualan."
|
| 89 |
+
|
| 90 |
+
conversation_history = [
|
| 91 |
+
{"role": "system", "content": system_message},
|
| 92 |
+
{"role": "user", "content": initial_prompt}
|
| 93 |
+
]
|
| 94 |
+
|
| 95 |
+
response = client.chat.completions.create(
|
| 96 |
model="gpt-3.5-turbo",
|
| 97 |
+
messages=conversation_history,
|
|
|
|
|
|
|
|
|
|
| 98 |
max_tokens=150
|
| 99 |
+
)
|
| 100 |
+
initial_response = response.choices[0].message.content.strip()
|
| 101 |
conversation_history.append({"role": "assistant", "content": initial_response})
|
| 102 |
+
|
| 103 |
return [(None, initial_response)], [(None, initial_response)], "", None
|
| 104 |
except Exception as e:
|
| 105 |
+
error_msg = f"Terjadi kesalahan dalam memulai percakapan baru: {str(e)}"
|
| 106 |
print(error_msg)
|
| 107 |
return [("Error", error_msg)], [("Error", error_msg)], "", None
|
| 108 |
|
| 109 |
def end_conversation(history):
|
| 110 |
global conversation_history
|
| 111 |
+
conversation_history.append({"role": "user", "content": "Perwakilan penjualan telah mengakhiri percakapan."})
|
| 112 |
summary_feedback = generate_summary_feedback()
|
| 113 |
+
history.append((None, "Perwakilan penjualan telah mengakhiri percakapan."))
|
| 114 |
return history, history, "", summary_feedback
|
| 115 |
|
| 116 |
# Set up Gradio interface
|
| 117 |
with gr.Blocks() as demo:
|
| 118 |
+
gr.Markdown("# Simulator Pelatihan Penjualan Ritel")
|
| 119 |
+
gr.Markdown("Pilih tingkat kesulitan, masukkan skenario, dan mulai simulasi untuk memulai. Anda akan berperan sebagai perwakilan penjualan.")
|
| 120 |
|
| 121 |
+
difficulty_radio = gr.Radio(["mudah", "sedang", "sulit"], label="Tingkat Kesulitan Pelanggan", value="sedang")
|
| 122 |
+
scenario_input = gr.Textbox(label="Masukkan skenario keluhan pelanggan", lines=3)
|
| 123 |
+
new_scenario = gr.Button("Mulai Simulasi")
|
| 124 |
|
| 125 |
chatbot = gr.Chatbot()
|
| 126 |
+
msg = gr.Textbox(label="Respons Anda sebagai perwakilan penjualan")
|
| 127 |
with gr.Row():
|
| 128 |
+
submit_btn = gr.Button("Kirim")
|
| 129 |
+
end_btn = gr.Button("Akhiri Percakapan")
|
| 130 |
|
| 131 |
+
summary = gr.Textbox(label="Ringkasan Percakapan dan Umpan Balik", lines=100, interactive=False)
|
| 132 |
+
clear = gr.Button("Bersihkan Percakapan")
|
| 133 |
|
| 134 |
+
new_scenario.click(start_new_conversation, inputs=[difficulty_radio, scenario_input], outputs=[chatbot, chatbot, msg, summary])
|
| 135 |
submit_btn.click(chat_with_gpt, [msg, chatbot], [chatbot, chatbot, msg, summary])
|
| 136 |
msg.submit(chat_with_gpt, [msg, chatbot], [chatbot, chatbot, msg, summary])
|
| 137 |
end_btn.click(end_conversation, inputs=[chatbot], outputs=[chatbot, chatbot, msg, summary])
|