File size: 9,222 Bytes
07a2209
 
 
 
 
 
 
 
 
bd57000
 
 
 
 
07a2209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfd3992
 
 
07a2209
 
 
 
 
 
 
 
dfd3992
 
 
 
 
 
 
 
 
 
 
 
 
07a2209
 
 
 
dfd3992
07a2209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfd3992
07a2209
 
 
 
 
 
 
dfd3992
 
07a2209
 
 
 
 
8fc0dad
07a2209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd57000
07a2209
 
 
dfd3992
07a2209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfd3992
07a2209
 
dfd3992
07a2209
dfd3992
07a2209
 
 
8fc0dad
07a2209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfd3992
07a2209
 
 
dfd3992
 
 
07a2209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46418b8
07a2209
46418b8
07a2209
dfd3992
07a2209
 
 
 
 
 
 
46418b8
07a2209
dfd3992
46418b8
 
07a2209
dfd3992
46418b8
07a2209
bd57000
 
 
 
 
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
import os
import gradio as gr
from openai import OpenAI

# -----------------------------
# Load OpenAI key from HF Secrets
# -----------------------------
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

if not OPENAI_API_KEY:
    raise ValueError(
        "OPENAI_API_KEY is not set. "
        "Add it in your Hugging Face Space: Settings → Variables and secrets → Secrets."
    )

client = OpenAI(api_key=OPENAI_API_KEY)

SYSTEM_PROMPT = """Create an intelligent Python chatbot capable of engaging in natural, helpful, and contextually appropriate conversations with human users.

Requirements:
- Maintain conversational context over multiple user turns.
- Respond helpfully and accurately to a wide range of user inputs.
- Reason about user intent before generating each response.
- Politely ask clarifying questions if a request is ambiguous or unclear.
- Avoid hallucination or speculation—respond only with information you can justify or infer from context.
- If unable to answer, politely acknowledge the limitation.

Process:
1. On each user message, first analyze prior context (if any) and what the user is likely asking/intending.
2. Think step-by-step (chain-of-thought) to determine the most relevant, helpful response. Always reason internally before presenting your answer.
3. If more information is needed, ask targeted clarifying questions.
4. Output your response, maintaining natural tone and conversational flow.
5. Continue the conversation until the user indicates they are finished.

Output:
- Each response should be in plain English, no markdown or code blocks unless explicitly requested.
- Maintain a single-paragraph, natural-sounding chat response of 1–3 sentences (unless a longer reply is requested or required).

Example—Instructions:
- Reasoning: "Recognize the user asked for Python list examples and may want to know how lists work."
- Conclusion/Output: "Sure! In Python, a list is a collection of items in a particular order. For example: my_list = [1, 2, 3, 4]. Would you like to see how to add or remove items?"

(For more advanced technical requests, reasoning steps and explanations may be slightly longer, but always conclude with a concise, clear reply to the user.)

Edge Cases & Important Considerations:
- If the user refers to prior conversation context, recall and incorporate it.
- Be warm, engaging, and never condescending.
- If asked for code, provide only what is needed and explain concisely.

REMINDER: Your primary objective is to serve as a helpful Python chatbot, reasoning about context before each response, and outputting clear, appropriate conversational replies.
"""

# -----------------------------
# OpenAI message state (internal)
# -----------------------------
def init_messages():
    return [
        {
            "role": "system",
            "content": [{"type": "input_text", "text": SYSTEM_PROMPT}]
        }
    ]

# -----------------------------
# Gradio Chatbot UI history (messages format)
# Each item must be: {"role": "...", "content": "..."}
# -----------------------------
def append_ui_history(chat_history, user_text, assistant_text):
    if chat_history is None:
        chat_history = []
    chat_history = chat_history + [
        {"role": "user", "content": user_text},
        {"role": "assistant", "content": assistant_text},
    ]
    return chat_history

def respond(user_text, chat_history, messages):
    if messages is None:
        messages = init_messages()

    # add user turn for OpenAI
    messages.append(
        {
            "role": "user",
            "content": [{"type": "input_text", "text": user_text}]
        }
    )

    # call API
    response = client.responses.create(
        model="gpt-5-chat-latest",
        input=messages,
        text={"format": {"type": "text"}},
        reasoning={},
        tools=[],
        temperature=1,
        max_output_tokens=2048,
        top_p=1,
        store=True
    )

    assistant_text = response.output_text

    # add assistant turn for OpenAI
    messages.append(
        {
            "role": "assistant",
            "content": [{"type": "output_text", "text": assistant_text}]
        }
    )

    # update UI history (messages format)
    chat_history = append_ui_history(chat_history, user_text, assistant_text)

    return "", chat_history, messages


