Saiyaswanth007 commited on
Commit
2ab617a
·
1 Parent(s): bf59c71

Check point 3

Browse files
Files changed (1) hide show
  1. app.py +101 -136
app.py CHANGED
@@ -787,34 +787,30 @@ def create_interface():
787
  result = update_settings(threshold, int(max_speakers))
788
  return result
789
 
790
- def update_display():
791
- """Continuously update the conversation display"""
792
- conversation = get_conversation()
793
- status = get_status()
794
- return conversation, status
795
 
796
- # Button event bindings
 
 
 
797
  init_btn.click(
798
  fn=on_initialize,
799
- inputs=[],
800
  outputs=[status_output, start_btn, stop_btn, clear_btn]
801
  )
802
 
803
  start_btn.click(
804
  fn=on_start,
805
- inputs=[],
806
  outputs=[status_output, start_btn, stop_btn]
807
  )
808
 
809
  stop_btn.click(
810
  fn=on_stop,
811
- inputs=[],
812
  outputs=[status_output, start_btn, stop_btn]
813
  )
814
 
815
  clear_btn.click(
816
  fn=on_clear,
817
- inputs=[],
818
  outputs=[status_output]
819
  )
820
 
@@ -824,108 +820,87 @@ def create_interface():
824
  outputs=[status_output]
825
  )
826
 
827
- # Auto-refresh conversation display every 500ms
 
 
 
 
 
 
 
828
  interface.load(
829
- fn=update_display,
830
- inputs=[],
831
- outputs=[conversation_output, status_output]
832
  )
833
 
834
  return interface
835
 
836
 
837
- # FastAPI integration for FastRTC
838
- def create_fastapi_app():
839
- """Create FastAPI app with FastRTC integration"""
840
- app = FastAPI(title="Real-time Speaker Diarization API")
841
-
842
- @app.get("/")
843
- async def root():
844
- return {"message": "Real-time Speaker Diarization API"}
845
-
846
- @app.get("/status")
847
- async def api_status():
848
- try:
849
- if diarization_system.speaker_detector:
850
- status = diarization_system.speaker_detector.get_status_info()
851
- return {
852
- "initialized": True,
853
- "running": diarization_system.is_running,
854
- "current_speaker": status["current_speaker"],
855
- "active_speakers": status["active_speakers"],
856
- "max_speakers": status["max_speakers"],
857
- "last_similarity": status["last_similarity"],
858
- "threshold": status["threshold"]
859
- }
860
- else:
861
- return {"initialized": False, "running": False}
862
- except Exception as e:
863
- return {"error": str(e)}
864
-
865
- @app.get("/conversation")
866
- async def get_conversation_api():
867
- try:
868
- return {
869
- "conversation": diarization_system.get_formatted_conversation(),
870
- "sentences": len(diarization_system.full_sentences)
871
- }
872
- except Exception as e:
873
- return {"error": str(e)}
874
-
875
- @app.post("/initialize")
876
- async def initialize_api():
877
- try:
878
- result = initialize_system()
879
- return {"message": result, "success": "✅" in result}
880
- except Exception as e:
881
- return {"error": str(e), "success": False}
882
-
883
- @app.post("/start")
884
- async def start_api():
885
- try:
886
- result = start_recording()
887
- return {"message": result, "success": "🎙️" in result}
888
- except Exception as e:
889
- return {"error": str(e), "success": False}
890
-
891
- @app.post("/stop")
892
- async def stop_api():
893
- try:
894
- result = stop_recording()
895
- return {"message": result, "success": "⏹️" in result}
896
- except Exception as e:
897
- return {"error": str(e), "success": False}
898
-
899
- @app.post("/clear")
900
- async def clear_api():
901
- try:
902
- result = clear_conversation()
903
- return {"message": result, "success": True}
904
- except Exception as e:
905
- return {"error": str(e), "success": False}
906
-
907
- # FastRTC stream endpoint
908
- if audio_handler:
909
- app.add_websocket_route("/stream", Stream(audio_handler))
910
-
911
- return app
912
 
913
 
914
  # Main execution
915
  if __name__ == "__main__":
916
  import argparse
917
 
918
- parser = argparse.ArgumentParser(description='Real-time Speaker Diarization System')
919
- parser.add_argument('--mode', choices=['gradio', 'api', 'both'], default='gradio',
920
- help='Run mode: gradio interface, API only, or both')
921
- parser.add_argument('--port', type=int, default=7860,
922
- help='Port to run on (default: 7860)')
923
- parser.add_argument('--host', type=str, default='0.0.0.0',
924
- help='Host to bind to (default: 0.0.0.0)')
925
 
926
  args = parser.parse_args()
927
 
928
- if args.mode == 'gradio':
929
  # Run Gradio interface only
930
  interface = create_interface()
931
  interface.launch(
@@ -935,50 +910,40 @@ if __name__ == "__main__":
935
  show_error=True
936
  )
937
 
938
- elif args.mode == 'api':
939
  # Run FastAPI only
940
- app = create_fastapi_app()
941
- uvicorn.run(app, host=args.host, port=args.port)
 
 
 
 
942
 
943
- elif args.mode == 'both':
944
  # Run both Gradio and FastAPI
 
945
  import threading
946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
947
  # Start FastAPI in a separate thread
948
- app = create_fastapi_app()
949
- api_thread = threading.Thread(
950
- target=lambda: uvicorn.run(app, host=args.host, port=args.port + 1),
951
- daemon=True
952
- )
953
  api_thread.start()
954
 
