Spaces:
Sleeping
Sleeping
Venkat V
commited on
Commit
Β·
4e099cb
1
Parent(s):
844dceb
fixes to urls and hf spaces description
Browse files- .hub/DESCRIPTION.yaml +4 -0
- api_backend.py +4 -1
- app.py +9 -4
- launch.py +81 -0
.hub/DESCRIPTION.yaml
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
build_and_run_command: python launch.py
|
2 |
+
repository: https://huggingface.co/spaces/username/flowchart-to-english
|
3 |
+
sdk: streamlit
|
4 |
+
app_port: 8501
|
api_backend.py
CHANGED
@@ -17,6 +17,7 @@ from PIL import Image
|
|
17 |
import io
|
18 |
import json
|
19 |
import base64
|
|
|
20 |
|
21 |
# π§ Import local processing modules
|
22 |
from yolo_module import run_yolo
|
@@ -117,4 +118,6 @@ async def process_image(
|
|
117 |
|
118 |
if __name__ == "__main__":
|
119 |
# Run the FastAPI app using Uvicorn
|
120 |
-
|
|
|
|
|
|
17 |
import io
|
18 |
import json
|
19 |
import base64
|
20 |
+
import os
|
21 |
|
22 |
# π§ Import local processing modules
|
23 |
from yolo_module import run_yolo
|
|
|
118 |
|
119 |
if __name__ == "__main__":
|
120 |
# Run the FastAPI app using Uvicorn
|
121 |
+
# Get port from environment variable or use default 7860
|
122 |
+
port = int(os.getenv("API_PORT", 7860))
|
123 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|
app.py
CHANGED
@@ -27,13 +27,18 @@ uploaded_file = st.file_uploader("Upload a flowchart image", type=["png", "jpg",
|
|
27 |
|
28 |
# Backend API URL (defaults to localhost for dev)
|
29 |
|
|
|
30 |
|
31 |
-
IS_SPACE = "SPACE_ID" in os.environ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
API_URL = "/process-image" if IS_SPACE else os.getenv("API_URL", "http://localhost:7860/process-image")
|
34 |
|
35 |
-
#API_URL = os.getenv("API_URL", "http://localhost:7860/process-image")
|
36 |
-
#API_URL = os.getenv("API_URL", "/process-image")
|
37 |
if uploaded_file:
|
38 |
# Load and resize uploaded image for preview
|
39 |
image = Image.open(uploaded_file).convert("RGB")
|
|
|
27 |
|
28 |
# Backend API URL (defaults to localhost for dev)
|
29 |
|
30 |
+
# Backend API URL handling
|
31 |
|
32 |
+
IS_SPACE = "SPACE_ID" in os.environ
|
33 |
+
|
34 |
+
# In Hugging Face Spaces, we use a relative URL path
|
35 |
+
# In local dev, we use the full URL with port
|
36 |
+
API_URL = "/process-image" if IS_SPACE else "http://localhost:7860/process-image"
|
37 |
+
|
38 |
+
# Allow override through environment variable if needed
|
39 |
+
API_URL = os.getenv("API_URL", API_URL)
|
40 |
|
|
|
41 |
|
|
|
|
|
42 |
if uploaded_file:
|
43 |
# Load and resize uploaded image for preview
|
44 |
image = Image.open(uploaded_file).convert("RGB")
|
launch.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
launch.py
|
3 |
+
|
4 |
+
Launcher script to run both FastAPI backend and Streamlit frontend.
|
5 |
+
- For local development: Starts both services on different ports
|
6 |
+
- For Hugging Face Spaces: Configured to work with the Spaces proxy
|
7 |
+
"""
|
8 |
+
|
9 |
+
import os
|
10 |
+
import subprocess
|
11 |
+
import threading
|
12 |
+
import time
|
13 |
+
import signal
|
14 |
+
import sys
|
15 |
+
|
16 |
+
# Configuration
|
17 |
+
FASTAPI_PORT = 7860
|
18 |
+
STREAMLIT_PORT = 8501
|
19 |
+
IS_SPACE = "SPACE_ID" in os.environ # Check if running on Hugging Face Spaces
|
20 |
+
|
21 |
+
# Define commands to run services
|
22 |
+
if IS_SPACE:
|
23 |
+
# On Hugging Face Spaces, we need to bind to 0.0.0.0 to allow external access
|
24 |
+
fastapi_cmd = ["python", "-m", "uvicorn", "api_backend:app", "--host", "0.0.0.0", "--port", str(FASTAPI_PORT)]
|
25 |
+
streamlit_cmd = ["streamlit", "run", "app.py", "--server.address", "0.0.0.0", "--server.port", str(STREAMLIT_PORT)]
|
26 |
+
else:
|
27 |
+
# For local development
|
28 |
+
fastapi_cmd = ["python", "-m", "uvicorn", "api_backend:app", "--port", str(FASTAPI_PORT)]
|
29 |
+
streamlit_cmd = ["streamlit", "run", "app.py", "--server.port", str(STREAMLIT_PORT)]
|
30 |
+
|
31 |
+
# Processes to track for cleanup
|
32 |
+
processes = []
|
33 |
+
|
34 |
+
# Signal handler for graceful shutdown
|
35 |
+
def handle_shutdown(signum, frame):
|
36 |
+
print("\nShutting down all services...")
|
37 |
+
for process in processes:
|
38 |
+
if process.poll() is None: # Check if process is still running
|
39 |
+
process.terminate()
|
40 |
+
sys.exit(0)
|
41 |
+
|
42 |
+
# Register signal handlers for graceful shutdown
|
43 |
+
signal.signal(signal.SIGINT, handle_shutdown)
|
44 |
+
signal.signal(signal.SIGTERM, handle_shutdown)
|
45 |
+
|
46 |
+
def run_service(cmd, service_name):
|
47 |
+
"""Run a service process and capture its output."""
|
48 |
+
print(f"Starting {service_name}...")
|
49 |
+
process = subprocess.Popen(
|
50 |
+
cmd,
|
51 |
+
stdout=subprocess.PIPE,
|
52 |
+
stderr=subprocess.STDOUT,
|
53 |
+
universal_newlines=True,
|
54 |
+
bufsize=1
|
55 |
+
)
|
56 |
+
processes.append(process)
|
57 |
+
|
58 |
+
# Print process output with service identifier
|
59 |
+
for line in process.stdout:
|
60 |
+
print(f"[{service_name}] {line}", end="")
|
61 |
+
|
62 |
+
# If we get here, the process has ended
|
63 |
+
print(f"{service_name} has stopped.")
|
64 |
+
|
65 |
+
# Main execution
|
66 |
+
if __name__ == "__main__":
|
67 |
+
print("π Starting Flowchart-to-English services")
|
68 |
+
|
69 |
+
# Start FastAPI in a separate thread
|
70 |
+
fastapi_thread = threading.Thread(
|
71 |
+
target=run_service,
|
72 |
+
args=(fastapi_cmd, "FastAPI"),
|
73 |
+
daemon=True
|
74 |
+
)
|
75 |
+
fastapi_thread.start()
|
76 |
+
|
77 |
+
# Give FastAPI a moment to start up
|
78 |
+
time.sleep(2)
|
79 |
+
|
80 |
+
# Start Streamlit (in the main thread)
|
81 |
+
run_service(streamlit_cmd, "Streamlit")
|