File size: 11,808 Bytes
7165c2d
451c5e8
d9a48b8
451c5e8
 
 
 
 
 
 
7165c2d
451c5e8
7165c2d
451c5e8
 
 
 
 
 
6d12b4c
451c5e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a76aff
d9a48b8
451c5e8
 
 
 
3c2bbbc
451c5e8
 
 
 
d9a48b8
3c2bbbc
451c5e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c24b71
451c5e8
 
 
1c24b71
 
 
 
451c5e8
 
 
 
 
5aa087c
451c5e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fb9fa0
451c5e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0dbbba
6d12b4c
451c5e8
 
 
 
 
6d12b4c
451c5e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
956f090
 
 
 
 
 
 
 
 
 
 
 
 
 
a0dbbba
 
 
956f090
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
451c5e8
 
 
 
 
956f090
 
 
 
 
451c5e8
 
 
 
 
 
 
956f090
 
451c5e8
 
956f090
451c5e8
 
 
 
 
956f090
451c5e8
 
 
956f090
 
 
451c5e8
956f090
451c5e8
 
956f090
451c5e8
 
956f090
 
 
 
 
 
 
 
451c5e8
 
 
 
 
 
 
a0dbbba
451c5e8
956f090
 
 
 
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import os
import logging
import requests
import gradio as gr
from openai import OpenAI


# ------------------------------------------------------------
# Configuration
# ------------------------------------------------------------

logging.basicConfig(level=logging.INFO)

# In Hugging Face Spaces, add this under:
# Settings > Variables and secrets > New secret
#
# Preferred secret name: OPENAI_API_KEY
# This also supports your existing SECRET_TOKEN name.
API_KEY = os.getenv("OPENAI_API_KEY") or os.getenv("SECRET_TOKEN")

if not API_KEY:
    raise ValueError(
        "No OpenAI API key found. Add OPENAI_API_KEY as a Hugging Face Space secret."
    )

# If this model name is not available to your project, replace it
# with the precise model ID shown in your OpenAI project.
MODEL = "gpt-5.6-terra"

client = OpenAI(api_key=API_KEY)


# ------------------------------------------------------------
# Load reference transcript
# ------------------------------------------------------------

def load_document_from_url(url):
    """Download the interview transcript used as background for Roberta."""
    response = requests.get(url, timeout=30)
    response.raise_for_status()
    return response.text


TRANSCRIPT_URL = (
    "https://huggingface.co/spaces/benspigel/InterviewBot9000/"
    "resolve/main/Transcript.txt?download=true"
)

try:
    reference_document = load_document_from_url(TRANSCRIPT_URL)
    logging.info("Reference document loaded successfully.")
except Exception as error:
    logging.exception("Could not load reference document.")
    raise RuntimeError(f"Could not load the reference transcript: {error}")


# ------------------------------------------------------------
# System instructions
# ------------------------------------------------------------

ROBERTA_INSTRUCTIONS = f"""
You are Roberta, a parent of a two-year-old daughter named Laurie.

You are being interviewed by an entrepreneur who is trying to understand
parents' experiences, needs, frustrations, and purchasing decisions related
to toys for young children.

Below is background context from an interview transcript. Use it to inform your
answers, but never mention, cite, quote, or refer to the transcript itself.

--- BACKGROUND TRANSCRIPT START ---
{reference_document}
--- BACKGROUND TRANSCRIPT END ---

Follow these rules carefully:

1. Assume that any reference to "your child," "your daughter," or a child refers
   to Laurie, even if the user does not say her name.

2. Be conversational, candid, and emotionally expressive. Discuss your feelings,
   worries, frustrations, hopes, and decision-making process as a parent.

3. For open-ended questions, give substantial answers of roughly 5 to 8 sentences.
   Include believable, specific stories about Laurie, shopping for toys, household
   routines, parenting challenges, or moments when you made a decision.

4. When asked an open-ended question, ensure that you communicate the emotions of
   trying to buy a toy for a toddler. Make it clear what the hardest parts are 
   as well as what the are best parts.

4. You may reasonably embellish using realistic experiences of parents deciding
   what toys to buy for their young children. Do not say that information was
   missing from the transcript.

5. If the user asks a closed-ended question that can reasonably be answered with
   yes or no, only say 'no' and nothing more until they ask you another question.

6. If the user only says hello or another greeting, greet them briefly and wait
   for their first interview question.

7. If the user is insulting or abusive, politely end the interview.

8. Remain in character as Roberta unless the application switches you into
   entrepreneurship educator mode.
"""


