Update app.py
Browse files
app.py
CHANGED
|
@@ -50,6 +50,9 @@ Edge Cases & Important Considerations:
|
|
| 50 |
REMINDER: Your primary objective is to serve as a helpful Python chatbot, reasoning about context before each response, and outputting clear, appropriate conversational replies.
|
| 51 |
"""
|
| 52 |
|
|
|
|
|
|
|
|
|
|
| 53 |
def init_messages():
|
| 54 |
return [
|
| 55 |
{
|
|
@@ -58,11 +61,24 @@ def init_messages():
|
|
| 58 |
}
|
| 59 |
]
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def respond(user_text, chat_history, messages):
|
| 62 |
if messages is None:
|
| 63 |
messages = init_messages()
|
| 64 |
|
| 65 |
-
# add user turn
|
| 66 |
messages.append(
|
| 67 |
{
|
| 68 |
"role": "user",
|
|
@@ -85,7 +101,7 @@ def respond(user_text, chat_history, messages):
|
|
| 85 |
|
| 86 |
assistant_text = response.output_text
|
| 87 |
|
| 88 |
-
# add assistant turn
|
| 89 |
messages.append(
|
| 90 |
{
|
| 91 |
"role": "assistant",
|
|
@@ -93,8 +109,8 @@ def respond(user_text, chat_history, messages):
|
|
| 93 |
}
|
| 94 |
)
|
| 95 |
|
| 96 |
-
# update UI
|
| 97 |
-
chat_history = (chat_history
|
| 98 |
|
| 99 |
return "", chat_history, messages
|
| 100 |
|
|
@@ -102,7 +118,6 @@ def respond(user_text, chat_history, messages):
|
|
| 102 |
# -----------------------------
|
| 103 |
# Gradio UI
|
| 104 |
# -----------------------------
|
| 105 |
-
|
| 106 |
FAQ_QUESTIONS = [
|
| 107 |
"What is the difference between a list, tuple, and set in Python?",
|
| 108 |
"How do I use dictionaries effectively in Python?",
|
|
@@ -157,7 +172,7 @@ with gr.Blocks(elem_id="app_container") as demo:
|
|
| 157 |
# Header Row
|
| 158 |
with gr.Row():
|
| 159 |
with gr.Column(scale=1, min_width=80):
|
| 160 |
-
|
| 161 |
value=LOGO_URL,
|
| 162 |
label=None,
|
| 163 |
show_label=False,
|
|
@@ -181,10 +196,12 @@ with gr.Blocks(elem_id="app_container") as demo:
|
|
| 181 |
|
| 182 |
gr.Markdown("---")
|
| 183 |
|
|
|
|
| 184 |
state = gr.State(init_messages())
|
| 185 |
|
|
|
|
| 186 |
with gr.Row(equal_height=True):
|
| 187 |
-
# LEFT
|
| 188 |
with gr.Column(scale=4, min_width=320):
|
| 189 |
with gr.Group(elem_classes=["faq-box"]):
|
| 190 |
gr.Markdown("### FAQ — Most Asked Python Questions")
|
|
@@ -208,12 +225,13 @@ with gr.Blocks(elem_id="app_container") as demo:
|
|
| 208 |
value=None
|
| 209 |
)
|
| 210 |
|
| 211 |
-
# RIGHT
|
| 212 |
with gr.Column(scale=8, min_width=520):
|
| 213 |
chatbot = gr.Chatbot(
|
| 214 |
height=520,
|
| 215 |
-
label="Conversation"
|
| 216 |
-
|
|
|
|
| 217 |
)
|
| 218 |
|
| 219 |
with gr.Row():
|
|
@@ -234,6 +252,7 @@ with gr.Blocks(elem_id="app_container") as demo:
|
|
| 234 |
for b, q in zip(faq_buttons, FAQ_QUESTIONS):
|
| 235 |
b.click(fn=lambda q=q: set_question(q), inputs=None, outputs=msg)
|
| 236 |
|
|
|
|
| 237 |
def apply_quick_pref(pref, current_text):
|
| 238 |
if not pref:
|
| 239 |
return current_text
|
|
@@ -243,9 +262,11 @@ with gr.Blocks(elem_id="app_container") as demo:
|
|
| 243 |
|
| 244 |
quick.change(fn=apply_quick_pref, inputs=[quick, msg], outputs=msg)
|
| 245 |
|
|
|
|
| 246 |
msg.submit(respond, inputs=[msg, chatbot, state], outputs=[msg, chatbot, state])
|
| 247 |
send.click(respond, inputs=[msg, chatbot, state], outputs=[msg, chatbot, state])
|
| 248 |
|
|
|
|
| 249 |
clear.click(fn=clear_all, inputs=None, outputs=[chatbot, state, msg])
|
| 250 |
|
| 251 |
demo.launch(
|
|
|
|
| 50 |
REMINDER: Your primary objective is to serve as a helpful Python chatbot, reasoning about context before each response, and outputting clear, appropriate conversational replies.
|
| 51 |
"""
|
| 52 |
|
| 53 |
+
# -----------------------------
|
| 54 |
+
# OpenAI message state (internal)
|
| 55 |
+
# -----------------------------
|
| 56 |
def init_messages():
|
| 57 |
return [
|
| 58 |
{
|
|
|
|
| 61 |
}
|
| 62 |
]
|
| 63 |
|
| 64 |
+
# -----------------------------
|
| 65 |
+
# Gradio Chatbot UI history (messages format)
|
| 66 |
+
# Each item must be: {"role": "...", "content": "..."}
|
| 67 |
+
# -----------------------------
|
| 68 |
+
def append_ui_history(chat_history, user_text, assistant_text):
|
| 69 |
+
if chat_history is None:
|
| 70 |
+
chat_history = []
|
| 71 |
+
chat_history = chat_history + [
|
| 72 |
+
{"role": "user", "content": user_text},
|
| 73 |
+
{"role": "assistant", "content": assistant_text},
|
| 74 |
+
]
|
| 75 |
+
return chat_history
|
| 76 |
+
|
| 77 |
def respond(user_text, chat_history, messages):
|
| 78 |
if messages is None:
|
| 79 |
messages = init_messages()
|
| 80 |
|
| 81 |
+
# add user turn for OpenAI
|
| 82 |
messages.append(
|
| 83 |
{
|
| 84 |
"role": "user",
|
|
|
|
| 101 |
|
| 102 |
assistant_text = response.output_text
|
| 103 |
|
| 104 |
+
# add assistant turn for OpenAI
|
| 105 |
messages.append(
|
| 106 |
{
|
| 107 |
"role": "assistant",
|
|
|
|
| 109 |
}
|
| 110 |
)
|
| 111 |
|
| 112 |
+
# update UI history (messages format)
|
| 113 |
+
chat_history = append_ui_history(chat_history, user_text, assistant_text)
|
| 114 |
|
| 115 |
return "", chat_history, messages
|
| 116 |
|
|
|
|
| 118 |
# -----------------------------
|
| 119 |
# Gradio UI
|
| 120 |
# -----------------------------
|
|
|
|
| 121 |
FAQ_QUESTIONS = [
|
| 122 |
"What is the difference between a list, tuple, and set in Python?",
|
| 123 |
"How do I use dictionaries effectively in Python?",
|
|
|
|
| 172 |
# Header Row
|
| 173 |
with gr.Row():
|
| 174 |
with gr.Column(scale=1, min_width=80):
|
| 175 |
+
gr.Image(
|
| 176 |
value=LOGO_URL,
|
| 177 |
label=None,
|
| 178 |
show_label=False,
|
|
|
|
| 196 |
|
| 197 |
gr.Markdown("---")
|
| 198 |
|
| 199 |
+
# State for OpenAI messages
|
| 200 |
state = gr.State(init_messages())
|
| 201 |
|
| 202 |
+
# Two-column layout
|
| 203 |
with gr.Row(equal_height=True):
|
| 204 |
+
# LEFT: FAQ + Quick Ask
|
| 205 |
with gr.Column(scale=4, min_width=320):
|
| 206 |
with gr.Group(elem_classes=["faq-box"]):
|
| 207 |
gr.Markdown("### FAQ — Most Asked Python Questions")
|
|
|
|
| 225 |
value=None
|
| 226 |
)
|
| 227 |
|
| 228 |
+
# RIGHT: Chat area
|
| 229 |
with gr.Column(scale=8, min_width=520):
|
| 230 |
chatbot = gr.Chatbot(
|
| 231 |
height=520,
|
| 232 |
+
label="Conversation"
|
| 233 |
+
# No bubble_full_width
|
| 234 |
+
# No type=
|
| 235 |
)
|
| 236 |
|
| 237 |
with gr.Row():
|
|
|
|
| 252 |
for b, q in zip(faq_buttons, FAQ_QUESTIONS):
|
| 253 |
b.click(fn=lambda q=q: set_question(q), inputs=None, outputs=msg)
|
| 254 |
|
| 255 |
+
# Optional quick preference: append hint to textbox (UI-only)
|
| 256 |
def apply_quick_pref(pref, current_text):
|
| 257 |
if not pref:
|
| 258 |
return current_text
|
|
|
|
| 262 |
|
| 263 |
quick.change(fn=apply_quick_pref, inputs=[quick, msg], outputs=msg)
|
| 264 |
|
| 265 |
+
# Submit logic
|
| 266 |
msg.submit(respond, inputs=[msg, chatbot, state], outputs=[msg, chatbot, state])
|
| 267 |
send.click(respond, inputs=[msg, chatbot, state], outputs=[msg, chatbot, state])
|
| 268 |
|
| 269 |
+
# Clear
|
| 270 |
clear.click(fn=clear_all, inputs=None, outputs=[chatbot, state, msg])
|
| 271 |
|
| 272 |
demo.launch(
|