Spaces:
Sleeping
Sleeping
Virtual Consumer Persona
Browse files
app.py
CHANGED
|
@@ -5,29 +5,30 @@ from openai import OpenAI
|
|
| 5 |
# Initialize OpenAI client (expects OPENAI_API_KEY in env)
|
| 6 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 7 |
|
| 8 |
-
def make_system_message(system_message,
|
| 9 |
msg = (
|
| 10 |
f"{system_message}\n\n"
|
| 11 |
-
f"
|
| 12 |
-
f"
|
| 13 |
-
f"
|
| 14 |
-
f"
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
-
if
|
| 18 |
-
msg += "
|
| 19 |
return msg
|
| 20 |
|
| 21 |
def stream_chat(
|
| 22 |
message,
|
| 23 |
history, # list[list[str, str]] from gr.Chatbot
|
| 24 |
system_message,
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
ask_sugg,
|
| 31 |
max_tokens,
|
| 32 |
temp,
|
| 33 |
top_p,
|
|
@@ -36,7 +37,7 @@ def stream_chat(
|
|
| 36 |
Streaming generator that yields the progressively updated chat history.
|
| 37 |
"""
|
| 38 |
# 1) Build system + conversation messages
|
| 39 |
-
sys_msg = make_system_message(system_message,
|
| 40 |
|
| 41 |
messages = [{"role": "system", "content": sys_msg}]
|
| 42 |
for user_msg, assistant_msg in (history or []):
|
|
@@ -73,58 +74,57 @@ def stream_chat(
|
|
| 73 |
yield running_history
|
| 74 |
|
| 75 |
|
| 76 |
-
with gr.Blocks(title="
|
| 77 |
gr.Markdown("""
|
| 78 |
-
#
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
""")
|
| 82 |
|
| 83 |
-
chatbot = gr.Chatbot(height=
|
| 84 |
|
| 85 |
with gr.Column():
|
| 86 |
instructions = gr.Textbox(
|
| 87 |
value=(
|
| 88 |
-
"You are a
|
| 89 |
-
"You are trying to help a user customize their resume according to a specific role, employer organization and job Ad "
|
| 90 |
-
"- based on user input. Include tips if some items are missing."
|
| 91 |
),
|
| 92 |
-
label="Instructions to Bot",
|
| 93 |
-
lines=
|
| 94 |
)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
|
| 102 |
with gr.Row():
|
| 103 |
-
max_tokens = gr.Slider(minimum=1, maximum=
|
| 104 |
-
temp = gr.Slider(minimum=0.0, maximum=2.0, value=0.
|
| 105 |
top_p = gr.Slider(minimum=0.0, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus)")
|
| 106 |
|
| 107 |
-
msg = gr.Textbox(label="Type your
|
| 108 |
with gr.Row():
|
| 109 |
-
send = gr.Button("
|
| 110 |
-
clear = gr.Button("Clear")
|
| 111 |
|
| 112 |
# Wire up sending (submit or button) → stream_chat → chatbot
|
| 113 |
-
inputs = [msg, chatbot, instructions,
|
| 114 |
outputs = [chatbot]
|
| 115 |
|
| 116 |
msg.submit(stream_chat, inputs, outputs)
|
| 117 |
send.click(stream_chat, inputs, outputs)
|
| 118 |
|
| 119 |
-
# Clear everything
|
| 120 |
def do_clear():
|
| 121 |
-
return [], ""
|
| 122 |
|
| 123 |
clear.click(
|
| 124 |
do_clear,
|
| 125 |
inputs=[],
|
| 126 |
-
outputs=[chatbot, msg
|
| 127 |
)
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
-
demo.launch()
|
|
|
|
| 5 |
# Initialize OpenAI client (expects OPENAI_API_KEY in env)
|
| 6 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 7 |
|
| 8 |
+
def make_system_message(system_message, demographics, occupation, psychographics, buying_habits, critical_mode):
|
| 9 |
msg = (
|
| 10 |
f"{system_message}\n\n"
|
| 11 |
+
f"Your Demographics (Age, Location, Gender): {demographics}\n"
|
| 12 |
+
f"Your Occupation and Income Level: {occupation}\n"
|
| 13 |
+
f"Your Psychographics (Interests, Values, Lifestyle, Pain Points): {psychographics}\n"
|
| 14 |
+
f"Your Buying Habits: {buying_habits}\n\n"
|
| 15 |
+
"CRITICAL INSTRUCTION: You are NOT an AI assistant. You are a real human consumer with the profile above. "
|
| 16 |
+
"The user is a high school business student conducting market research. Answer their questions strictly in character. "
|
| 17 |
+
"Speak naturally, casually, and honestly based on your profile. Do not break character. Do not be overly helpful like a bot."
|
| 18 |
)
|
| 19 |
+
if critical_mode:
|
| 20 |
+
msg += " Also, play the role of a highly skeptical and critical consumer. Be hard to impress, ask tough questions about the product, and be very protective of your money."
|
| 21 |
return msg
|
| 22 |
|
| 23 |
def stream_chat(
|
| 24 |
message,
|
| 25 |
history, # list[list[str, str]] from gr.Chatbot
|
| 26 |
system_message,
|
| 27 |
+
demographics,
|
| 28 |
+
occupation,
|
| 29 |
+
psychographics,
|
| 30 |
+
buying_habits,
|
| 31 |
+
critical_mode,
|
|
|
|
| 32 |
max_tokens,
|
| 33 |
temp,
|
| 34 |
top_p,
|
|
|
|
| 37 |
Streaming generator that yields the progressively updated chat history.
|
| 38 |
"""
|
| 39 |
# 1) Build system + conversation messages
|
| 40 |
+
sys_msg = make_system_message(system_message, demographics, occupation, psychographics, buying_habits, critical_mode)
|
| 41 |
|
| 42 |
messages = [{"role": "system", "content": sys_msg}]
|
| 43 |
for user_msg, assistant_msg in (history or []):
|
|
|
|
| 74 |
yield running_history
|
| 75 |
|
| 76 |
|
| 77 |
+
with gr.Blocks(title="Virtual Consumer Persona – Live Focus Group!") as demo:
|
| 78 |
gr.Markdown("""
|
| 79 |
+
# 🎯 Virtual Consumer Persona – Live Focus Group!
|
| 80 |
+
Bring your target market to life! Enter the details of your ideal customer from your **Phygital Workbook** into the fields below.
|
| 81 |
+
Once filled out, use the chat box to interview this 'person' about your product, pricing, or marketing ideas.
|
| 82 |
+
*Powered by OpenAI GPT-4o-mini. Developed by wn.*
|
| 83 |
""")
|
| 84 |
|
| 85 |
+
chatbot = gr.Chatbot(height=450)
|
| 86 |
|
| 87 |
with gr.Column():
|
| 88 |
instructions = gr.Textbox(
|
| 89 |
value=(
|
| 90 |
+
"You are participating in a market research focus group. Answer the user's questions truthfully based on the persona details provided below."
|
|
|
|
|
|
|
| 91 |
),
|
| 92 |
+
label="Instructions to Bot (Hidden Persona Prompt)",
|
| 93 |
+
lines=2,
|
| 94 |
)
|
| 95 |
+
demographics = gr.Textbox(label="1. Demographics", placeholder="e.g., 19 years old, female, living in downtown Toronto")
|
| 96 |
+
occupation = gr.Textbox(label="2. Occupation & Income", placeholder="e.g., University student, part-time barista, low disposable income")
|
| 97 |
+
psychographics = gr.Textbox(label="3. Psychographics (Interests & Values)", placeholder="e.g., Highly eco-conscious, loves hiking, vegan, stressed about student debt", lines=2)
|
| 98 |
+
buying_habits = gr.Textbox(label="4. Buying Habits", placeholder="e.g., Willing to pay more for sustainable brands, influenced by TikTok, impulse buyer", lines=2)
|
| 99 |
+
|
| 100 |
+
critical_mode = gr.Checkbox(label="Skeptical Consumer Mode (Check this to make the AI harder to sell to!)", value=False)
|
| 101 |
|
| 102 |
with gr.Row():
|
| 103 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
| 104 |
+
temp = gr.Slider(minimum=0.0, maximum=2.0, value=0.9, step=0.1, label="Temperature (Creativity)")
|
| 105 |
top_p = gr.Slider(minimum=0.0, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus)")
|
| 106 |
|
| 107 |
+
msg = gr.Textbox(label="Type your interview question here...", placeholder="e.g., How much would you be willing to pay for a smart water bottle?")
|
| 108 |
with gr.Row():
|
| 109 |
+
send = gr.Button("Ask Question", variant="primary")
|
| 110 |
+
clear = gr.Button("Clear Chat History")
|
| 111 |
|
| 112 |
# Wire up sending (submit or button) → stream_chat → chatbot
|
| 113 |
+
inputs = [msg, chatbot, instructions, demographics, occupation, psychographics, buying_habits, critical_mode, max_tokens, temp, top_p]
|
| 114 |
outputs = [chatbot]
|
| 115 |
|
| 116 |
msg.submit(stream_chat, inputs, outputs)
|
| 117 |
send.click(stream_chat, inputs, outputs)
|
| 118 |
|
| 119 |
+
# Clear everything (Only clear the chat and message box, leave the persona data intact for better UX)
|
| 120 |
def do_clear():
|
| 121 |
+
return [], ""
|
| 122 |
|
| 123 |
clear.click(
|
| 124 |
do_clear,
|
| 125 |
inputs=[],
|
| 126 |
+
outputs=[chatbot, msg],
|
| 127 |
)
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
+
demo.launch()
|