Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,233 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
for
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
for
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
import os
|
| 5 |
+
import random
|
| 6 |
+
import asyncio
|
| 7 |
+
import urllib.parse
|
| 8 |
+
|
| 9 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 10 |
+
|
| 11 |
+
DEFAULT_LANGUAGE = "English"
|
| 12 |
+
WELCOME_MESSAGE_EN = "Hi! I'm your friendly assistant 🤖 Let's begin!\n\nPlease tell me which language you'd like to use."
|
| 13 |
+
|
| 14 |
+
# === Aron Question Bank ===
|
| 15 |
+
ARON_SET_I = [
|
| 16 |
+
"Given the choice of anyone in the world, whom would you want as a dinner guest?",
|
| 17 |
+
"Would you like to be famous? In what way?",
|
| 18 |
+
"Before making a telephone call, do you ever rehearse what you are going to say? Why?",
|
| 19 |
+
"What would constitute a 'perfect' day for you?",
|
| 20 |
+
"When did you last sing to yourself? To someone else?",
|
| 21 |
+
"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?",
|
| 22 |
+
"Do you have a secret hunch about how you will die?",
|
| 23 |
+
"Name three things you and me appear to have in common.",
|
| 24 |
+
"For what in your life do you feel most grateful?",
|
| 25 |
+
"If you could change anything about the way you were raised, what would it be?",
|
| 26 |
+
"Take one minute and tell me your life story in as much detail as possible.",
|
| 27 |
+
"If you could wake up tomorrow having gained any one quality or ability, what would it be?"
|
| 28 |
+
]
|
| 29 |
+
ARON_SET_II = [
|
| 30 |
+
"If a crystal ball could tell you the truth about yourself, your life, the future or anything else, what would you want to know?",
|
| 31 |
+
"Is there something that you\u2019ve dreamed of doing for a long time? Why haven\u2019t you done it?",
|
| 32 |
+
"What is the greatest accomplishment of your life?",
|
| 33 |
+
"What do you value most in a friendship?",
|
| 34 |
+
"What is your most treasured memory?",
|
| 35 |
+
"What is your most terrible memory?",
|
| 36 |
+
"If you knew that in one year you would die suddenly, would you change anything about the way you are now living? Why?",
|
| 37 |
+
"What does friendship mean to you?",
|
| 38 |
+
"What roles do love and affection play in your life?",
|
| 39 |
+
"Alternate sharing something you consider a positive characteristic of me. Share a total of three items.",
|
| 40 |
+
"How close and warm is your family? Do you feel your childhood was happier than most other people\u2019s?",
|
| 41 |
+
"How do you feel about your relationship with your mother?"
|
| 42 |
+
]
|
| 43 |
+
ARON_SET_III = [
|
| 44 |
+
"Make three true 'we' statements each. For instance, 'We are both in this chatroom feeling ...'",
|
| 45 |
+
"Complete this sentence: 'I wish I had someone with whom I could share ...'",
|
| 46 |
+
"If you were going to become a close friend with me, please share what would be important for me to know.",
|
| 47 |
+
"Tell me what you like about me; be very honest this time, saying things that you might not say to someone you\u2019ve just met.",
|
| 48 |
+
"Share with me an embarrassing moment in your life.",
|
| 49 |
+
"When did you last cry in front of another person? By yourself?",
|
| 50 |
+
"Tell me something that you like about me already.",
|
| 51 |
+
"What, if anything, is too serious to be joked about?",
|
| 52 |
+
"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\u2019t you told them yet?",
|
| 53 |
+
"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?",
|
| 54 |
+
"Of all the people in your family, whose death would you find most disturbing? Why?",
|
| 55 |
+
"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."
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
# === Normal Question Bank ===
|
| 59 |
+
NORMAL_SET_I = [
|
| 60 |
+
"When was the last time you walked for more than an hour? Describe where you went and what you saw.",
|
| 61 |
+
"What is the best gift you have ever received? Why?",
|
| 62 |
+
"If you had to leave California, where would you move to? What do you miss most about California?",
|
| 63 |
+
"How did you celebrate Halloween last year?",
|
| 64 |
+
"Do you read newspapers often? Which newspapers do you like? Why?",
|
| 65 |
+
"What is the ideal number of students to share a house with? Why?",
|
| 66 |
+
"If you could invent a new flavor of ice cream, what flavor would you create?",
|
| 67 |
+
"What is the best restaurant you have been to in the past month that your partner has not been to? Describe it to your partner.",
|
| 68 |
+
"Describe the pet you last got.",
|
| 69 |
+
"What is your favorite holiday? Why?",
|
| 70 |
+
"Tell your partner the funniest thing that happened when you were with a child."
|
| 71 |
+
]
|
| 72 |
+
NORMAL_SET_II = [
|
| 73 |
+
"What gift did you get on your last birthday?",
|
| 74 |
+
"Describe your last trip to the zoo.",
|
| 75 |
+
"Name and age of your family members, including grandparents, uncles and aunts, and where they were born (to the extent known).",
|
| 76 |
+
"One person says a word, and the next person says a word starting with the last letter of the previous word. Continue for 50 words. No need to form sentences.",
|
| 77 |
+
"Do you like to wake up early or stay up late? Has anything interesting ever happened to you because of this?",
|
| 78 |
+
"Where are you from? List all the places you have lived.",
|
| 79 |
+
"What is your favorite class at UC Santa Cruz? Why?",
|
| 80 |
+
"What did you do this summer?",
|
| 81 |
+
"What gifts did you receive last Christmas/Hanukkah?",
|
| 82 |
+
"Who is your favorite same-sex actor? Describe a great scene he or she starred in.",
|
| 83 |
+
"What was your impression of UC Santa Cruz when you first came to it?",
|
| 84 |
+
"What is the best TV show you have seen in the past month that your partner has not seen? Describe it to your partner.",
|
| 85 |
+
"What is your favorite holiday? Why?"
|
| 86 |
+
]
|
| 87 |
+
NORMAL_SET_III = [
|
| 88 |
+
"Where did you go to high school? What was it like?",
|
| 89 |
+
"What is the best book you have read in the past three months that your partner has not read? Describe it to your partner.",
|
| 90 |
+
"What country would you most like to visit? What attracts you to it?",
|
| 91 |
+
"Do you prefer digital or analog watches/clocks? Why?",
|
| 92 |
+
"Describe your mother's best friend.",
|
| 93 |
+
"What are the pros and cons of artificial Christmas trees?",
|
| 94 |
+
"How often do you get your hair cut? Where do you go? Have you ever had a bad haircut?",
|
| 95 |
+
"Did you have a class pet in elementary school? Do you remember its name?",
|
| 96 |
+
"Do you think left-handed people are more creative than right-handed people?",
|
| 97 |
+
"What was the last concert you attended? How many albums of the band do you have? Have you seen them perform before? Where?",
|
| 98 |
+
"What magazines do you subscribe to? What have you subscribed to in the past?",
|
| 99 |
+
"Have you ever participated in a school play? What role did you play? What was the plot? Did anything funny happen during the play?"
|
| 100 |
+
]
|
| 101 |
+
|
| 102 |
+
def generate_question_set(question_type):
|
| 103 |
+
if question_type == "aron":
|
| 104 |
+
return random.sample(ARON_SET_I, 3) + random.sample(ARON_SET_II, 3) + random.sample(ARON_SET_III, 3)
|
| 105 |
+
else:
|
| 106 |
+
return random.sample(NORMAL_SET_I, 3) + random.sample(NORMAL_SET_II, 3) + random.sample(NORMAL_SET_III, 3)
|
| 107 |
+
|
| 108 |
+
async def gpt_translate(text, target_lang):
|
| 109 |
+
if target_lang == "English":
|
| 110 |
+
return text
|
| 111 |
+
prompt = f"Translate the following into {target_lang}:\n\n{text}"
|
| 112 |
+
response = client.chat.completions.create(
|
| 113 |
+
model="gpt-3.5-turbo",
|
| 114 |
+
messages=[{"role": "user", "content": prompt}],
|
| 115 |
+
temperature=0.3
|
| 116 |
+
)
|
| 117 |
+
return response.choices[0].message.content.strip()
|
| 118 |
+
|
| 119 |
+
def get_group_from_url(request: gr.Request):
|
| 120 |
+
query = urllib.parse.parse_qs(request.query_string.decode())
|
| 121 |
+
group = query.get("group", [None])[0]
|
| 122 |
+
if group not in ["A", "B", "C", "D"]:
|
| 123 |
+
group = random.choice(["A", "B", "C", "D"])
|
| 124 |
+
humanlike = group in ["A", "B"]
|
| 125 |
+
question_type = "aron" if group in ["A", "C"] else "normal"
|
| 126 |
+
return group, humanlike, question_type
|
| 127 |
+
|
| 128 |
+
async def respond(user_input, history, step, language, questions, followup_mode, followup_total_count, group, humanlike, question_type):
|
| 129 |
+
history.append({"role": "user", "content": user_input})
|
| 130 |
+
|
| 131 |
+
if language == "":
|
| 132 |
+
user_language = user_input.strip()
|
| 133 |
+
questions_en = generate_question_set(question_type)
|
| 134 |
+
translated_questions = [await gpt_translate(q, user_language) for q in questions_en]
|
| 135 |
+
welcome = await gpt_translate("Great! We will now continue in your selected language.", user_language)
|
| 136 |
+
history.append({"role": "assistant", "content": f"{welcome}\n\n1. {translated_questions[0]}"} )
|
| 137 |
+
return history, "", 1, user_language, translated_questions, False, 0, group, humanlike, question_type
|
| 138 |
+
|
| 139 |
+
if step >= len(questions):
|
| 140 |
+
thank_you = await gpt_translate("Thank you for your response!", language)
|
| 141 |
+
end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. 📝", language)
|
| 142 |
+
history.append({"role": "assistant", "content": thank_you})
|
| 143 |
+
history.append({"role": "assistant", "content": end_note})
|
| 144 |
+
return history, "", step, language, questions, False, 0, group, humanlike, question_type
|
| 145 |
+
|
| 146 |
+
# 评论
|
| 147 |
+
comment_prompt = [
|
| 148 |
+
{"role": "system", "content": f"You are a helpful, attentive assistant. Always respond in {language}. Use natural, short, respectful replies."},
|
| 149 |
+
history[-2], history[-1]
|
| 150 |
+
]
|
| 151 |
+
comment_response = client.chat.completions.create(
|
| 152 |
+
model="gpt-3.5-turbo",
|
| 153 |
+
messages=comment_prompt,
|
| 154 |
+
temperature=0.7
|
| 155 |
+
)
|
| 156 |
+
comment = comment_response.choices[0].message.content.strip()
|
| 157 |
+
history.append({"role": "assistant", "content": comment})
|
| 158 |
+
await asyncio.sleep(0.02)
|
| 159 |
+
|
| 160 |
+
if followup_mode:
|
| 161 |
+
if followup_total_count == 1 and random.random() < 0.1:
|
| 162 |
+
# 第二次追问
|
| 163 |
+
followup_prompt = [
|
| 164 |
+
{"role": "system", "content": f"Ask 1 more open-ended follow-up question in {language}, unnumbered."},
|
| 165 |
+
history[-4], history[-3], history[-2], history[-1]
|
| 166 |
+
]
|
| 167 |
+
followup_response = client.chat.completions.create(
|
| 168 |
+
model="gpt-3.5-turbo",
|
| 169 |
+
messages=followup_prompt,
|
| 170 |
+
temperature=0.9
|
| 171 |
+
)
|
| 172 |
+
second_followup = followup_response.choices[0].message.content.strip()
|
| 173 |
+
history.append({"role": "assistant", "content": second_followup})
|
| 174 |
+
return history, "", step, language, questions, True, 2, group, humanlike, question_type
|
| 175 |
+
|
| 176 |
+
# 追问结束 → 进入下一主问题
|
| 177 |
+
next_question = f"{step+1}. {questions[step]}"
|
| 178 |
+
history.append({"role": "assistant", "content": next_question})
|
| 179 |
+
return history, "", step + 1, language, questions, False, 0, group, humanlike, question_type
|
| 180 |
+
|
| 181 |
+
if random.random() < 0.4:
|
| 182 |
+
# 第一次追问
|
| 183 |
+
followup_prompt = [
|
| 184 |
+
{"role": "system", "content": f"Ask 1 open-ended follow-up question in {language}, based on user's response."},
|
| 185 |
+
history[-2], history[-1]
|
| 186 |
+
]
|
| 187 |
+
followup_response = client.chat.completions.create(
|
| 188 |
+
model="gpt-3.5-turbo",
|
| 189 |
+
messages=followup_prompt,
|
| 190 |
+
temperature=0.9
|
| 191 |
+
)
|
| 192 |
+
first_followup = followup_response.choices[0].message.content.strip()
|
| 193 |
+
history.append({"role": "assistant", "content": first_followup})
|
| 194 |
+
return history, "", step, language, questions, True, 1, group, humanlike, question_type
|
| 195 |
+
|
| 196 |
+
# 没有追问,直接提问
|
| 197 |
+
next_question = f"{step+1}. {questions[step]}"
|
| 198 |
+
history.append({"role": "assistant", "content": next_question})
|
| 199 |
+
return history, "", step + 1, language, questions, False, 0, group, humanlike, question_type
|
| 200 |
+
|
| 201 |
+
def init():
|
| 202 |
+
return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False, 0
|
| 203 |
+
|
| 204 |
+
with gr.Blocks() as demo:
|
| 205 |
+
chatbot = gr.Chatbot(
|
| 206 |
+
label="", elem_id="chatbox",
|
| 207 |
+
avatar_images=["user_avatar.jpg", "humanlike_avatar.png"],
|
| 208 |
+
bubble_full_width=False,
|
| 209 |
+
height=500, show_copy_button=False, type="messages"
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
with gr.Row():
|
| 213 |
+
user_input = gr.Textbox(show_label=False, placeholder="Type your message here...", container=True, scale=10)
|
| 214 |
+
send_btn = gr.Button(value="➤")
|
| 215 |
+
|
| 216 |
+
state = gr.State([])
|
| 217 |
+
step = gr.State(0)
|
| 218 |
+
language = gr.State("")
|
| 219 |
+
questions = gr.State([])
|
| 220 |
+
followup_mode = gr.State(False)
|
| 221 |
+
followup_total_count = gr.State(0)
|
| 222 |
+
group = gr.State()
|
| 223 |
+
humanlike = gr.State()
|
| 224 |
+
question_type = gr.State()
|
| 225 |
+
|
| 226 |
+
demo.load(get_group_from_url, None, [group, humanlike, question_type], queue=False)
|
| 227 |
+
demo.load(init, None, [state, step, language, questions, followup_mode, followup_total_count])
|
| 228 |
+
user_input.submit(respond, [user_input, state, step, language, questions, followup_mode, followup_total_count, group, humanlike, question_type],
|
| 229 |
+
[chatbot, user_input, step, language, questions, followup_mode, followup_total_count, group, humanlike, question_type])
|
| 230 |
+
send_btn.click(respond, [user_input, state, step, language, questions, followup_mode, followup_total_count, group, humanlike, question_type],
|
| 231 |
+
[chatbot, user_input, step, language, questions, followup_mode, followup_total_count, group, humanlike, question_type])
|
| 232 |
+
|
| 233 |
+
demo.queue().launch()
|