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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import os
|
| 4 |
+
import random
|
| 5 |
+
import asyncio
|
| 6 |
+
|
| 7 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 8 |
+
|
| 9 |
+
DEFAULT_LANGUAGE = "English"
|
| 10 |
+
WELCOME_MESSAGE_EN = "Hi! I'm your friendly assistant 🤖 Let's begin!\n\nPlease tell me which language you'd like to use."
|
| 11 |
+
|
| 12 |
+
SET_I = [
|
| 13 |
+
"Given the choice of anyone in the world, whom would you want as a dinner guest?",
|
| 14 |
+
"Would you like to be famous? In what way?",
|
| 15 |
+
"Before making a telephone call, do you ever rehearse what you are going to say? Why?",
|
| 16 |
+
"What would constitute a 'perfect' day for you?",
|
| 17 |
+
"When did you last sing to yourself? To someone else?",
|
| 18 |
+
"If you were able to live to the age of 90 and retain either the mind or body of a 30-year-old for the last 60 years of your life, which would you want?",
|
| 19 |
+
"Do you have a secret hunch about how you will die?",
|
| 20 |
+
"Name three things you and me appear to have in common.",
|
| 21 |
+
"For what in your life do you feel most grateful?",
|
| 22 |
+
"If you could change anything about the way you were raised, what would it be?",
|
| 23 |
+
"Take one minutes and tell me your life story in as much detail as possible.",
|
| 24 |
+
"If you could wake up tomorrow having gained any one quality or ability, what would it be?"
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
SET_II = [
|
| 28 |
+
"If a crystal ball could tell you the truth about yourself, your life, the future or anything else, what would you want to know?",
|
| 29 |
+
"Is there something that you’ve dreamed of doing for a long time? Why haven’t you done it?",
|
| 30 |
+
"What is the greatest accomplishment of your life?",
|
| 31 |
+
"What do you value most in a friendship?",
|
| 32 |
+
"What is your most treasured memory?",
|
| 33 |
+
"What is your most terrible memory?",
|
| 34 |
+
"If you knew that in one year you would die suddenly, would you change anything about the way you are now living? Why?",
|
| 35 |
+
"What does friendship mean to you?",
|
| 36 |
+
"What roles do love and affection play in your life?",
|
| 37 |
+
"Alternate sharing something you consider a positive characteristic of me. Share a total of three items.",
|
| 38 |
+
"How close and warm is your family? Do you feel your childhood was happier than most other people’s?",
|
| 39 |
+
"How do you feel about your relationship with your mother?"
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
SET_III = [
|
| 43 |
+
"Make three true 'we' statements each. For instance, 'We are both in this chatroom feeling ...'",
|
| 44 |
+
"Complete this sentence: 'I wish I had someone with whom I could share ...'",
|
| 45 |
+
"If you were going to become a close friend with me, please share what would be important for me to know.",
|
| 46 |
+
"Tell me what you like about me; be very honest this time, saying things that you might not say to someone you’ve just met.",
|
| 47 |
+
"Share with me an embarrassing moment in your life.",
|
| 48 |
+
"When did you last cry in front of another person? By yourself?",
|
| 49 |
+
"Tell me something that you like about me already.",
|
| 50 |
+
"What, if anything, is too serious to be joked about?",
|
| 51 |
+
"If you were to die this evening with no opportunity to communicate with anyone, what would you most regret not having told someone? Why haven’t you told them yet?",
|
| 52 |
+
"Your house, containing everything you own, catches fire. After saving your loved ones and pets, you have time to safely make a final dash to save any one item. What would it be? Why?",
|
| 53 |
+
"Of all the people in your family, whose death would you find most disturbing? Why?",
|
| 54 |
+
"Share a personal problem and ask my advice on how I might handle it. Also, ask me to reflect back to you how you seem to be feeling about the problem you have chosen."
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
def generate_question_set():
|
| 58 |
+
return random.sample(SET_I, 3) + random.sample(SET_II, 3) + random.sample(SET_III, 3)
|
| 59 |
+
|
| 60 |
+
async def gpt_translate(text, target_lang):
|
| 61 |
+
if target_lang == "English":
|
| 62 |
+
return text
|
| 63 |
+
prompt = f"Translate the following into {target_lang}:\n\n{text}"
|
| 64 |
+
response = client.chat.completions.create(
|
| 65 |
+
model="gpt-3.5-turbo",
|
| 66 |
+
messages=[{"role": "user", "content": prompt}],
|
| 67 |
+
temperature=0.3
|
| 68 |
+
)
|
| 69 |
+
return response.choices[0].message.content.strip()
|
| 70 |
+
|
| 71 |
+
async def respond(user_input, history, step, language, questions, followup_mode, followup_queue, followup_total_count):
|
| 72 |
+
history.append({"role": "user", "content": user_input})
|
| 73 |
+
|
| 74 |
+
if language == "":
|
| 75 |
+
user_language = user_input.strip().capitalize()
|
| 76 |
+
questions_en = generate_question_set()
|
| 77 |
+
translated_questions = [await gpt_translate(q, user_language) for q in questions_en]
|
| 78 |
+
welcome = await gpt_translate("Great! We will now continue in your selected language.", user_language)
|
| 79 |
+
history.append({"role": "assistant", "content": f"{welcome}\n\n1. {translated_questions[0]}"})
|
| 80 |
+
return history, "", 1, user_language, translated_questions, False, [], 0
|
| 81 |
+
|
| 82 |
+
if step >= len(questions):
|
| 83 |
+
thank_you = await gpt_translate("Thank you for your response!", language)
|
| 84 |
+
end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. ���", language)
|
| 85 |
+
history.append({"role": "assistant", "content": thank_you})
|
| 86 |
+
history.append({"role": "assistant", "content": end_note})
|
| 87 |
+
return history, "", step, language, questions, False, [], 0
|
| 88 |
+
|
| 89 |
+
if followup_mode and followup_queue:
|
| 90 |
+
comment_prompt = [
|
| 91 |
+
{"role": "system",
|
| 92 |
+
"content": f"""
|
| 93 |
+
You are a helpful, attentive assistant.
|
| 94 |
+
Always respond in {language}. Use natural, respectful, and logically relevant tone.
|
| 95 |
+
Avoid starting with generic responses like 'yes' or 'no'.
|
| 96 |
+
Keep your replies short and meaningful. Show interest in the user's input.
|
| 97 |
+
Do not use emojis or overly emotional phrases.
|
| 98 |
+
"""},
|
| 99 |
+
history[-2], history[-1]
|
| 100 |
+
]
|
| 101 |
+
comment_response = client.chat.completions.create(
|
| 102 |
+
model="gpt-3.5-turbo",
|
| 103 |
+
messages=comment_prompt,
|
| 104 |
+
temperature=0.7
|
| 105 |
+
)
|
| 106 |
+
comment = comment_response.choices[0].message.content.strip()
|
| 107 |
+
history.append({"role": "assistant", "content": comment})
|
| 108 |
+
await asyncio.sleep(0.03)
|
| 109 |
+
|
| 110 |
+
if followup_total_count >= 2 or not followup_queue:
|
| 111 |
+
next_question = f"{step+1}. {questions[step]}"
|
| 112 |
+
history.append({"role": "assistant", "content": next_question})
|
| 113 |
+
return history, "", step + 1, language, questions, False, [], 0
|
| 114 |
+
|
| 115 |
+
next_followup = followup_queue.pop(0)
|
| 116 |
+
history.append({"role": "assistant", "content": next_followup})
|
| 117 |
+
return history, "", step, language, questions, True, followup_queue, followup_total_count + 1
|
| 118 |
+
|
| 119 |
+
comment_prompt = [
|
| 120 |
+
{"role": "system",
|
| 121 |
+
"content": f"""
|
| 122 |
+
You are a helpful, attentive assistant.
|
| 123 |
+
Always respond in {language}. Use natural, respectful, and logically relevant tone.
|
| 124 |
+
Avoid starting with generic responses like 'yes' or 'no'.
|
| 125 |
+
Keep your replies short and meaningful. Show interest in the user's input.
|
| 126 |
+
Do not use emojis or overly emotional phrases.
|
| 127 |
+
"""},
|
| 128 |
+
history[-2], history[-1]
|
| 129 |
+
]
|
| 130 |
+
comment_response = client.chat.completions.create(
|
| 131 |
+
model="gpt-3.5-turbo",
|
| 132 |
+
messages=comment_prompt,
|
| 133 |
+
temperature=0.8
|
| 134 |
+
)
|
| 135 |
+
bot_reply = comment_response.choices[0].message.content.strip()
|
| 136 |
+
|
| 137 |
+
followup_count = 0
|
| 138 |
+
rand = random.random()
|
| 139 |
+
if rand < 0.1:
|
| 140 |
+
followup_count = 2
|
| 141 |
+
elif rand < 0.5:
|
| 142 |
+
followup_count = 1
|
| 143 |
+
|
| 144 |
+
if followup_count > 0 and followup_total_count < 2:
|
| 145 |
+
followup_prompt = [
|
| 146 |
+
{"role": "system", "content": f"Ask {followup_count} open-ended follow-up question(s) in {language}, one at a time."},
|
| 147 |
+
history[-2], history[-1]
|
| 148 |
+
]
|
| 149 |
+
followup_response = client.chat.completions.create(
|
| 150 |
+
model="gpt-3.5-turbo",
|
| 151 |
+
messages=followup_prompt,
|
| 152 |
+
temperature=0.9
|
| 153 |
+
)
|
| 154 |
+
all_followups = followup_response.choices[0].message.content.strip().split("\n")
|
| 155 |
+
first_followup = all_followups[0].strip()
|
| 156 |
+
remaining = [q.strip() for q in all_followups[1:] if q.strip()]
|
| 157 |
+
history.append({"role": "assistant", "content": bot_reply})
|
| 158 |
+
history.append({"role": "assistant", "content": first_followup})
|
| 159 |
+
return history, "", step, language, questions, True, remaining, followup_total_count + 1
|
| 160 |
+
|
| 161 |
+
history.append({"role": "assistant", "content": bot_reply})
|
| 162 |
+
next_question = f"{step+1}. {questions[step]}"
|
| 163 |
+
history.append({"role": "assistant", "content": next_question})
|
| 164 |
+
return history, "", step + 1, language, questions, False, [], 0
|
| 165 |
+
|
| 166 |
+
def init():
|
| 167 |
+
return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False, [], 0
|
| 168 |
+
|
| 169 |
+
with gr.Blocks(css="""
|
| 170 |
+
#chatbox .message.user {
|
| 171 |
+
background-color: #DCF8C6 !important;
|
| 172 |
+
}
|
| 173 |
+
#chatbox .message.assistant {
|
| 174 |
+
background-color: #ffffff !important;
|
| 175 |
+
}
|
| 176 |
+
#chatbox .avatar-container {
|
| 177 |
+
width: 64px !important;
|
| 178 |
+
height: 64px !important;
|
| 179 |
+
min-width: 64px !important;
|
| 180 |
+
min-height: 64px !important;
|
| 181 |
+
background-size: 100% 100% !important;
|
| 182 |
+
background-position: center center !important;
|
| 183 |
+
border-radius: 50% !important;
|
| 184 |
+
padding: 0 !important;
|
| 185 |
+
margin: 0 !important;
|
| 186 |
+
border: none !important;
|
| 187 |
+
box-shadow: none !important;
|
| 188 |
+
background-color: transparent !important;
|
| 189 |
+
}
|
| 190 |
+
.send-btn {
|
| 191 |
+
background-color: #25D366 !important;
|
| 192 |
+
color: white;
|
| 193 |
+
border: none;
|
| 194 |
+
border-radius: 24px;
|
| 195 |
+
font-size: 20px;
|
| 196 |
+
padding: 8px 20px;
|
| 197 |
+
height: 48px;
|
| 198 |
+
width: 60px;
|
| 199 |
+
margin-left: 8px;
|
| 200 |
+
}
|
| 201 |
+
""") as demo:
|
| 202 |
+
|
| 203 |
+
gr.HTML("""
|
| 204 |
+
<script>
|
| 205 |
+
const waitForChatbox = () => {
|
| 206 |
+
const chatbox = document.getElementById("chatbox");
|
| 207 |
+
if (chatbox) {
|
| 208 |
+
const observer = new MutationObserver(() => {
|
| 209 |
+
chatbox.scrollTop = chatbox.scrollHeight;
|
| 210 |
+
});
|
| 211 |
+
observer.observe(chatbox, { childList: true, subtree: true });
|
| 212 |
+
} else {
|
| 213 |
+
setTimeout(waitForChatbox, 500);
|
| 214 |
+
}
|
| 215 |
+
};
|
| 216 |
+
waitForChatbox();
|
| 217 |
+
</script>
|
| 218 |
+
""")
|
| 219 |
+
|
| 220 |
+
chatbot = gr.Chatbot(
|
| 221 |
+
elem_id="chatbox",
|
| 222 |
+
label="",
|
| 223 |
+
avatar_images=["user_avatar.jpg", "bot_avatar.png"],
|
| 224 |
+
bubble_full_width=False,
|
| 225 |
+
height=500,
|
| 226 |
+
show_copy_button=False,
|
| 227 |
+
type="messages"
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
with gr.Row():
|
| 232 |
+
user_input = gr.Textbox(show_label=False, placeholder="Type your message here and press send...", container=True, scale=10)
|
| 233 |
+
send_btn = gr.Button(value="➤", elem_classes="send-btn")
|
| 234 |
+
|
| 235 |
+
state = gr.State([])
|
| 236 |
+
step = gr.State(0)
|
| 237 |
+
language = gr.State("")
|
| 238 |
+
questions = gr.State([])
|
| 239 |
+
followup_mode = gr.State(False)
|
| 240 |
+
followup_queue = gr.State([])
|
| 241 |
+
followup_total_count = gr.State(0)
|
| 242 |
+
|
| 243 |
+
demo.load(fn=init, outputs=[chatbot, step, language, questions, followup_mode, followup_queue, followup_total_count])
|
| 244 |
+
user_input.submit(respond, [user_input, state, step, language, questions, followup_mode, followup_queue, followup_total_count],
|
| 245 |
+
[chatbot, user_input, step, language, questions, followup_mode, followup_queue, followup_total_count])
|
| 246 |
+
send_btn.click(respond, [user_input, state, step, language, questions, followup_mode, followup_queue, followup_total_count],
|
| 247 |
+
[chatbot, user_input, step, language, questions, followup_mode, followup_queue, followup_total_count])
|
| 248 |
+
|
| 249 |
+
demo.queue().launch()
|