955
- # Start Gradio interface
956
- interface = create_interface()
957
- interface.launch(
958
- server_name=args.host,
959
- server_port=args.port,
960
- share=True,
961
- show_error=True
962
- )
963
-
964
-
965
- # Additional utility functions for Hugging Face Spaces
966
- def setup_for_huggingface():
967
- """Setup function specifically for Hugging Face Spaces"""
968
- # Auto-initialize when running on HF Spaces
969
- try:
970
- if os.environ.get('SPACE_ID'): # Running on HF Spaces
971
- logger.info("Running on Hugging Face Spaces - Auto-initializing...")
972
- initialize_system()
973
- logger.info("System ready for Hugging Face Spaces!")
974
- except Exception as e:
975
- logger.error(f"HF Spaces setup error: {e}")
976
-
977
- # Call setup for HF Spaces
978
- setup_for_huggingface()
979
-
980
- # For Hugging Face Spaces, create and launch interface directly
981
- interface = create_interface()
982
-
983
- # Export the interface for HF Spaces
984
- demo = interface
 
787
  result = update_settings(threshold, int(max_speakers))
788
  return result
789
 
790
+ def refresh_conversation():
791
+ return get_conversation()
 
 
 
792
 
793
+ def refresh_status():
794
+ return get_status()
795
+
796
+ # Button click handlers
797
  init_btn.click(
798
  fn=on_initialize,
 
799
  outputs=[status_output, start_btn, stop_btn, clear_btn]
800
  )
801
 
802
  start_btn.click(
803
  fn=on_start,
 
804
  outputs=[status_output, start_btn, stop_btn]
805
  )
806
 
807
  stop_btn.click(
808
  fn=on_stop,
 
809
  outputs=[status_output, start_btn, stop_btn]
810
  )
811
 
812
  clear_btn.click(
813
  fn=on_clear,
 
814
  outputs=[status_output]
815
  )
816
 
 
820
  outputs=[status_output]
821
  )
822
 
823
+ # Auto-refresh conversation display every 1 second
824
+ interface.load(
825
+ fn=refresh_conversation,
826
+ outputs=[conversation_output],
827
+ every=1
828
+ )
829
+
830
+ # Auto-refresh status every 2 seconds
831
  interface.load(
832
+ fn=refresh_status,
833
+ outputs=[status_output],
834
+ every=2
835
  )
836
 
837
  return interface
838
 
839
 
840
+ # FastAPI setup for FastRTC integration
841
+ app = FastAPI()
842
+
843
+ @app.get("/")
844
+ async def root():
845
+ return {"message": "Real-time Speaker Diarization API"}
846
+
847
+ @app.get("/health")
848
+ async def health_check():
849
+ return {"status": "healthy", "system_running": diarization_system.is_running}
850
+
851
+ @app.post("/initialize")
852
+ async def api_initialize():
853
+ result = initialize_system()
854
+ return {"result": result, "success": "✅" in result}
855
+
856
+ @app.post("/start")
857
+ async def api_start():
858
+ result = start_recording()
859
+ return {"result": result, "success": "🎙️" in result}
860
+
861
+ @app.post("/stop")
862
+ async def api_stop():
863
+ result = stop_recording()
864
+ return {"result": result, "success": "⏹️" in result}
865
+
866
+ @app.post("/clear")
867
+ async def api_clear():
868
+ result = clear_conversation()
869
+ return {"result": result}
870
+
871
+ @app.get("/conversation")
872
+ async def api_get_conversation():
873
+ return {"conversation": get_conversation()}
874
+
875
+ @app.get("/status")
876
+ async def api_get_status():
877
+ return {"status": get_status()}
878
+
879
+ @app.post("/settings")
880
+ async def api_update_settings(threshold: float, max_speakers: int):
881
+ result = update_settings(threshold, max_speakers)
882
+ return {"result": result}
883
+
884
+ # FastRTC Stream setup
885
+ if audio_handler:
886
+ stream = Stream(handler=audio_handler)
887
+ app.include_router(stream.router, prefix="/stream")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
888
 
889
 
890
  # Main execution
891
  if __name__ == "__main__":
892
  import argparse
893
 
894
+ parser = argparse.ArgumentParser(description="Real-time Speaker Diarization System")
895
+ parser.add_argument("--mode", choices=["gradio", "api", "both"], default="gradio",
896
+ help="Run mode: gradio interface, API only, or both")
897
+ parser.add_argument("--host", default="0.0.0.0", help="Host to bind to")
898
+ parser.add_argument("--port", type=int, default=7860, help="Port to bind to")
899
+ parser.add_argument("--api-port", type=int, default=8000, help="API port (when running both)")
 
900
 
901
  args = parser.parse_args()
902
 
903
+ if args.mode == "gradio":
904
  # Run Gradio interface only
905
  interface = create_interface()
906
  interface.launch(
 
910
  show_error=True
911
  )
912
 
913
+ elif args.mode == "api":
914
  # Run FastAPI only
915
+ uvicorn.run(
916
+ app,
917
+ host=args.host,
918
+ port=args.port,
919
+ log_level="info"
920
+ )
921
 
922
+ elif args.mode == "both":
923
  # Run both Gradio and FastAPI
924
+ import multiprocessing
925
  import threading
926
 
927
+ def run_gradio():
928
+ interface = create_interface()
929
+ interface.launch(
930
+ server_name=args.host,
931
+ server_port=args.port,
932
+ share=True,
933
+ show_error=True
934
+ )
935
+
936
+ def run_fastapi():
937
+ uvicorn.run(
938
+ app,
939
+ host=args.host,
940
+ port=args.api_port,
941
+ log_level="info"
942
+ )
943
+
944
  # Start FastAPI in a separate thread
945
+ api_thread = threading.Thread(target=run_fastapi, daemon=True)
 
 
 
 
946
  api_thread.start()
947
 
948
+ # Start Gradio in main thread
949
+ run_gradio()