petter2025 commited on
Commit
5335a6e
Β·
verified Β·
1 Parent(s): 1e59b62

Update hf_demo.py

Browse files
Files changed (1) hide show
  1. hf_demo.py +19 -18
hf_demo.py CHANGED
@@ -21,7 +21,7 @@ try:
21
  from fastapi import FastAPI, HTTPException
22
  from fastapi.middleware.cors import CORSMiddleware
23
  from pydantic import BaseModel, Field
24
- import uvicorn
25
  FASTAPI_AVAILABLE = True
26
  print("βœ… FastAPI available")
27
  except ImportError as e:
@@ -201,9 +201,12 @@ if FASTAPI_AVAILABLE:
201
  evaluation = await evaluate_action(request)
202
  return evaluation
203
 
204
- def run_fastapi():
205
- """Run FastAPI server in background thread"""
206
- uvicorn.run(api_app, host="0.0.0.0", port=8000, log_level="warning")
 
 
 
207
 
208
  # ============== HUGGING FACE SPACES DETECTION ==============
209
  def is_huggingface_spaces():
@@ -1162,22 +1165,20 @@ if __name__ == "__main__":
1162
  print("🌐 Environment:", "Hugging Face Spaces" if is_huggingface_spaces() else "Local Development")
1163
  print("="*80)
1164
 
1165
- # Start FastAPI in background thread if available
1166
- if FASTAPI_AVAILABLE:
1167
- fastapi_thread = threading.Thread(target=run_fastapi, daemon=True)
1168
- fastapi_thread.start()
1169
- print("βœ… FastAPI server started on port 8000")
1170
- print(" API endpoints available at:")
1171
- print(" - GET /api/v1/config")
1172
- print(" - POST /api/v1/evaluate")
1173
- print(" - POST /api/v1/process")
1174
- print(" - POST /api/v1/simulate")
1175
-
1176
- # Create the demo
1177
  demo = create_enhanced_demo()
1178
 
1179
- # GRADIO 4.x - Simple launch configuration (PROVEN TO WORK ON HUGGING FACE SPACES)
1180
- demo.launch(
 
 
 
 
 
 
 
 
 
1181
  server_name="0.0.0.0",
1182
  server_port=7860,
1183
  share=False,
 
21
  from fastapi import FastAPI, HTTPException
22
  from fastapi.middleware.cors import CORSMiddleware
23
  from pydantic import BaseModel, Field
24
+ from gradio import mount_gradio_app
25
  FASTAPI_AVAILABLE = True
26
  print("βœ… FastAPI available")
27
  except ImportError as e:
 
201
  evaluation = await evaluate_action(request)
202
  return evaluation
203
 
204
+ print("βœ… FastAPI endpoints configured")
205
+ print(" Endpoints will be available at:")
206
+ print(" - /api/v1/config")
207
+ print(" - /api/v1/evaluate")
208
+ print(" - /api/v1/process")
209
+ print(" - /api/v1/simulate")
210
 
211
  # ============== HUGGING FACE SPACES DETECTION ==============
212
  def is_huggingface_spaces():
 
1165
  print("🌐 Environment:", "Hugging Face Spaces" if is_huggingface_spaces() else "Local Development")
1166
  print("="*80)
1167
 
1168
+ # Create the Gradio demo
 
 
 
 
 
 
 
 
 
 
 
1169
  demo = create_enhanced_demo()
1170
 
1171
+ # Mount FastAPI on the Gradio app if available
1172
+ if FASTAPI_AVAILABLE:
1173
+ print("πŸ”„ Mounting FastAPI endpoints on Gradio app...")
1174
+ app = mount_gradio_app(api_app, demo, path="/")
1175
+ print("βœ… FastAPI endpoints mounted successfully")
1176
+ print(" Access API at: https://petter2025-agentic-reliability-framework.hf.space/api/v1/...")
1177
+ else:
1178
+ app = demo
1179
+
1180
+ # Launch the combined app
1181
+ app.launch(
1182
  server_name="0.0.0.0",
1183
  server_port=7860,
1184
  share=False,