Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,9 +15,27 @@ from pathlib import Path
|
|
15 |
|
16 |
load_dotenv()
|
17 |
|
18 |
-
# Check Gradio version
|
19 |
print(f"Gradio version: {gr.__version__}")
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
# There are tools set here dependent on environment variables
|
22 |
from graph import graph, weak_model, search_enabled # noqa
|
23 |
|
@@ -394,6 +412,13 @@ footer {visibility: hidden}
|
|
394 |
display: none !important;
|
395 |
}
|
396 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
397 |
.wrap.sidebar-parent {
|
398 |
min-height: 2400px !important;
|
399 |
height: 2400px !important;
|
@@ -429,8 +454,14 @@ if __name__ == "__main__":
|
|
429 |
logger.info(f"BrowserState available: {has_browser_state}")
|
430 |
|
431 |
if not has_browser_state:
|
|
|
|
|
432 |
logger.warning("BrowserState not available in this Gradio version. Using regular State instead.")
|
433 |
logger.warning("To use BrowserState, upgrade Gradio: pip install gradio>=4.0.0")
|
|
|
|
|
|
|
|
|
434 |
|
435 |
with gr.Blocks(title="DIYO - DIY Assistant", fill_height=True, css=CSS, elem_id="main-app") as demo:
|
436 |
# State management - Use BrowserState if available, otherwise regular State
|
@@ -545,9 +576,11 @@ if __name__ == "__main__":
|
|
545 |
|
546 |
# Check if cache_examples parameter is supported
|
547 |
try:
|
548 |
-
|
|
|
549 |
examples_kwargs["cache_examples"] = False
|
550 |
-
except:
|
|
|
551 |
pass
|
552 |
|
553 |
gr.Examples(**examples_kwargs)
|
@@ -576,16 +609,20 @@ if __name__ == "__main__":
|
|
576 |
|
577 |
# Check if newer textbox parameters are supported
|
578 |
try:
|
579 |
-
|
|
|
|
|
580 |
textbox_kwargs["autofocus"] = True
|
581 |
-
if 'submit_btn' in
|
582 |
textbox_kwargs["submit_btn"] = True
|
583 |
-
if 'stop_btn' in
|
584 |
textbox_kwargs["stop_btn"] = True
|
585 |
except Exception as e:
|
586 |
logger.warning(f"Error checking textbox parameters: {e}")
|
|
|
587 |
|
588 |
textbox = textbox_component(**textbox_kwargs)
|
|
|
589 |
# Check if newer Chatbot parameters are supported
|
590 |
chatbot_kwargs = {
|
591 |
"height": 400,
|
@@ -594,23 +631,32 @@ if __name__ == "__main__":
|
|
594 |
|
595 |
# Add parameters that might not be available in older versions
|
596 |
try:
|
597 |
-
#
|
598 |
-
|
599 |
|
600 |
-
#
|
601 |
-
if
|
602 |
chatbot_kwargs["type"] = "messages"
|
|
|
|
|
|
|
603 |
|
604 |
# Check if 'show_copy_button' parameter is supported
|
605 |
-
if 'show_copy_button' in
|
606 |
chatbot_kwargs["show_copy_button"] = True
|
607 |
|
608 |
# Check if 'scale' parameter is supported
|
609 |
-
if 'scale' in
|
610 |
chatbot_kwargs["scale"] = 0
|
611 |
|
612 |
except Exception as e:
|
613 |
logger.warning(f"Error checking Chatbot parameters: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
614 |
|
615 |
chatbot = gr.Chatbot(**chatbot_kwargs)
|
616 |
|
@@ -631,8 +677,10 @@ if __name__ == "__main__":
|
|
631 |
outputs=[current_prompt_state]
|
632 |
)
|
633 |
|
634 |
-
# Sidebar
|
635 |
-
with gr.
|
|
|
|
|
636 |
@gr.render(inputs=[tab_edit_uuid_state, end_of_assistant_response_state, sidebar_names_state, current_uuid_state, chatbot, offloaded_tabs_data_storage])
|
637 |
def render_chats(tab_uuid_edit, end_of_chat_response, sidebar_summaries, active_uuid, messages, tabs):
|
638 |
# Ensure sidebar_summaries is a dict
|
@@ -777,16 +825,19 @@ if __name__ == "__main__":
|
|
777 |
|
778 |
# Check if newer ChatInterface parameters are supported
|
779 |
try:
|
|
|
|
|
780 |
# Check if 'type' parameter is supported
|
781 |
-
if 'type' in
|
782 |
chat_interface_kwargs["type"] = "messages"
|
783 |
|
784 |
# Check if 'multimodal' parameter is supported
|
785 |
-
if 'multimodal' in
|
786 |
chat_interface_kwargs["multimodal"] = multimodal
|
787 |
|
788 |
except Exception as e:
|
789 |
logger.warning(f"Error checking ChatInterface parameters: {e}")
|
|
|
790 |
|
791 |
chat_interface = gr.ChatInterface(**chat_interface_kwargs)
|
792 |
|
@@ -831,13 +882,21 @@ if __name__ == "__main__":
|
|
831 |
def setup_change_handler(fn, inputs, outputs, trigger_mode=None):
|
832 |
"""Helper function to set up change handlers with optional trigger_mode"""
|
833 |
try:
|
834 |
-
|
|
|
|
|
|
|
835 |
return chatbot.change(fn=fn, inputs=inputs, outputs=outputs, trigger_mode=trigger_mode)
|
836 |
else:
|
837 |
return chatbot.change(fn=fn, inputs=inputs, outputs=outputs)
|
838 |
except Exception as e:
|
839 |
logger.warning(f"Error setting up change handler: {e}")
|
840 |
-
|
|
|
|
|
|
|
|
|
|
|
841 |
|
842 |
setup_change_handler(
|
843 |
fn=populate_followup_questions,
|
|
|
15 |
|
16 |
load_dotenv()
|
17 |
|
18 |
+
# Check Gradio version and provide guidance
|
19 |
print(f"Gradio version: {gr.__version__}")
|
20 |
|
21 |
+
# Parse version to check compatibility
|
22 |
+
try:
|
23 |
+
version_parts = gr.__version__.split('.')
|
24 |
+
major_version = int(version_parts[0])
|
25 |
+
minor_version = int(version_parts[1]) if len(version_parts) > 1 else 0
|
26 |
+
|
27 |
+
if major_version < 4:
|
28 |
+
print("⚠️ WARNING: You're using an older version of Gradio.")
|
29 |
+
print(" Some features may be limited. Consider upgrading:")
|
30 |
+
print(" pip install --upgrade gradio>=4.0.0")
|
31 |
+
elif major_version >= 4:
|
32 |
+
print("✅ Gradio version is compatible with all features.")
|
33 |
+
|
34 |
+
except (ValueError, IndexError):
|
35 |
+
print("Could not parse Gradio version.")
|
36 |
+
|
37 |
+
print() # Add spacing
|
38 |
+
|
39 |
# There are tools set here dependent on environment variables
|
40 |
from graph import graph, weak_model, search_enabled # noqa
|
41 |
|
|
|
412 |
display: none !important;
|
413 |
}
|
414 |
|
415 |
+
.sidebar-replacement {
|
416 |
+
background-color: #f8f9fa;
|
417 |
+
border-left: 1px solid #dee2e6;
|
418 |
+
padding: 10px;
|
419 |
+
min-height: 400px;
|
420 |
+
}
|
421 |
+
|
422 |
.wrap.sidebar-parent {
|
423 |
min-height: 2400px !important;
|
424 |
height: 2400px !important;
|
|
|
454 |
logger.info(f"BrowserState available: {has_browser_state}")
|
455 |
|
456 |
if not has_browser_state:
|
457 |
+
print("📝 Note: Using session-only state (data won't persist after refresh)")
|
458 |
+
print(" For data persistence, upgrade to Gradio 4.0+")
|
459 |
logger.warning("BrowserState not available in this Gradio version. Using regular State instead.")
|
460 |
logger.warning("To use BrowserState, upgrade Gradio: pip install gradio>=4.0.0")
|
461 |
+
else:
|
462 |
+
print("💾 Using persistent browser state (data persists after refresh)")
|
463 |
+
|
464 |
+
print() # Add spacing
|
465 |
|
466 |
with gr.Blocks(title="DIYO - DIY Assistant", fill_height=True, css=CSS, elem_id="main-app") as demo:
|
467 |
# State management - Use BrowserState if available, otherwise regular State
|
|
|
576 |
|
577 |
# Check if cache_examples parameter is supported
|
578 |
try:
|
579 |
+
init_params = gr.Examples.__init__.__code__.co_varnames
|
580 |
+
if 'cache_examples' in init_params:
|
581 |
examples_kwargs["cache_examples"] = False
|
582 |
+
except Exception:
|
583 |
+
# Parameter not supported, skip it
|
584 |
pass
|
585 |
|
586 |
gr.Examples(**examples_kwargs)
|
|
|
609 |
|
610 |
# Check if newer textbox parameters are supported
|
611 |
try:
|
612 |
+
init_params = textbox_component.__init__.__code__.co_varnames
|
613 |
+
|
614 |
+
if 'autofocus' in init_params:
|
615 |
textbox_kwargs["autofocus"] = True
|
616 |
+
if 'submit_btn' in init_params:
|
617 |
textbox_kwargs["submit_btn"] = True
|
618 |
+
if 'stop_btn' in init_params:
|
619 |
textbox_kwargs["stop_btn"] = True
|
620 |
except Exception as e:
|
621 |
logger.warning(f"Error checking textbox parameters: {e}")
|
622 |
+
# Keep minimal parameters as fallback
|
623 |
|
624 |
textbox = textbox_component(**textbox_kwargs)
|
625 |
+
|
626 |
# Check if newer Chatbot parameters are supported
|
627 |
chatbot_kwargs = {
|
628 |
"height": 400,
|
|
|
631 |
|
632 |
# Add parameters that might not be available in older versions
|
633 |
try:
|
634 |
+
# Check parameter availability without creating test instance
|
635 |
+
init_params = gr.Chatbot.__init__.__code__.co_varnames
|
636 |
|
637 |
+
# Always try to set type="messages" to avoid the deprecation warning
|
638 |
+
if 'type' in init_params:
|
639 |
chatbot_kwargs["type"] = "messages"
|
640 |
+
logger.info("Using 'messages' type for chatbot")
|
641 |
+
else:
|
642 |
+
logger.warning("Chatbot 'type' parameter not supported, using default")
|
643 |
|
644 |
# Check if 'show_copy_button' parameter is supported
|
645 |
+
if 'show_copy_button' in init_params:
|
646 |
chatbot_kwargs["show_copy_button"] = True
|
647 |
|
648 |
# Check if 'scale' parameter is supported
|
649 |
+
if 'scale' in init_params:
|
650 |
chatbot_kwargs["scale"] = 0
|
651 |
|
652 |
except Exception as e:
|
653 |
logger.warning(f"Error checking Chatbot parameters: {e}")
|
654 |
+
# Use minimal parameters as fallback, but try to set type to avoid warning
|
655 |
+
chatbot_kwargs = {"height": 400}
|
656 |
+
try:
|
657 |
+
chatbot_kwargs["type"] = "messages"
|
658 |
+
except:
|
659 |
+
pass
|
660 |
|
661 |
chatbot = gr.Chatbot(**chatbot_kwargs)
|
662 |
|
|
|
677 |
outputs=[current_prompt_state]
|
678 |
)
|
679 |
|
680 |
+
# Chat History Sidebar (using simple approach for compatibility)
|
681 |
+
with gr.Column():
|
682 |
+
gr.Markdown("### Chat History")
|
683 |
+
|
684 |
@gr.render(inputs=[tab_edit_uuid_state, end_of_assistant_response_state, sidebar_names_state, current_uuid_state, chatbot, offloaded_tabs_data_storage])
|
685 |
def render_chats(tab_uuid_edit, end_of_chat_response, sidebar_summaries, active_uuid, messages, tabs):
|
686 |
# Ensure sidebar_summaries is a dict
|
|
|
825 |
|
826 |
# Check if newer ChatInterface parameters are supported
|
827 |
try:
|
828 |
+
init_params = gr.ChatInterface.__init__.__code__.co_varnames
|
829 |
+
|
830 |
# Check if 'type' parameter is supported
|
831 |
+
if 'type' in init_params:
|
832 |
chat_interface_kwargs["type"] = "messages"
|
833 |
|
834 |
# Check if 'multimodal' parameter is supported
|
835 |
+
if 'multimodal' in init_params:
|
836 |
chat_interface_kwargs["multimodal"] = multimodal
|
837 |
|
838 |
except Exception as e:
|
839 |
logger.warning(f"Error checking ChatInterface parameters: {e}")
|
840 |
+
# Keep minimal parameters as fallback
|
841 |
|
842 |
chat_interface = gr.ChatInterface(**chat_interface_kwargs)
|
843 |
|
|
|
882 |
def setup_change_handler(fn, inputs, outputs, trigger_mode=None):
|
883 |
"""Helper function to set up change handlers with optional trigger_mode"""
|
884 |
try:
|
885 |
+
# Get the change method's parameter names
|
886 |
+
change_params = chatbot.change.__code__.co_varnames
|
887 |
+
|
888 |
+
if trigger_mode and 'trigger_mode' in change_params:
|
889 |
return chatbot.change(fn=fn, inputs=inputs, outputs=outputs, trigger_mode=trigger_mode)
|
890 |
else:
|
891 |
return chatbot.change(fn=fn, inputs=inputs, outputs=outputs)
|
892 |
except Exception as e:
|
893 |
logger.warning(f"Error setting up change handler: {e}")
|
894 |
+
# Fallback to basic change handler
|
895 |
+
try:
|
896 |
+
return chatbot.change(fn=fn, inputs=inputs, outputs=outputs)
|
897 |
+
except Exception as fallback_error:
|
898 |
+
logger.error(f"Failed to set up change handler: {fallback_error}")
|
899 |
+
return None
|
900 |
|
901 |
setup_change_handler(
|
902 |
fn=populate_followup_questions,
|