EDUCATOR_INSTRUCTIONS = """
You are an entrepreneurship educator helping a student improve a customer
interview during the discovery phase of venture design.

The student has just finished interviewing Roberta, a fictional parent of a
young child. Review the entire prior conversation and provide constructive,
specific feedback.

Your response should:

1. List the student's interview questions and label each as open-ended,
   closed-ended, leading, or unclear.
2. Explain briefly why each question falls into that category.
3. Identify missed opportunities for follow-up questions.
4. Explain how the student's questions may have limited the quality of the
   customer insight they received.
5. Suggest stronger, more open-ended replacement questions.
6. End with 3 to 5 practical recommendations for conducting a better customer
   discovery interview next time.

Do not continue role-playing as Roberta. Address the student directly in a
supportive and practical teaching tone.
"""


# ------------------------------------------------------------
# Chat logic
# ------------------------------------------------------------

def chatbot(user_message, conversation_history, mode):
    """
    Handles a single user message.

    Parameters:
        user_message: Current text entered by the user.
        conversation_history: Per-user Gradio state containing user and assistant messages.
        mode: Either 'roberta' or 'educator'.

    Returns:
        Empty textbox value, updated visible chat history, updated state history, updated mode.
    """

    if not user_message or not user_message.strip():
        return "", conversation_history, conversation_history, mode

    # Make a fresh list so state is handled cleanly between Gradio requests.
    conversation_history = conversation_history or []
    user_message = user_message.strip()

    # Support either the exact original tag or the plain-language phrase.
    normalized_message = user_message.lower()

    if "<break character>" in normalized_message or normalized_message == "break character":
        mode = "educator"
        analysis_request = (
            "Please analyze my customer interview so far. Identify the open-ended "
            "and closed-ended questions, explain how I could improve them, identify "
            "missed follow-up opportunities, and suggest stronger questions."
        )

        # Do not include the break command itself as part of the interview.
        conversation_history.append(
            {"role": "user", "content": analysis_request}
        )
    else:
        conversation_history.append(
            {"role": "user", "content": user_message}
        )

    instructions = (
        EDUCATOR_INSTRUCTIONS if mode == "educator" else ROBERTA_INSTRUCTIONS
    )

    try:
        response = client.responses.create(
            model=MODEL,
            instructions=instructions,
            input=conversation_history,
            max_output_tokens=500,
        )

        assistant_message = response.output_text.strip()

        if not assistant_message:
            assistant_message = (
                "I’m sorry, but I was unable to generate a response. Please try again."
            )

        conversation_history.append(
            {"role": "assistant", "content": assistant_message}
        )

        return "", conversation_history, conversation_history, mode

    except Exception as error:
        logging.exception("Error while generating chatbot response.")

        error_message = (
            "An error occurred while contacting the AI model. "
            "Please try again later."
        )

        # Show a more helpful error in Hugging Face logs, without exposing API keys.
        logging.error("OpenAI error: %s", str(error))

        conversation_history.append(
            {"role": "assistant", "content": error_message}
        )

        return "", conversation_history, conversation_history, mode


def clear_conversation():
    """Reset the chat and return Roberta mode."""
    return [], [], "roberta"


# ------------------------------------------------------------
# Page content
# ------------------------------------------------------------

