Update app.py
Browse files
app.py
CHANGED
|
@@ -178,10 +178,14 @@ logger.info("π’ Agent initialized successfully!")
|
|
| 178 |
# Defines a function to process a user's question.
|
| 179 |
def ask_agent(question):
|
| 180 |
logger.info(f"Question asked: {question[:100]}...")
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
# Use a regular expression to find the first URL ending in '.pdf' in the response.
|
| 186 |
match = re.search(r'https?://[^\s]+\.pdf', full_content, re.IGNORECASE)
|
| 187 |
# Extract the link if a match is found, otherwise set it to None.
|
|
@@ -192,6 +196,7 @@ def ask_agent(question):
|
|
| 192 |
else:
|
| 193 |
logger.info("π΄ No PDF link found in response")
|
| 194 |
# Return the full text response and the extracted PDF link.
|
|
|
|
| 195 |
return full_content, link
|
| 196 |
|
| 197 |
# Defines a function to download the raw content of a PDF from a URL.
|
|
@@ -268,6 +273,19 @@ with gr.Blocks(
|
|
| 268 |
with gr.Column(scale=3):
|
| 269 |
# The chatbot display window.
|
| 270 |
chatbot = gr.Chatbot(label="π¬ Conversation", elem_classes="chatbot", height=450)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
# A text area for status messages (used for PDF loading status).
|
| 272 |
status_text = gr.Markdown("")
|
| 273 |
# The textbox where the user types their question.
|
|
@@ -277,7 +295,10 @@ with gr.Blocks(
|
|
| 277 |
lines=1
|
| 278 |
)
|
| 279 |
# The submit button.
|
| 280 |
-
ask_btn = gr.Button("Submit π€", variant="primary")
|
|
|
|
|
|
|
|
|
|
| 281 |
# A section for example questions.
|
| 282 |
gr.Markdown("### π‘ Example Questions")
|
| 283 |
gr.Examples(
|
|
@@ -303,6 +324,7 @@ with gr.Blocks(
|
|
| 303 |
label="β¬οΈ Cheat Sheet Preview",
|
| 304 |
visible=False
|
| 305 |
)
|
|
|
|
| 306 |
|
| 307 |
# Defines the main chat logic as a generator function for streaming output.
|
| 308 |
def chat_ui(user_message, chat_history):
|
|
@@ -319,7 +341,7 @@ with gr.Blocks(
|
|
| 319 |
# Append a temporary "Thinking..." message from the assistant.
|
| 320 |
chat_history.append({
|
| 321 |
"role": "assistant",
|
| 322 |
-
"content": "π€
|
| 323 |
})
|
| 324 |
|
| 325 |
# `yield` immediately updates the UI with the user's message and "Thinking...".
|
|
@@ -359,6 +381,37 @@ with gr.Blocks(
|
|
| 359 |
[chatbot, link_state, output_image, question]
|
| 360 |
)
|
| 361 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
# Set up the event handler for the "Submit" button click.
|
| 363 |
ask_btn.click(
|
| 364 |
*submit_chain()
|
|
@@ -373,6 +426,10 @@ with gr.Blocks(
|
|
| 373 |
display_pdf,
|
| 374 |
inputs=link_state, # Use the same link.
|
| 375 |
outputs=[output_image, pdf_status] # Update the image and hide the status text.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
)
|
| 377 |
|
| 378 |
# Set up the same event handler for when the user presses Enter in the textbox.
|
|
@@ -386,6 +443,10 @@ with gr.Blocks(
|
|
| 386 |
display_pdf,
|
| 387 |
inputs=link_state,
|
| 388 |
outputs=[output_image, pdf_status]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
)
|
| 390 |
|
| 391 |
# This block ensures the code inside only runs when the script is executed directly.
|
|
|
|
| 178 |
# Defines a function to process a user's question.
|
| 179 |
def ask_agent(question):
|
| 180 |
logger.info(f"Question asked: {question[:100]}...")
|
| 181 |
+
try:
|
| 182 |
+
# Run the agent with the user's question, ensuring it uses its knowledge base.
|
| 183 |
+
response = agent.run(question, use_knowledge=True)
|
| 184 |
+
# Get the agent's response as a single string.
|
| 185 |
+
full_content = response.get_content_as_string()
|
| 186 |
+
except Exception as e:
|
| 187 |
+
logger.error(str(e))
|
| 188 |
+
return "β Something went wrong. Please try again.", None
|
| 189 |
# Use a regular expression to find the first URL ending in '.pdf' in the response.
|
| 190 |
match = re.search(r'https?://[^\s]+\.pdf', full_content, re.IGNORECASE)
|
| 191 |
# Extract the link if a match is found, otherwise set it to None.
|
|
|
|
| 196 |
else:
|
| 197 |
logger.info("π΄ No PDF link found in response")
|
| 198 |
# Return the full text response and the extracted PDF link.
|
| 199 |
+
full_content += "\n\n---\n**π Try asking:**\n- Give me a real example...\n- Explain step by step...\n- Compare with alternatives..."
|
| 200 |
return full_content, link
|
| 201 |
|
| 202 |
# Defines a function to download the raw content of a PDF from a URL.
|
|
|
|
| 273 |
with gr.Column(scale=3):
|
| 274 |
# The chatbot display window.
|
| 275 |
chatbot = gr.Chatbot(label="π¬ Conversation", elem_classes="chatbot", height=450)
|
| 276 |
+
# ππ Feedback buttons
|
| 277 |
+
with gr.Row():
|
| 278 |
+
thumbs_up = gr.Button("π Helpful")
|
| 279 |
+
thumbs_down = gr.Button("π Not Helpful")
|
| 280 |
+
|
| 281 |
+
# Hidden feedback box (only appears on π)
|
| 282 |
+
feedback_box = gr.Textbox(
|
| 283 |
+
placeholder="π¬ Optional: tell us what went wrong...",
|
| 284 |
+
visible=False
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
submit_feedback_btn = gr.Button("Submit Feedback", visible=False)
|
| 288 |
+
feedback_status = gr.Markdown("")
|
| 289 |
# A text area for status messages (used for PDF loading status).
|
| 290 |
status_text = gr.Markdown("")
|
| 291 |
# The textbox where the user types their question.
|
|
|
|
| 295 |
lines=1
|
| 296 |
)
|
| 297 |
# The submit button.
|
| 298 |
+
#ask_btn = gr.Button("Submit π€", variant="primary")
|
| 299 |
+
with gr.Row():
|
| 300 |
+
ask_btn = gr.Button("Submit π€", variant="primary")
|
| 301 |
+
clear_btn = gr.Button("π§Ή Clear Chat")
|
| 302 |
# A section for example questions.
|
| 303 |
gr.Markdown("### π‘ Example Questions")
|
| 304 |
gr.Examples(
|
|
|
|
| 324 |
label="β¬οΈ Cheat Sheet Preview",
|
| 325 |
visible=False
|
| 326 |
)
|
| 327 |
+
pdf_link_btn = gr.Markdown("")
|
| 328 |
|
| 329 |
# Defines the main chat logic as a generator function for streaming output.
|
| 330 |
def chat_ui(user_message, chat_history):
|
|
|
|
| 341 |
# Append a temporary "Thinking..." message from the assistant.
|
| 342 |
chat_history.append({
|
| 343 |
"role": "assistant",
|
| 344 |
+
"content": "π€ Dox is thinking..."
|
| 345 |
})
|
| 346 |
|
| 347 |
# `yield` immediately updates the UI with the user's message and "Thinking...".
|
|
|
|
| 381 |
[chatbot, link_state, output_image, question]
|
| 382 |
)
|
| 383 |
|
| 384 |
+
def show_pdf_link(link):
|
| 385 |
+
if link:
|
| 386 |
+
return f"[π₯ Open Full PDF]({link})"
|
| 387 |
+
return ""
|
| 388 |
+
|
| 389 |
+
def clear_chat():
|
| 390 |
+
return [], None, gr.update(value=None, visible=False)
|
| 391 |
+
|
| 392 |
+
clear_btn.click(
|
| 393 |
+
clear_chat,
|
| 394 |
+
outputs=[chatbot, link_state, output_image]
|
| 395 |
+
)
|
| 396 |
+
|
| 397 |
+
def show_feedback_box():
|
| 398 |
+
return gr.update(visible=True), gr.update(visible=True)
|
| 399 |
+
|
| 400 |
+
thumbs_down.click(
|
| 401 |
+
show_feedback_box,
|
| 402 |
+
outputs=[feedback_box, submit_feedback_btn]
|
| 403 |
+
)
|
| 404 |
+
|
| 405 |
+
def handle_feedback(text):
|
| 406 |
+
logger.info(f"User feedback: {text}")
|
| 407 |
+
return "β
Feedback submitted. Thank you!"
|
| 408 |
+
|
| 409 |
+
submit_feedback_btn.click(
|
| 410 |
+
handle_feedback,
|
| 411 |
+
inputs=feedback_box,
|
| 412 |
+
outputs=feedback_status
|
| 413 |
+
)
|
| 414 |
+
|
| 415 |
# Set up the event handler for the "Submit" button click.
|
| 416 |
ask_btn.click(
|
| 417 |
*submit_chain()
|
|
|
|
| 426 |
display_pdf,
|
| 427 |
inputs=link_state, # Use the same link.
|
| 428 |
outputs=[output_image, pdf_status] # Update the image and hide the status text.
|
| 429 |
+
).then(
|
| 430 |
+
show_pdf_link,
|
| 431 |
+
inputs=link_state,
|
| 432 |
+
outputs=pdf_link_btn
|
| 433 |
)
|
| 434 |
|
| 435 |
# Set up the same event handler for when the user presses Enter in the textbox.
|
|
|
|
| 443 |
display_pdf,
|
| 444 |
inputs=link_state,
|
| 445 |
outputs=[output_image, pdf_status]
|
| 446 |
+
).then(
|
| 447 |
+
show_pdf_link,
|
| 448 |
+
inputs=link_state,
|
| 449 |
+
outputs=pdf_link_btn
|
| 450 |
)
|
| 451 |
|
| 452 |
# This block ensures the code inside only runs when the script is executed directly.
|