Chris4K commited on
Commit
9706d54
·
verified ·
1 Parent(s): 10e9354

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -73
app.py CHANGED
@@ -15,6 +15,9 @@ from pathlib import Path
15
 
16
  load_dotenv()
17
 
 
 
 
18
  # There are tools set here dependent on environment variables
19
  from graph import graph, weak_model, search_enabled # noqa
20
 
@@ -26,9 +29,14 @@ USER_INPUT_MAX_LENGTH = 10000 # Characters
26
  # If you store sensitive data, you should store your secret in .env
27
  BROWSER_STORAGE_SECRET = "itsnosecret"
28
 
29
- with open('logging-config.json', 'r') as fh:
30
- config = json.load(fh)
31
- logging.config.dictConfig(config)
 
 
 
 
 
32
  logger = logging.getLogger(__name__)
33
 
34
  def load_initial_greeting(filepath="greeting_prompt.txt") -> str:
@@ -416,44 +424,59 @@ function triggerChatButtonClick() {
416
  if __name__ == "__main__":
417
  logger.info("Starting the DIYO interface")
418
 
 
 
 
 
 
 
 
 
419
  with gr.Blocks(title="DIYO - DIY Assistant", fill_height=True, css=CSS, elem_id="main-app") as demo:
420
- # State management
421
  is_new_user_for_greeting = gr.State(True)
422
 
423
- current_prompt_state = gr.BrowserState(
424
- value="You are a helpful DIY assistant.",
425
- storage_key="current_prompt_state",
426
- secret=BROWSER_STORAGE_SECRET,
427
- )
428
- current_uuid_state = gr.BrowserState(
429
- value=uuid4,
430
- storage_key="current_uuid_state",
431
- secret=BROWSER_STORAGE_SECRET,
432
- )
433
- current_langgraph_state = gr.BrowserState(
434
- value=dict,
435
- storage_key="current_langgraph_state",
436
- secret=BROWSER_STORAGE_SECRET,
437
- )
438
- end_of_assistant_response_state = gr.State(False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
439
 
440
- # [uuid] -> summary of chat
441
- sidebar_names_state = gr.BrowserState(
442
- value=dict,
443
- storage_key="sidebar_names_state",
444
- secret=BROWSER_STORAGE_SECRET,
445
- )
446
- # [uuid] -> {"graph": gradio_graph, "messages": messages}
447
- offloaded_tabs_data_storage = gr.BrowserState(
448
- value=dict,
449
- storage_key="offloaded_tabs_data_storage",
450
- secret=BROWSER_STORAGE_SECRET,
451
- )
452
- chatbot_message_storage = gr.BrowserState(
453
- value=list,
454
- storage_key="chatbot_message_storage",
455
- secret=BROWSER_STORAGE_SECRET,
456
- )
457
 
458
  # Header
459
  with gr.Row(elem_classes="header-margin"):
@@ -785,43 +808,51 @@ if __name__ == "__main__":
785
  trigger_mode="always_last"
786
  )
787
 
788
- # Load event handlers
789
- @demo.load(
790
- inputs=[is_new_user_for_greeting, chatbot_message_storage],
791
- outputs=[chatbot_message_storage, is_new_user_for_greeting]
792
- )
793
- def handle_initial_greeting_load(current_is_new_user_flag: bool, existing_chat_history: list):
794
- """Handle initial greeting when the app loads"""
795
- if current_is_new_user_flag:
796
- greeting_message_text = load_initial_greeting()
797
- greeting_entry = {"role": "assistant", "content": greeting_message_text}
798
-
799
- if not isinstance(existing_chat_history, list):
800
- existing_chat_history = []
801
 
802
- updated_chat_history = [greeting_entry] + existing_chat_history
803
- updated_is_new_user_flag = False
804
- logger.info("Greeting added for new user.")
805
- return updated_chat_history, updated_is_new_user_flag
806
- else:
807
- logger.info("Not a new user or already greeted.")
808
- if not isinstance(existing_chat_history, list):
809
- existing_chat_history = []
810
- return existing_chat_history, False
811
-
812
- @demo.load(inputs=[chatbot_message_storage], outputs=[chatbot])
813
- def load_messages(messages):
814
- """Load stored messages into chatbot"""
815
- if isinstance(messages, list):
816
- return messages
817
- return []
818
-
819
- @demo.load(inputs=[current_prompt_state], outputs=[prompt_textbox])
820
- def load_prompt(current_prompt):
821
- """Load stored prompt"""
822
- if current_prompt:
823
- return current_prompt
824
- return "You are a helpful DIY assistant."
 
 
 
 
 
 
 
 
 
 
825
 
826
  # Launch the application
827
  demo.launch(debug=True, share=True)
 
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
 
 
29
  # If you store sensitive data, you should store your secret in .env
30
  BROWSER_STORAGE_SECRET = "itsnosecret"
31
 
32
+ try:
33
+ with open('logging-config.json', 'r') as fh:
34
+ config = json.load(fh)
35
+ logging.config.dictConfig(config)
36
+ except FileNotFoundError:
37
+ # Fallback logging configuration
38
+ logging.basicConfig(level=logging.INFO)
39
+
40
  logger = logging.getLogger(__name__)
41
 
42
  def load_initial_greeting(filepath="greeting_prompt.txt") -> str:
 
424
  if __name__ == "__main__":
425
  logger.info("Starting the DIYO interface")
426
 
427
+ # Check if BrowserState is available
428
+ has_browser_state = hasattr(gr, 'BrowserState')
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
437
  is_new_user_for_greeting = gr.State(True)
438
 
439
+ if has_browser_state:
440
+ current_prompt_state = gr.BrowserState(
441
+ value="You are a helpful DIY assistant.",
442
+ storage_key="current_prompt_state",
443
+ secret=BROWSER_STORAGE_SECRET,
444
+ )
445
+ current_uuid_state = gr.BrowserState(
446
+ value=uuid4,
447
+ storage_key="current_uuid_state",
448
+ secret=BROWSER_STORAGE_SECRET,
449
+ )
450
+ current_langgraph_state = gr.BrowserState(
451
+ value=dict,
452
+ storage_key="current_langgraph_state",
453
+ secret=BROWSER_STORAGE_SECRET,
454
+ )
455
+ sidebar_names_state = gr.BrowserState(
456
+ value=dict,
457
+ storage_key="sidebar_names_state",
458
+ secret=BROWSER_STORAGE_SECRET,
459
+ )
460
+ offloaded_tabs_data_storage = gr.BrowserState(
461
+ value=dict,
462
+ storage_key="offloaded_tabs_data_storage",
463
+ secret=BROWSER_STORAGE_SECRET,
464
+ )
465
+ chatbot_message_storage = gr.BrowserState(
466
+ value=list,
467
+ storage_key="chatbot_message_storage",
468
+ secret=BROWSER_STORAGE_SECRET,
469
+ )
470
+ else:
471
+ # Fallback to regular State
472
+ current_prompt_state = gr.State("You are a helpful DIY assistant.")
473
+ current_uuid_state = gr.State(uuid4())
474
+ current_langgraph_state = gr.State({})
475
+ sidebar_names_state = gr.State({})
476
+ offloaded_tabs_data_storage = gr.State({})
477
+ chatbot_message_storage = gr.State([])
478
 
479
+ end_of_assistant_response_state = gr.State(False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
480
 
481
  # Header
482
  with gr.Row(elem_classes="header-margin"):
 
808
  trigger_mode="always_last"
809
  )
810
 
811
+ # Load event handlers - only add these if we have BrowserState
812
+ if has_browser_state:
813
+ @demo.load(
814
+ inputs=[is_new_user_for_greeting, chatbot_message_storage],
815
+ outputs=[chatbot_message_storage, is_new_user_for_greeting]
816
+ )
817
+ def handle_initial_greeting_load(current_is_new_user_flag: bool, existing_chat_history: list):
818
+ """Handle initial greeting when the app loads"""
819
+ if current_is_new_user_flag:
820
+ greeting_message_text = load_initial_greeting()
821
+ greeting_entry = {"role": "assistant", "content": greeting_message_text}
 
 
822
 
823
+ if not isinstance(existing_chat_history, list):
824
+ existing_chat_history = []
825
+
826
+ updated_chat_history = [greeting_entry] + existing_chat_history
827
+ updated_is_new_user_flag = False
828
+ logger.info("Greeting added for new user.")
829
+ return updated_chat_history, updated_is_new_user_flag
830
+ else:
831
+ logger.info("Not a new user or already greeted.")
832
+ if not isinstance(existing_chat_history, list):
833
+ existing_chat_history = []
834
+ return existing_chat_history, False
835
+
836
+ @demo.load(inputs=[chatbot_message_storage], outputs=[chatbot])
837
+ def load_messages(messages):
838
+ """Load stored messages into chatbot"""
839
+ if isinstance(messages, list):
840
+ return messages
841
+ return []
842
+
843
+ @demo.load(inputs=[current_prompt_state], outputs=[prompt_textbox])
844
+ def load_prompt(current_prompt):
845
+ """Load stored prompt"""
846
+ if current_prompt:
847
+ return current_prompt
848
+ return "You are a helpful DIY assistant."
849
+ else:
850
+ # For regular State, add a simple greeting on load
851
+ @demo.load(outputs=[chatbot])
852
+ def load_initial_greeting():
853
+ """Load initial greeting for users without BrowserState"""
854
+ greeting_text = load_initial_greeting()
855
+ return [{"role": "assistant", "content": greeting_text}]
856
 
857
  # Launch the application
858
  demo.launch(debug=True, share=True)