Bibek Mukherjee
commited on
Update src/frontend/app.py
Browse files- src/frontend/app.py +19 -17
src/frontend/app.py
CHANGED
@@ -25,9 +25,26 @@ st.set_page_config(
|
|
25 |
|
26 |
# Function to start the API server
|
27 |
def run_api():
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
# Only start the server once
|
31 |
if "api_started" not in st.session_state:
|
32 |
threading.Thread(target=run_api).start()
|
33 |
st.session_state.api_started = True
|
@@ -36,21 +53,6 @@ if "api_started" not in st.session_state:
|
|
36 |
# Wait a few seconds for server to start
|
37 |
time.sleep(15)
|
38 |
|
39 |
-
# Function to check if API is running
|
40 |
-
def is_api_running():
|
41 |
-
try:
|
42 |
-
response = requests.get("http://localhost:8000/health")
|
43 |
-
return response.status_code == 200
|
44 |
-
except:
|
45 |
-
return False
|
46 |
-
|
47 |
-
# Check API status
|
48 |
-
api_status = is_api_running()
|
49 |
-
if api_status:
|
50 |
-
st.sidebar.success("✅ API is running")
|
51 |
-
else:
|
52 |
-
st.sidebar.error("❌ API is not running. Please refresh the page.")
|
53 |
-
|
54 |
# Add the project root to the Python path
|
55 |
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
|
56 |
|
|
|
25 |
|
26 |
# Function to start the API server
|
27 |
def run_api():
|
28 |
+
try:
|
29 |
+
# For Hugging Face Spaces, set environment variable
|
30 |
+
os.environ['SPACE_ID'] = 'huggingface'
|
31 |
+
|
32 |
+
# Start the API server
|
33 |
+
process = subprocess.Popen(
|
34 |
+
["python", "-m", "uvicorn", "src.api.main:app", "--host", "0.0.0.0", "--port", "8000"],
|
35 |
+
stdout=subprocess.PIPE,
|
36 |
+
stderr=subprocess.PIPE,
|
37 |
+
text=True
|
38 |
+
)
|
39 |
+
|
40 |
+
# Log any errors
|
41 |
+
for line in process.stderr:
|
42 |
+
print(f"API Error: {line.strip()}")
|
43 |
+
|
44 |
+
except Exception as e:
|
45 |
+
print(f"Error starting API: {e}")
|
46 |
|
47 |
+
# Only start the server once after page config is set
|
48 |
if "api_started" not in st.session_state:
|
49 |
threading.Thread(target=run_api).start()
|
50 |
st.session_state.api_started = True
|
|
|
53 |
# Wait a few seconds for server to start
|
54 |
time.sleep(15)
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
# Add the project root to the Python path
|
57 |
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
|
58 |
|