seawolf2357 commited on
Commit
e4b0406
·
verified ·
1 Parent(s): 6c6de8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -36
app.py CHANGED
@@ -678,6 +678,10 @@ print(f"Search client initialized: {search_client is not None}, API key present:
678
  # Store web search settings by connection
679
  web_search_settings = {}
680
 
 
 
 
 
681
 
682
  class OpenAIHandler(AsyncStreamHandler):
683
  def __init__(self, web_search_enabled: bool = False, webrtc_id: str = None) -> None:
@@ -698,14 +702,21 @@ class OpenAIHandler(AsyncStreamHandler):
698
  print(f"Handler created with web_search_enabled={web_search_enabled}, webrtc_id={webrtc_id}")
699
 
700
  def copy(self):
701
- # Check for stored settings when copying
702
- if self.webrtc_id and self.webrtc_id in web_search_settings:
703
- web_search_enabled = web_search_settings[self.webrtc_id]
704
- else:
705
- web_search_enabled = self.web_search_enabled
 
 
 
 
 
 
 
706
 
707
- print(f"Handler.copy() called - creating new handler with web_search_enabled={web_search_enabled}")
708
- return OpenAIHandler(web_search_enabled=web_search_enabled, webrtc_id=self.webrtc_id)
709
 
710
  async def search_web(self, query: str) -> str:
711
  """Perform web search and return formatted results"""
@@ -730,6 +741,18 @@ class OpenAIHandler(AsyncStreamHandler):
730
 
731
  async def start_up(self):
732
  """Connect to realtime API with function calling enabled"""
 
 
 
 
 
 
 
 
 
 
 
 
733
  print(f"Starting up handler with web_search_enabled={self.web_search_enabled}")
734
  self.client = openai.AsyncOpenAI()
735
 
@@ -871,23 +894,15 @@ class OpenAIHandler(AsyncStreamHandler):
871
  self.connection = None
872
 
873
 
874
- # Factory function to create handler with settings
875
- def create_handler():
876
- """Factory function to create handler - will be customized per connection"""
877
- return OpenAIHandler(web_search_enabled=False)
878
-
879
-
880
- def update_chatbot(chatbot: list[dict], response: ResponseAudioTranscriptDoneEvent):
881
- chatbot.append({"role": "assistant", "content": response.transcript})
882
- return chatbot
883
-
884
 
885
  # Create components
886
  chatbot = gr.Chatbot(type="messages")
887
 
888
- # Create initial stream with factory function
889
  stream = Stream(
890
- create_handler, # Pass factory function instead of instance
891
  mode="send-receive",
892
  modality="audio",
893
  additional_inputs=[chatbot],
@@ -900,23 +915,6 @@ stream = Stream(
900
 
901
  app = FastAPI()
902
 
903
- # Custom handler factory that reads settings
904
- def custom_handler_factory():
905
- """Custom factory that checks for web search settings"""
906
- # Try to get the most recent settings
907
- if web_search_settings:
908
- # Get the most recent webrtc_id
909
- recent_id = max(web_search_settings.keys(), key=lambda k: web_search_settings[k].get('timestamp', 0))
910
- settings = web_search_settings[recent_id]
911
- return OpenAIHandler(
912
- web_search_enabled=settings.get('enabled', False),
913
- webrtc_id=recent_id
914
- )
915
- return OpenAIHandler(web_search_enabled=False)
916
-
917
- # Replace the handler factory
918
- stream.handler = custom_handler_factory
919
-
920
  # Mount stream
921
  stream.mount(app)
922
 
 
678
  # Store web search settings by connection
679
  web_search_settings = {}
680
 
681
+ def update_chatbot(chatbot: list[dict], response: ResponseAudioTranscriptDoneEvent):
682
+ chatbot.append({"role": "assistant", "content": response.transcript})
683
+ return chatbot
684
+
685
 
686
  class OpenAIHandler(AsyncStreamHandler):
687
  def __init__(self, web_search_enabled: bool = False, webrtc_id: str = None) -> None:
 
702
  print(f"Handler created with web_search_enabled={web_search_enabled}, webrtc_id={webrtc_id}")
703
 
704
  def copy(self):
705
+ # Get the most recent settings
706
+ if web_search_settings:
707
+ # Get the most recent webrtc_id
708
+ recent_ids = sorted(web_search_settings.keys(),
709
+ key=lambda k: web_search_settings[k].get('timestamp', 0),
710
+ reverse=True)
711
+ if recent_ids:
712
+ recent_id = recent_ids[0]
713
+ settings = web_search_settings[recent_id]
714
+ web_search_enabled = settings.get('enabled', False)
715
+ print(f"Handler.copy() using recent settings - webrtc_id={recent_id}, web_search_enabled={web_search_enabled}")
716
+ return OpenAIHandler(web_search_enabled=web_search_enabled, webrtc_id=recent_id)
717
 
718
+ print(f"Handler.copy() called - creating new handler with default settings")
719
+ return OpenAIHandler(web_search_enabled=False)
720
 
721
  async def search_web(self, query: str) -> str:
722
  """Perform web search and return formatted results"""
 
741
 
742
  async def start_up(self):
743
  """Connect to realtime API with function calling enabled"""
744
+ # First check if we have the most recent settings
745
+ if web_search_settings:
746
+ recent_ids = sorted(web_search_settings.keys(),
747
+ key=lambda k: web_search_settings[k].get('timestamp', 0),
748
+ reverse=True)
749
+ if recent_ids:
750
+ recent_id = recent_ids[0]
751
+ settings = web_search_settings[recent_id]
752
+ self.web_search_enabled = settings.get('enabled', False)
753
+ self.webrtc_id = recent_id
754
+ print(f"start_up: Updated settings from storage - webrtc_id={self.webrtc_id}, web_search_enabled={self.web_search_enabled}")
755
+
756
  print(f"Starting up handler with web_search_enabled={self.web_search_enabled}")
757
  self.client = openai.AsyncOpenAI()
758
 
 
894
  self.connection = None
895
 
896
 
897
+ # Create initial handler instance
898
+ handler = OpenAIHandler(web_search_enabled=False)
 
 
 
 
 
 
 
 
899
 
900
  # Create components
901
  chatbot = gr.Chatbot(type="messages")
902
 
903
+ # Create stream with handler instance
904
  stream = Stream(
905
+ handler, # Pass instance, not factory
906
  mode="send-receive",
907
  modality="audio",
908
  additional_inputs=[chatbot],
 
915
 
916
  app = FastAPI()
917
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
918
  # Mount stream
919
  stream.mount(app)
920