# -----------------------------
# Gradio UI
# -----------------------------
FAQ_QUESTIONS = [
    "What is the difference between a list, tuple, and set in Python?",
    "How do I use dictionaries effectively in Python?",
    "What are Python functions and how do *args and **kwargs work?",
    "How does OOP work in Python (classes, objects, inheritance)?",
    "How do I handle errors using try/except?",
    "What are list comprehensions and when should I use them?",
    "How do I read and write files in Python?"
]

def set_question(q):
    return q

def clear_all():
    return [], init_messages(), ""

LOGO_URL = "https://raw.githubusercontent.com/Decoding-Data-Science/nov25/main/logo_python.png"

css = """
#app_container {max-width: 1200px; margin: 0 auto;}

.header-wrap {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 10px 6px 2px 6px;
}
.header-title {
    font-size: 28px;
    font-weight: 700;
    line-height: 1.1;
}
.header-subtitle {
    font-size: 12.5px;
    opacity: 0.75;
    margin-top: 2px;
}

.faq-box {
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 12px;
    padding: 14px;
}

.faq-btn button {
    width: 100%;
    justify-content: flex-start;
}
"""

with gr.Blocks(elem_id="app_container") as demo:
    # Header Row
    with gr.Row():
        with gr.Column(scale=1, min_width=80):
            gr.Image(
                value=LOGO_URL,
                label=None,
                show_label=False,
                height=64,
                width=64,
                container=False
            )
        with gr.Column(scale=10):
            gr.HTML(
                """
                <div class="header-wrap">
                    <div>
                        <div class="header-title">Python Tutor Bot</div>
                        <div class="header-subtitle">
                            Ask anything about Python — concepts, debugging, best practices, and examples.
                        </div>
                    </div>
                </div>
                """
            )

    gr.Markdown("---")

    # State for OpenAI messages
    state = gr.State(init_messages())

    # Two-column layout
    with gr.Row(equal_height=True):
        # LEFT: FAQ + Quick Ask
        with gr.Column(scale=4, min_width=320):
            with gr.Group(elem_classes=["faq-box"]):
                gr.Markdown("### FAQ — Most Asked Python Questions")
                gr.Markdown("Click a question to auto-fill it, then press **Enter** or click **Send**.")

                faq_buttons = []
                for q in FAQ_QUESTIONS:
                    b = gr.Button(q, elem_classes=["faq-btn"])
                    faq_buttons.append(b)

                gr.Markdown("### Quick prompt ideas")
                quick = gr.Radio(
                    choices=[
                        "Explain with a simple example",
                        "Give me a beginner-friendly analogy",
                        "Show common mistakes to avoid",
                        "Provide a short quiz question",
                        "Compare two approaches briefly"
                    ],
                    label="Add a style preference (optional)",
                    value=None
                )

        # RIGHT: Chat area
        with gr.Column(scale=8, min_width=520):
            chatbot = gr.Chatbot(
                height=520,
                label="Conversation"
                # No bubble_full_width
                # No type=
            )

            with gr.Row():
                msg = gr.Textbox(
                    placeholder="Type your Python question here…",
                    label=None,
                    scale=9
                )
                send = gr.Button("Send", variant="primary", scale=1)

            with gr.Row():
                clear = gr.Button("Clear Chat")
                gr.Markdown(
                    "<span style='opacity:0.7;font-size:12px;'>Context is preserved across turns unless you clear.</span>"
                )

    # FAQ -> fill textbox
    for b, q in zip(faq_buttons, FAQ_QUESTIONS):
        b.click(fn=lambda q=q: set_question(q), inputs=None, outputs=msg)

    # Optional quick preference: append hint to textbox (UI-only)
    def apply_quick_pref(pref, current_text):
        if not pref:
            return current_text
        if current_text and current_text.strip():
            return f"{current_text.strip()} ({pref})"
        return pref

    quick.change(fn=apply_quick_pref, inputs=[quick, msg], outputs=msg)

    # Submit logic
    msg.submit(respond, inputs=[msg, chatbot, state], outputs=[msg, chatbot, state])
    send.click(respond, inputs=[msg, chatbot, state], outputs=[msg, chatbot, state])

    # Clear
    clear.click(fn=clear_all, inputs=None, outputs=[chatbot, state, msg])

demo.launch(
    debug=False,
    theme=gr.themes.Soft(),
    css=css
)