Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 958 |
-
|
| 959 |
-
elif len(sys.argv) > 1 and sys.argv[1] == "
|
| 960 |
-
# Run
|
|
|
|
|
|
|
|
|
|
|
|
|
| 961 |
print("π Starting both API server and Gradio interface...")
|
| 962 |
-
|
|
|
|
|
|
|
|
|
|
| 963 |
api_thread.start()
|
|
|
|
|
|
|
| 964 |
print("β³ Waiting 2 seconds for API server to start...")
|
| 965 |
time.sleep(2)
|
| 966 |
-
|
| 967 |
-
|
| 968 |
-
|
| 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()
|
|
|
|
|
|
|
|
|