willn9 commited on
Commit
f7b6e36
·
verified ·
1 Parent(s): 56a97d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -16
app.py CHANGED
@@ -21,9 +21,9 @@ def make_system_message(
21
  f"Your Psychographics (Interests, Values, Lifestyle, Pain Points): {psychographics}\n"
22
  f"Your Buying Habits: {buying_habits}\n\n"
23
  "CRITICAL INSTRUCTION: You are not an AI assistant. You are a real human consumer with the profile above. "
24
- "The user is conducting market research. Answer questions strictly in character. "
25
- "Speak naturally, casually, and honestly based on your profile. Do not break character. "
26
- "Do not sound like a bot or assistant."
27
  )
28
 
29
  if critical_mode:
@@ -56,14 +56,13 @@ def stream_chat(
56
  return
57
 
58
  running_history = history.copy()
59
- running_history.append({"role": "user", "content": message})
60
- running_history.append({"role": "assistant", "content": ""})
61
  yield running_history
62
 
63
  if client is None:
64
- running_history[-1]["content"] = (
65
- "❌ Missing OPENAI_API_KEY. Please add it in Hugging Face Space "
66
- "Settings → Variables and secrets."
67
  )
68
  yield running_history
69
  return
@@ -78,11 +77,13 @@ def stream_chat(
78
  )
79
 
80
  messages = [{"role": "system", "content": sys_msg}]
81
- for msg in history:
82
- if isinstance(msg, dict) and msg.get("role") in {"user", "assistant"}:
83
- messages.append(
84
- {"role": msg["role"], "content": msg.get("content", "")}
85
- )
 
 
86
  messages.append({"role": "user", "content": message})
87
 
88
  try:
@@ -101,11 +102,11 @@ def stream_chat(
101
  delta = chunk.choices[0].delta
102
  if delta and delta.content:
103
  reply += delta.content
104
- running_history[-1]["content"] = reply
105
  yield running_history
106
 
107
  except Exception as e:
108
- running_history[-1]["content"] = f"❌ An error occurred: {str(e)}"
109
  yield running_history
110
 
111
 
@@ -125,7 +126,6 @@ Bring your target market to life. Enter the details of your ideal customer below
125
  )
126
 
127
  chatbot = gr.Chatbot(
128
- type="messages",
129
  height=450,
130
  label="Persona Interview",
131
  )
 
21
  f"Your Psychographics (Interests, Values, Lifestyle, Pain Points): {psychographics}\n"
22
  f"Your Buying Habits: {buying_habits}\n\n"
23
  "CRITICAL INSTRUCTION: You are not an AI assistant. You are a real human consumer with the profile above. "
24
+ "The user is a business or marketing student conducting market research. "
25
+ "Answer questions strictly in character. Speak naturally, casually, and honestly based on your profile. "
26
+ "Do not break character. Do not sound like a bot or assistant."
27
  )
28
 
29
  if critical_mode:
 
56
  return
57
 
58
  running_history = history.copy()
59
+ running_history.append((message, ""))
 
60
  yield running_history
61
 
62
  if client is None:
63
+ running_history[-1] = (
64
+ message,
65
+ "❌ Missing OPENAI_API_KEY. Please add it in Hugging Face Space Settings → Variables and secrets.",
66
  )
67
  yield running_history
68
  return
 
77
  )
78
 
79
  messages = [{"role": "system", "content": sys_msg}]
80
+
81
+ for user_msg, assistant_msg in history:
82
+ if user_msg:
83
+ messages.append({"role": "user", "content": str(user_msg)})
84
+ if assistant_msg:
85
+ messages.append({"role": "assistant", "content": str(assistant_msg)})
86
+
87
  messages.append({"role": "user", "content": message})
88
 
89
  try:
 
102
  delta = chunk.choices[0].delta
103
  if delta and delta.content:
104
  reply += delta.content
105
+ running_history[-1] = (message, reply)
106
  yield running_history
107
 
108
  except Exception as e:
109
+ running_history[-1] = (message, f"❌ An error occurred: {str(e)}")
110
  yield running_history
111
 
112
 
 
126
  )
127
 
128
  chatbot = gr.Chatbot(
 
129
  height=450,
130
  label="Persona Interview",
131
  )