Spaces:
Sleeping
Sleeping
Commit
·
2ab617a
1
Parent(s):
bf59c71
Check point 3
Browse files
app.py
CHANGED
@@ -787,34 +787,30 @@ def create_interface():
|
|
787 |
result = update_settings(threshold, int(max_speakers))
|
788 |
return result
|
789 |
|
790 |
-
def
|
791 |
-
|
792 |
-
conversation = get_conversation()
|
793 |
-
status = get_status()
|
794 |
-
return conversation, status
|
795 |
|
796 |
-
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
828 |
interface.load(
|
829 |
-
fn=
|
830 |
-
|
831 |
-
|
832 |
)
|
833 |
|
834 |
return interface
|
835 |
|
836 |
|
837 |
-
# FastAPI
|
838 |
-
|
839 |
-
|
840 |
-
|
841 |
-
|
842 |
-
|
843 |
-
|
844 |
-
|
845 |
-
|
846 |
-
|
847 |
-
|
848 |
-
|
849 |
-
|
850 |
-
|
851 |
-
|
852 |
-
|
853 |
-
|
854 |
-
|
855 |
-
|
856 |
-
|
857 |
-
|
858 |
-
|
859 |
-
|
860 |
-
|
861 |
-
|
862 |
-
|
863 |
-
|
864 |
-
|
865 |
-
|
866 |
-
|
867 |
-
|
868 |
-
|
869 |
-
|
870 |
-
|
871 |
-
|
872 |
-
|
873 |
-
|
874 |
-
|
875 |
-
|
876 |
-
|
877 |
-
|
878 |
-
|
879 |
-
|
880 |
-
|
881 |
-
|
882 |
-
|
883 |
-
|
884 |
-
|
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=
|
919 |
-
parser.add_argument(
|
920 |
-
help=
|
921 |
-
parser.add_argument(
|
922 |
-
|
923 |
-
parser.add_argument(
|
924 |
-
help='Host to bind to (default: 0.0.0.0)')
|
925 |
|
926 |
args = parser.parse_args()
|
927 |
|
928 |
-
if args.mode ==
|
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 ==
|
939 |
# Run FastAPI only
|
940 |
-
|
941 |
-
|
|
|
|
|
|
|
|
|
942 |
|
943 |
-
elif args.mode ==
|
944 |
# Run both Gradio and FastAPI
|
|
|
945 |
import threading
|
946 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
947 |
# Start FastAPI in a separate thread
|
948 |
-
|
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
|
956 |
-
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|