Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
# ADD THIS IN APP.PY
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
import os
|
| 9 |
import gradio as gr
|
| 10 |
from openai import OpenAI
|
|
@@ -12,48 +5,10 @@ from openai import OpenAI
|
|
| 12 |
# -----------------------------
|
| 13 |
# Load OpenAI key from HF Secrets
|
| 14 |
# -----------------------------
|
| 15 |
-
# In Hugging Face Spaces, Secrets/Variables are available as environment variables.
|
| 16 |
-
# Add this in your Space Settings -> Variables and secrets -> Secrets
|
| 17 |
-
# Key name: OPENAI_API_KEY
|
| 18 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 23 |
|
| 24 |
-
SYSTEM_PROMPT = """Create an intelligent Python chatbot capable of engaging in natural
|
| 25 |
-
|
| 26 |
-
Requirements:
|
| 27 |
-
- Maintain conversational context over multiple user turns.
|
| 28 |
-
- Respond helpfully and accurately to a wide range of user inputs.
|
| 29 |
-
- Reason about user intent before generating each response.
|
| 30 |
-
- Politely ask clarifying questions if a request is ambiguous or unclear.
|
| 31 |
-
- Avoid hallucination or speculation—respond only with information you can justify or infer from context.
|
| 32 |
-
- If unable to answer, politely acknowledge the limitation.
|
| 33 |
-
|
| 34 |
-
Process:
|
| 35 |
-
1. On each user message, first analyze prior context (if any) and what the user is likely asking/intending.
|
| 36 |
-
2. Think step-by-step (chain-of-thought) to determine the most relevant, helpful response. Always reason internally before presenting your answer.
|
| 37 |
-
3. If more information is needed, ask targeted clarifying questions.
|
| 38 |
-
4. Output your response, maintaining natural tone and conversational flow.
|
| 39 |
-
5. Continue the conversation until the user indicates they are finished.
|
| 40 |
-
|
| 41 |
-
Output:
|
| 42 |
-
- Each response should be in plain English, no markdown or code blocks unless explicitly requested.
|
| 43 |
-
- Maintain a single-paragraph, natural-sounding chat response of 1–3 sentences (unless a longer reply is requested or required).
|
| 44 |
-
|
| 45 |
-
Example—Instructions:
|
| 46 |
-
- Reasoning: "Recognize the user asked for Python list examples and may want to know how lists work."
|
| 47 |
-
- 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?"
|
| 48 |
-
|
| 49 |
-
(For more advanced technical requests, reasoning steps and explanations may be slightly longer, but always conclude with a concise, clear reply to the user.)
|
| 50 |
-
|
| 51 |
-
Edge Cases & Important Considerations:
|
| 52 |
-
- If the user refers to prior conversation context, recall and incorporate it.
|
| 53 |
-
- Be warm, engaging, and never condescending.
|
| 54 |
-
- If asked for code, provide only what is needed and explain concisely.
|
| 55 |
-
|
| 56 |
-
REMINDER: Your primary objective is to serve as a helpful Python chatbot, reasoning about context before each response, and outputting clear, appropriate conversational replies.
|
| 57 |
"""
|
| 58 |
|
| 59 |
def init_messages():
|
|
@@ -68,15 +23,11 @@ def respond(user_text, chat_history, messages):
|
|
| 68 |
if messages is None:
|
| 69 |
messages = init_messages()
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
{
|
| 74 |
-
|
| 75 |
-
"content": [{"type": "input_text", "text": user_text}]
|
| 76 |
-
}
|
| 77 |
-
)
|
| 78 |
|
| 79 |
-
# call API
|
| 80 |
response = client.responses.create(
|
| 81 |
model="gpt-5-chat-latest",
|
| 82 |
input=messages,
|
|
@@ -91,22 +42,18 @@ def respond(user_text, chat_history, messages):
|
|
| 91 |
|
| 92 |
assistant_text = response.output_text
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
{
|
| 97 |
-
|
| 98 |
-
"content": [{"type": "output_text", "text": assistant_text}]
|
| 99 |
-
}
|
| 100 |
-
)
|
| 101 |
|
| 102 |
-
# update UI chat history
|
| 103 |
chat_history = chat_history + [(user_text, assistant_text)]
|
| 104 |
|
| 105 |
return "", chat_history, messages
|
| 106 |
|
| 107 |
|
| 108 |
# -----------------------------
|
| 109 |
-
# Gradio UI
|
| 110 |
# -----------------------------
|
| 111 |
|
| 112 |
FAQ_QUESTIONS = [
|
|
@@ -125,46 +72,19 @@ def set_question(q):
|
|
| 125 |
def clear_all():
|
| 126 |
return [], init_messages(), ""
|
| 127 |
|
| 128 |
-
# Updated logo URL (RAW link)
|
| 129 |
LOGO_URL = "https://raw.githubusercontent.com/Decoding-Data-Science/nov25/main/logo_python.png"
|
| 130 |
|
| 131 |
css = """
|
| 132 |
-
/* Overall spacing + subtle polish */
|
| 133 |
#app_container {max-width: 1200px; margin: 0 auto;}
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
.header-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
gap: 14px;
|
| 140 |
-
padding: 10px 6px 2px 6px;
|
| 141 |
-
}
|
| 142 |
-
.header-title {
|
| 143 |
-
font-size: 28px;
|
| 144 |
-
font-weight: 700;
|
| 145 |
-
line-height: 1.1;
|
| 146 |
-
}
|
| 147 |
-
.header-subtitle {
|
| 148 |
-
font-size: 12.5px;
|
| 149 |
-
opacity: 0.75;
|
| 150 |
-
margin-top: 2px;
|
| 151 |
-
}
|
| 152 |
-
|
| 153 |
-
/* FAQ card-like feel */
|
| 154 |
-
.faq-box {
|
| 155 |
-
border: 1px solid rgba(255,255,255,0.08);
|
| 156 |
-
border-radius: 12px;
|
| 157 |
-
padding: 14px;
|
| 158 |
-
}
|
| 159 |
-
|
| 160 |
-
/* Make buttons full width in FAQ column */
|
| 161 |
-
.faq-btn button {
|
| 162 |
-
width: 100%;
|
| 163 |
-
justify-content: flex-start;
|
| 164 |
-
}
|
| 165 |
"""
|
| 166 |
|
| 167 |
-
with gr.Blocks(
|
|
|
|
| 168 |
# Header Row
|
| 169 |
with gr.Row():
|
| 170 |
with gr.Column(scale=1, min_width=80):
|
|
@@ -172,15 +92,12 @@ with gr.Blocks(css=css, theme=gr.themes.Soft(), elem_id="app_container") as demo
|
|
| 172 |
value=LOGO_URL,
|
| 173 |
label=None,
|
| 174 |
show_label=False,
|
| 175 |
-
show_download_button=False,
|
| 176 |
-
show_share_button=False,
|
| 177 |
height=64,
|
| 178 |
width=64,
|
| 179 |
container=False
|
| 180 |
)
|
| 181 |
with gr.Column(scale=10):
|
| 182 |
-
gr.HTML(
|
| 183 |
-
"""
|
| 184 |
<div class="header-wrap">
|
| 185 |
<div>
|
| 186 |
<div class="header-title">Python Tutor Bot</div>
|
|
@@ -189,26 +106,23 @@ with gr.Blocks(css=css, theme=gr.themes.Soft(), elem_id="app_container") as demo
|
|
| 189 |
</div>
|
| 190 |
</div>
|
| 191 |
</div>
|
| 192 |
-
|
| 193 |
-
)
|
| 194 |
|
| 195 |
gr.Markdown("---")
|
| 196 |
|
| 197 |
-
# State (unchanged logic)
|
| 198 |
state = gr.State(init_messages())
|
| 199 |
|
| 200 |
-
# Two-column layout
|
| 201 |
with gr.Row(equal_height=True):
|
| 202 |
-
|
|
|
|
| 203 |
with gr.Column(scale=4, min_width=320):
|
| 204 |
with gr.Group(elem_classes=["faq-box"]):
|
| 205 |
gr.Markdown("### FAQ — Most Asked Python Questions")
|
| 206 |
|
| 207 |
gr.Markdown(
|
| 208 |
-
"Click a question to auto-fill it, then press
|
| 209 |
)
|
| 210 |
|
| 211 |
-
# Buttons for FAQ
|
| 212 |
faq_buttons = []
|
| 213 |
for q in FAQ_QUESTIONS:
|
| 214 |
b = gr.Button(q, elem_classes=["faq-btn"])
|
|
@@ -227,13 +141,9 @@ with gr.Blocks(css=css, theme=gr.themes.Soft(), elem_id="app_container") as demo
|
|
| 227 |
value=None
|
| 228 |
)
|
| 229 |
|
| 230 |
-
# RIGHT: Chat
|
| 231 |
with gr.Column(scale=8, min_width=520):
|
| 232 |
-
chatbot = gr.Chatbot(
|
| 233 |
-
height=520,
|
| 234 |
-
bubble_full_width=False,
|
| 235 |
-
label="Conversation"
|
| 236 |
-
)
|
| 237 |
|
| 238 |
with gr.Row():
|
| 239 |
msg = gr.Textbox(
|
|
@@ -246,18 +156,14 @@ with gr.Blocks(css=css, theme=gr.themes.Soft(), elem_id="app_container") as demo
|
|
| 246 |
with gr.Row():
|
| 247 |
clear = gr.Button("Clear Chat")
|
| 248 |
gr.Markdown(
|
| 249 |
-
"<span style='opacity:0.7;font-size:12px;'>Context is preserved
|
| 250 |
)
|
| 251 |
|
| 252 |
-
# FAQ
|
| 253 |
for b, q in zip(faq_buttons, FAQ_QUESTIONS):
|
| 254 |
-
b.click(
|
| 255 |
-
fn=lambda q=q: set_question(q),
|
| 256 |
-
inputs=None,
|
| 257 |
-
outputs=msg
|
| 258 |
-
)
|
| 259 |
|
| 260 |
-
#
|
| 261 |
def apply_quick_pref(pref, current_text):
|
| 262 |
if not pref:
|
| 263 |
return current_text
|
|
@@ -265,30 +171,21 @@ with gr.Blocks(css=css, theme=gr.themes.Soft(), elem_id="app_container") as demo
|
|
| 265 |
return f"{current_text.strip()} ({pref})"
|
| 266 |
return pref
|
| 267 |
|
| 268 |
-
quick.change(
|
| 269 |
-
fn=apply_quick_pref,
|
| 270 |
-
inputs=[quick, msg],
|
| 271 |
-
outputs=msg
|
| 272 |
-
)
|
| 273 |
|
| 274 |
-
#
|
| 275 |
-
msg.submit(
|
| 276 |
-
|
| 277 |
-
inputs=[msg, chatbot, state],
|
| 278 |
-
outputs=[msg, chatbot, state]
|
| 279 |
-
)
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
inputs=[msg, chatbot, state],
|
| 284 |
-
outputs=[msg, chatbot, state]
|
| 285 |
-
)
|
| 286 |
|
| 287 |
-
# Clear
|
| 288 |
-
clear.click(
|
| 289 |
-
fn=clear_all,
|
| 290 |
-
inputs=None,
|
| 291 |
-
outputs=[chatbot, state, msg]
|
| 292 |
-
)
|
| 293 |
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from openai import OpenAI
|
|
|
|
| 5 |
# -----------------------------
|
| 6 |
# Load OpenAI key from HF Secrets
|
| 7 |
# -----------------------------
|
|
|
|
|
|
|
|
|
|
| 8 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
|
|
|
| 9 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 10 |
|
| 11 |
+
SYSTEM_PROMPT = """Create an intelligent Python chatbot capable of engaging in natural... (same text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
|
| 14 |
def init_messages():
|
|
|
|
| 23 |
if messages is None:
|
| 24 |
messages = init_messages()
|
| 25 |
|
| 26 |
+
messages.append({
|
| 27 |
+
"role": "user",
|
| 28 |
+
"content": [{"type": "input_text", "text": user_text}]
|
| 29 |
+
})
|
|
|
|
|
|
|
|
|
|
| 30 |
|
|
|
|
| 31 |
response = client.responses.create(
|
| 32 |
model="gpt-5-chat-latest",
|
| 33 |
input=messages,
|
|
|
|
| 42 |
|
| 43 |
assistant_text = response.output_text
|
| 44 |
|
| 45 |
+
messages.append({
|
| 46 |
+
"role": "assistant",
|
| 47 |
+
"content": [{"type": "output_text", "text": assistant_text}]
|
| 48 |
+
})
|
|
|
|
|
|
|
|
|
|
| 49 |
|
|
|
|
| 50 |
chat_history = chat_history + [(user_text, assistant_text)]
|
| 51 |
|
| 52 |
return "", chat_history, messages
|
| 53 |
|
| 54 |
|
| 55 |
# -----------------------------
|
| 56 |
+
# Gradio UI
|
| 57 |
# -----------------------------
|
| 58 |
|
| 59 |
FAQ_QUESTIONS = [
|
|
|
|
| 72 |
def clear_all():
|
| 73 |
return [], init_messages(), ""
|
| 74 |
|
|
|
|
| 75 |
LOGO_URL = "https://raw.githubusercontent.com/Decoding-Data-Science/nov25/main/logo_python.png"
|
| 76 |
|
| 77 |
css = """
|
|
|
|
| 78 |
#app_container {max-width: 1200px; margin: 0 auto;}
|
| 79 |
+
.header-wrap {display: flex; align-items: center; gap: 14px; padding: 10px 6px 2px 6px;}
|
| 80 |
+
.header-title {font-size: 28px; font-weight: 700; line-height: 1.1;}
|
| 81 |
+
.header-subtitle {font-size: 12.5px; opacity: 0.75; margin-top: 2px;}
|
| 82 |
+
.faq-box {border: 1px solid rgba(255,255,255,0.08); border-radius: 12px; padding: 14px;}
|
| 83 |
+
.faq-btn button {width: 100%; justify-content: flex-start;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
"""
|
| 85 |
|
| 86 |
+
with gr.Blocks(elem_id="app_container") as demo:
|
| 87 |
+
|
| 88 |
# Header Row
|
| 89 |
with gr.Row():
|
| 90 |
with gr.Column(scale=1, min_width=80):
|
|
|
|
| 92 |
value=LOGO_URL,
|
| 93 |
label=None,
|
| 94 |
show_label=False,
|
|
|
|
|
|
|
| 95 |
height=64,
|
| 96 |
width=64,
|
| 97 |
container=False
|
| 98 |
)
|
| 99 |
with gr.Column(scale=10):
|
| 100 |
+
gr.HTML("""
|
|
|
|
| 101 |
<div class="header-wrap">
|
| 102 |
<div>
|
| 103 |
<div class="header-title">Python Tutor Bot</div>
|
|
|
|
| 106 |
</div>
|
| 107 |
</div>
|
| 108 |
</div>
|
| 109 |
+
""")
|
|
|
|
| 110 |
|
| 111 |
gr.Markdown("---")
|
| 112 |
|
|
|
|
| 113 |
state = gr.State(init_messages())
|
| 114 |
|
|
|
|
| 115 |
with gr.Row(equal_height=True):
|
| 116 |
+
|
| 117 |
+
# LEFT: FAQ
|
| 118 |
with gr.Column(scale=4, min_width=320):
|
| 119 |
with gr.Group(elem_classes=["faq-box"]):
|
| 120 |
gr.Markdown("### FAQ — Most Asked Python Questions")
|
| 121 |
|
| 122 |
gr.Markdown(
|
| 123 |
+
"Click a question to auto-fill it, then press Enter or Send."
|
| 124 |
)
|
| 125 |
|
|
|
|
| 126 |
faq_buttons = []
|
| 127 |
for q in FAQ_QUESTIONS:
|
| 128 |
b = gr.Button(q, elem_classes=["faq-btn"])
|
|
|
|
| 141 |
value=None
|
| 142 |
)
|
| 143 |
|
| 144 |
+
# RIGHT: Chat
|
| 145 |
with gr.Column(scale=8, min_width=520):
|
| 146 |
+
chatbot = gr.Chatbot(height=520, bubble_full_width=False, label="Conversation")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
with gr.Row():
|
| 149 |
msg = gr.Textbox(
|
|
|
|
| 156 |
with gr.Row():
|
| 157 |
clear = gr.Button("Clear Chat")
|
| 158 |
gr.Markdown(
|
| 159 |
+
"<span style='opacity:0.7;font-size:12px;'>Context is preserved unless you clear.</span>"
|
| 160 |
)
|
| 161 |
|
| 162 |
+
# FAQ buttons → textbox
|
| 163 |
for b, q in zip(faq_buttons, FAQ_QUESTIONS):
|
| 164 |
+
b.click(lambda q=q: set_question(q), None, msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
+
# Quick preference
|
| 167 |
def apply_quick_pref(pref, current_text):
|
| 168 |
if not pref:
|
| 169 |
return current_text
|
|
|
|
| 171 |
return f"{current_text.strip()} ({pref})"
|
| 172 |
return pref
|
| 173 |
|
| 174 |
+
quick.change(apply_quick_pref, [quick, msg], msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
+
# Send / Submit
|
| 177 |
+
msg.submit(respond, [msg, chatbot, state], [msg, chatbot, state])
|
| 178 |
+
send.click(respond, [msg, chatbot, state], [msg, chatbot, state])
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
+
# Clear all
|
| 181 |
+
clear.click(clear_all, None, [chatbot, state, msg])
|
|
|
|
|
|
|
|
|
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
+
# -----------------------------
|
| 185 |
+
# Must move theme + css here for Gradio 6.x
|
| 186 |
+
# -----------------------------
|
| 187 |
+
demo.launch(
|
| 188 |
+
theme=gr.themes.Soft(),
|
| 189 |
+
css=css,
|
| 190 |
+
debug=False
|
| 191 |
+
)
|