Spaces:
Sleeping
Sleeping
File size: 6,692 Bytes
2a6f904 6f99e1e 2a6f904 6f99e1e 2648ef7 cf9ab13 6f99e1e 2648ef7 6f99e1e cf9ab13 6f99e1e 2648ef7 6f99e1e 2648ef7 2a6f904 c8edcdd 2a6f904 cf9ab13 2a6f904 2648ef7 2a6f904 2648ef7 6f99e1e 2648ef7 6f99e1e 2a6f904 6f99e1e 2648ef7 6f99e1e 2a6f904 6f99e1e 5c21691 6f99e1e 2648ef7 2a6f904 5c21691 2a6f904 2648ef7 2a6f904 6f99e1e 5c21691 2a6f904 6f99e1e 5c21691 2648ef7 6f99e1e 2a6f904 5c21691 2648ef7 6f99e1e cf9ab13 6f99e1e 2648ef7 6f99e1e 2648ef7 6f99e1e 2648ef7 cf9ab13 2648ef7 6f99e1e 2648ef7 6f99e1e 2648ef7 cf9ab13 2648ef7 6f99e1e 2648ef7 6f99e1e 2648ef7 6f99e1e 2648ef7 6f99e1e 2a6f904 cf9ab13 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | import os
import gradio as gr
from openai import OpenAI
# Expects OPENAI_API_KEY in Hugging Face Space secrets / environment variables
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def make_system_message(
system_message,
demographics,
occupation,
psychographics,
buying_habits,
critical_mode,
):
msg = (
f"{system_message}\n\n"
f"Your Demographics (Age, Location, Gender): {demographics}\n"
f"Your Occupation and Income Level: {occupation}\n"
f"Your Psychographics (Interests, Values, Lifestyle, Pain Points): {psychographics}\n"
f"Your Buying Habits: {buying_habits}\n\n"
"CRITICAL INSTRUCTION: You are NOT an AI assistant. You are a real human consumer with the profile above. "
"The user is a business student conducting market research. Answer their questions strictly in character. "
"Speak naturally, casually, and honestly based on your profile. Do not break character. "
"Do not be overly helpful like a bot."
)
if critical_mode:
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."
)
return msg
def stream_chat(
message,
history,
system_message,
demographics,
occupation,
psychographics,
buying_habits,
critical_mode,
max_tokens,
temp,
top_p,
):
"""
Streaming generator that yields progressively updated chatbot history.
Uses Gradio Chatbot with type='messages'.
"""
history = history or []
# Ignore empty submissions
if not message or not message.strip():
yield history
return
sys_msg = make_system_message(
system_message,
demographics,
occupation,
psychographics,
buying_habits,
critical_mode,
)
# Build OpenAI messages
messages = [{"role": "system", "content": sys_msg}]
for msg in history:
if msg.get("role") in {"user", "assistant"} and "content" in msg:
messages.append({"role": msg["role"], "content": msg["content"]})
messages.append({"role": "user", "content": message})
# Build UI history
running_history = history.copy()
running_history.append({"role": "user", "content": message})
running_history.append({"role": "assistant", "content": ""})
# Show typing bubble immediately
yield running_history
try:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
max_tokens=int(max_tokens),
temperature=float(temp),
top_p=float(top_p),
stream=True,
)
running_reply = ""
for chunk in response:
delta = chunk.choices[0].delta
if delta and getattr(delta, "content", None):
running_reply += delta.content
running_history[-1]["content"] = running_reply
yield running_history
except Exception as e:
running_history[-1]["content"] = f"❌ An error occurred: {str(e)}"
yield running_history
def clear_chat():
return [], ""
with gr.Blocks(title="Virtual Consumer Persona – Live Focus Group!") as demo:
gr.Markdown(
"""
# 🎯 Virtual Consumer Persona – Live Focus Group!
Bring your target market to life. Enter the details of your ideal customer from your **Phygital Workbook** into the fields below.
Then use the chat box to interview this persona about your product, pricing, branding, messaging, or marketing ideas.
*Powered by OpenAI GPT-4o-mini. Developed by wn.*
"""
)
chatbot = gr.Chatbot(type="messages", height=450, label="Persona Interview")
with gr.Column():
instructions = gr.Textbox(
value=(
"You are participating in a market research focus group. "
"Answer the user's questions truthfully based on the persona details provided below."
),
label="Instructions to Bot (Hidden Persona Prompt)",
lines=2,
)
demographics = gr.Textbox(
label="1. Demographics",
placeholder="e.g., 19 years old, female, living in downtown Toronto",
)
occupation = gr.Textbox(
label="2. Occupation & Income",
placeholder="e.g., University student, part-time barista, low disposable income",
)
psychographics = gr.Textbox(
label="3. Psychographics (Interests & Values)",
placeholder="e.g., Highly eco-conscious, loves hiking, vegan, stressed about student debt",
lines=2,
)
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,
)
critical_mode = gr.Checkbox(
label="Skeptical Consumer Mode",
info="Check this to make the persona harder to convince.",
value=False,
)
with gr.Row():
max_tokens = gr.Slider(
minimum=1,
maximum=2048,
value=512,
step=1,
label="Max New Tokens",
)
temp = gr.Slider(
minimum=0.0,
maximum=2.0,
value=0.9,
step=0.1,
label="Temperature",
)
top_p = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.95,
step=0.05,
label="Top-p",
)
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?",
)
with gr.Row():
send = gr.Button("Ask Question", variant="primary")
clear = gr.Button("Clear Chat History")
inputs = [
msg,
chatbot,
instructions,
demographics,
occupation,
psychographics,
buying_habits,
critical_mode,
max_tokens,
temp,
top_p,
]
outputs = [chatbot]
msg.submit(stream_chat, inputs=inputs, outputs=outputs)
send.click(stream_chat, inputs=inputs, outputs=outputs)
# Clear only chat + question box, keep persona fields for convenience
clear.click(clear_chat, inputs=[], outputs=[chatbot, msg], queue=False)
demo.queue()
if __name__ == "__main__":
demo.launch() |