Update app.py
Browse files
app.py
CHANGED
|
@@ -93,8 +93,8 @@ def respond(user_text, chat_history, messages):
|
|
| 93 |
}
|
| 94 |
)
|
| 95 |
|
| 96 |
-
# update UI chat history
|
| 97 |
-
chat_history = chat_history + [(user_text, assistant_text)]
|
| 98 |
|
| 99 |
return "", chat_history, messages
|
| 100 |
|
|
@@ -122,10 +122,8 @@ def clear_all():
|
|
| 122 |
LOGO_URL = "https://raw.githubusercontent.com/Decoding-Data-Science/nov25/main/logo_python.png"
|
| 123 |
|
| 124 |
css = """
|
| 125 |
-
/* Overall spacing + subtle polish */
|
| 126 |
#app_container {max-width: 1200px; margin: 0 auto;}
|
| 127 |
|
| 128 |
-
/* Header styling */
|
| 129 |
.header-wrap {
|
| 130 |
display: flex;
|
| 131 |
align-items: center;
|
|
@@ -143,14 +141,12 @@ css = """
|
|
| 143 |
margin-top: 2px;
|
| 144 |
}
|
| 145 |
|
| 146 |
-
/* FAQ card-like feel */
|
| 147 |
.faq-box {
|
| 148 |
border: 1px solid rgba(255,255,255,0.08);
|
| 149 |
border-radius: 12px;
|
| 150 |
padding: 14px;
|
| 151 |
}
|
| 152 |
|
| 153 |
-
/* Make buttons full width in FAQ column */
|
| 154 |
.faq-btn button {
|
| 155 |
width: 100%;
|
| 156 |
justify-content: flex-start;
|
|
@@ -185,12 +181,10 @@ with gr.Blocks(elem_id="app_container") as demo:
|
|
| 185 |
|
| 186 |
gr.Markdown("---")
|
| 187 |
|
| 188 |
-
# State
|
| 189 |
state = gr.State(init_messages())
|
| 190 |
|
| 191 |
-
# Two-column layout
|
| 192 |
with gr.Row(equal_height=True):
|
| 193 |
-
# LEFT
|
| 194 |
with gr.Column(scale=4, min_width=320):
|
| 195 |
with gr.Group(elem_classes=["faq-box"]):
|
| 196 |
gr.Markdown("### FAQ — Most Asked Python Questions")
|
|
@@ -214,11 +208,12 @@ with gr.Blocks(elem_id="app_container") as demo:
|
|
| 214 |
value=None
|
| 215 |
)
|
| 216 |
|
| 217 |
-
# RIGHT
|
| 218 |
with gr.Column(scale=8, min_width=520):
|
| 219 |
chatbot = gr.Chatbot(
|
| 220 |
height=520,
|
| 221 |
-
label="Conversation"
|
|
|
|
| 222 |
)
|
| 223 |
|
| 224 |
with gr.Row():
|
|
@@ -235,15 +230,10 @@ with gr.Blocks(elem_id="app_container") as demo:
|
|
| 235 |
"<span style='opacity:0.7;font-size:12px;'>Context is preserved across turns unless you clear.</span>"
|
| 236 |
)
|
| 237 |
|
| 238 |
-
# FAQ
|
| 239 |
for b, q in zip(faq_buttons, FAQ_QUESTIONS):
|
| 240 |
-
b.click(
|
| 241 |
-
fn=lambda q=q: set_question(q),
|
| 242 |
-
inputs=None,
|
| 243 |
-
outputs=msg
|
| 244 |
-
)
|
| 245 |
|
| 246 |
-
# Optional quick preference: append hint to textbox (UI-only)
|
| 247 |
def apply_quick_pref(pref, current_text):
|
| 248 |
if not pref:
|
| 249 |
return current_text
|
|
@@ -251,31 +241,12 @@ with gr.Blocks(elem_id="app_container") as demo:
|
|
| 251 |
return f"{current_text.strip()} ({pref})"
|
| 252 |
return pref
|
| 253 |
|
| 254 |
-
quick.change(
|
| 255 |
-
fn=apply_quick_pref,
|
| 256 |
-
inputs=[quick, msg],
|
| 257 |
-
outputs=msg
|
| 258 |
-
)
|
| 259 |
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
respond,
|
| 263 |
-
inputs=[msg, chatbot, state],
|
| 264 |
-
outputs=[msg, chatbot, state]
|
| 265 |
-
)
|
| 266 |
|
| 267 |
-
|
| 268 |
-
respond,
|
| 269 |
-
inputs=[msg, chatbot, state],
|
| 270 |
-
outputs=[msg, chatbot, state]
|
| 271 |
-
)
|
| 272 |
-
|
| 273 |
-
# Clear
|
| 274 |
-
clear.click(
|
| 275 |
-
fn=clear_all,
|
| 276 |
-
inputs=None,
|
| 277 |
-
outputs=[chatbot, state, msg]
|
| 278 |
-
)
|
| 279 |
|
| 280 |
demo.launch(
|
| 281 |
debug=False,
|
|
|
|
| 93 |
}
|
| 94 |
)
|
| 95 |
|
| 96 |
+
# update UI chat history (tuple format)
|
| 97 |
+
chat_history = (chat_history or []) + [(user_text, assistant_text)]
|
| 98 |
|
| 99 |
return "", chat_history, messages
|
| 100 |
|
|
|
|
| 122 |
LOGO_URL = "https://raw.githubusercontent.com/Decoding-Data-Science/nov25/main/logo_python.png"
|
| 123 |
|
| 124 |
css = """
|
|
|
|
| 125 |
#app_container {max-width: 1200px; margin: 0 auto;}
|
| 126 |
|
|
|
|
| 127 |
.header-wrap {
|
| 128 |
display: flex;
|
| 129 |
align-items: center;
|
|
|
|
| 141 |
margin-top: 2px;
|
| 142 |
}
|
| 143 |
|
|
|
|
| 144 |
.faq-box {
|
| 145 |
border: 1px solid rgba(255,255,255,0.08);
|
| 146 |
border-radius: 12px;
|
| 147 |
padding: 14px;
|
| 148 |
}
|
| 149 |
|
|
|
|
| 150 |
.faq-btn button {
|
| 151 |
width: 100%;
|
| 152 |
justify-content: flex-start;
|
|
|
|
| 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 |
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 |
+
type="tuples" # ✅ IMPORTANT FIX
|
| 217 |
)
|
| 218 |
|
| 219 |
with gr.Row():
|
|
|
|
| 230 |
"<span style='opacity:0.7;font-size:12px;'>Context is preserved across turns unless you clear.</span>"
|
| 231 |
)
|
| 232 |
|
| 233 |
+
# FAQ -> fill textbox
|
| 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
|
|
|
|
| 241 |
return f"{current_text.strip()} ({pref})"
|
| 242 |
return pref
|
| 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(
|
| 252 |
debug=False,
|