Subham9126 commited on
Commit
27d79e6
Β·
verified Β·
1 Parent(s): 8d9e583

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -14
app.py CHANGED
@@ -946,26 +946,46 @@ atexit.register(cleanup_session)
946
 
947
  if __name__ == "__main__":
948
  import sys
949
-
 
 
950
  # Set logging level based on environment
951
  if "--debug" in sys.argv:
952
  logging.getLogger().setLevel(logging.DEBUG)
953
  logger.info("πŸ› Debug logging enabled")
954
-
 
 
 
 
 
 
 
 
 
 
 
 
955
  if len(sys.argv) > 1 and sys.argv[1] == "api":
956
- # Run only API server
957
- print("πŸš€ Starting optimized API server...")
958
- run_api_server()
959
- elif len(sys.argv) > 1 and sys.argv[1] == "both":
960
- # Run both API and Gradio in separate threads
 
 
 
 
961
  print("πŸš€ Starting both API server and Gradio interface...")
962
- api_thread = threading.Thread(target=run_api_server, daemon=True)
 
 
 
963
  api_thread.start()
 
 
964
  print("⏳ Waiting 2 seconds for API server to start...")
965
  time.sleep(2)
966
- run_gradio_interface()
967
- else:
968
- # Default: Run only Gradio
969
- print("πŸš€ Starting optimized Gradio interface...")
970
- run_gradio_interface()
971
- run_api_server()
 
946
 
947
  if __name__ == "__main__":
948
  import sys
949
+ import threading
950
+ import time
951
+
952
  # Set logging level based on environment
953
  if "--debug" in sys.argv:
954
  logging.getLogger().setLevel(logging.DEBUG)
955
  logger.info("πŸ› Debug logging enabled")
956
+
957
+ # --- Thread-safe function to run the API server ---
958
+ def run_api():
959
+ print("πŸš€ Starting optimized API server on http://0.0.0.0:8000")
960
+ run_api_server(host="0.0.0.0", port=8000)
961
+
962
+ # --- Main function to start Gradio ---
963
+ def run_gradio():
964
+ print("πŸš€ Starting optimized Gradio interface...")
965
+ # The demo.launch() will block the main thread
966
+ run_gradio_interface()
967
+
968
+ # --- Logic to run API, Gradio, or both ---
969
  if len(sys.argv) > 1 and sys.argv[1] == "api":
970
+ # Run only the API server
971
+ run_api()
972
+
973
+ elif len(sys.argv) > 1 and sys.argv[1] == "gradio":
974
+ # Run only the Gradio interface
975
+ run_gradio()
976
+
977
+ else:
978
+ # Default behavior: Run both API and Gradio
979
  print("πŸš€ Starting both API server and Gradio interface...")
980
+
981
+ # Run the API server in a daemon thread
982
+ # A daemon thread will exit when the main program exits.
983
+ api_thread = threading.Thread(target=run_api, daemon=True)
984
  api_thread.start()
985
+
986
+ # Give the API server a moment to initialize
987
  print("⏳ Waiting 2 seconds for API server to start...")
988
  time.sleep(2)
989
+
990
+ # Run the Gradio interface in the main thread
991
+ run_gradio()