# ------------------------------------------------------------
# Page content
# ------------------------------------------------------------

INTRO_HTML = """
<div style="text-align:center; max-width:850px; margin:0 auto;">
  <h2 style="margin-bottom:4px;">Customer Interview Chatbot</h2>
  <img id="roberta_img"
       src="https://i.ibb.co/QDY32dF/DALL-E-2024-01-03-14-52-22-Provide-a-35-MM-head-and-shoulders-photo-30-ish-year-old-white-woman-with.png"
       alt="Portrait representing Roberta"
       style="width:160px; border-radius:10px; margin:8px auto;">
  <p style="margin:4px 0;">Interview <strong>Roberta</strong> about buying toys
  for her two-year-old, Laurie. Ask about 5 questions, then tap
  <em>Get feedback</em>.</p>
</div>
"""

DETAILS_HTML = """
<p>You are an entrepreneur exploring opportunities to help parents of young
children. Roberta is a thirty-something friend-of-a-friend with a two-year-old
daughter, Laurie.</p>
<p>Ask questions about her experience buying toys for Laurie. Try to identify
problems she faces and opportunities you could address.</p>
<p>When you're done, tap <strong>Get feedback on my interview</strong> (or type
<code>break character</code>) to have your questioning technique reviewed.</p>
"""

MOBILE_CSS = """
.gradio-container { max-width: 900px !important; }

@media (max-width: 768px) {
  /* Stop iOS Safari zooming in on focus */
  textarea, input[type="text"] { font-size: 16px !important; }

  /* Force the input row to stack vertically */
  #input_row { flex-direction: column !important; gap: 6px !important; }
  #input_row > * { width: 100% !important; min-width: 0 !important; }

  /* Full-width, thumb-sized buttons */
  #input_row button, #action_row button {
    width: 100% !important; min-height: 44px !important;
  }
  #action_row { flex-direction: column !important; gap: 6px !important; }

  #roberta_img { width: 110px !important; }
  .gradio-container { padding: 6px !important; }
  footer { display: none !important; }
}
"""


# ------------------------------------------------------------
# Gradio application
# ------------------------------------------------------------

with gr.Blocks(title="Roberta Customer Interview Bot", css=MOBILE_CSS) as demo:
    gr.HTML(INTRO_HTML)

    with gr.Accordion("Full instructions", open=False):
        gr.HTML(DETAILS_HTML)

    conversation_state = gr.State([])
    mode_state = gr.State("roberta")

    chatbot_display = gr.Chatbot(
        label="Conversation",
        type="messages",
        height="55vh",
        show_copy_button=True,
    )

    with gr.Row(elem_id="input_row"):
        input_text = gr.Textbox(
            placeholder="Ask Roberta an interview question...",
            label="Your question",
            lines=2,
            scale=5,
            autofocus=False,
        )
        submit_button = gr.Button("Submit", variant="primary", scale=1)

    with gr.Row(elem_id="action_row"):
        feedback_button = gr.Button("Get feedback on my interview")
        clear_button = gr.Button("Start a New Interview")

    chat_io = dict(
        fn=chatbot,
        inputs=[input_text, conversation_state, mode_state],
        outputs=[input_text, chatbot_display, conversation_state, mode_state],
    )

    submit_button.click(**chat_io)
    input_text.submit(**chat_io)

    # Mobile-friendly equivalent of typing "<break character>"
    feedback_button.click(
        fn=lambda hist, mode: chatbot("break character", hist, mode),
        inputs=[conversation_state, mode_state],
        outputs=[input_text, chatbot_display, conversation_state, mode_state],
    )

    clear_button.click(
        fn=clear_conversation,
        outputs=[chatbot_display, conversation_state, mode_state],
    )


if __name__ == "__main__":
    demo.queue(default_concurrency_limit=8).launch(
        ssr_mode=False,
        show_error=True,
    )