MozI1223 commited on
Commit
da9c720
·
1 Parent(s): 598b910

fix: ZeroGPU server binding 0.0.0.0:7860

Browse files
Files changed (1) hide show
  1. app.py +63 -14
app.py CHANGED
@@ -1,35 +1,84 @@
1
  """
2
  Alture AI — Hugging Face ZeroGPU Production Backend Entrypoint
3
  ============================================================
4
- FastAPI REST API Server mounted with Gradio ZeroGPU pipeline handler at /gradio.
5
  """
6
 
7
  import os
8
- import uvicorn
 
9
  import gradio as gr
 
 
10
 
11
  try:
12
  import spaces
13
  @spaces.GPU
14
- def zero_gpu_pipeline(resume_text: str):
15
- return "ZeroGPU Pipeline Active & Ready"
16
  except ImportError:
17
- def zero_gpu_pipeline(resume_text: str):
18
- return "CPU Pipeline Active & Ready"
19
 
20
- from deployment.backend.main import app as fastapi_app
21
 
22
- # Create Gradio demo to satisfy ZeroGPU SDK requirement
23
  with gr.Blocks(title="Alture AI Backend API") as demo:
24
  gr.Markdown("# Alture AI — Production ZeroGPU Backend Engine")
25
- gr.Markdown("FastAPI REST endpoints available at `/api/v1` for Vercel Frontend integration.")
26
  btn = gr.Button("⚡ Verify ZeroGPU Pipeline", variant="primary")
27
  out = gr.Textbox(label="Pipeline Output")
28
- btn.click(zero_gpu_pipeline, inputs=gr.Textbox(value="Sample Resume"), outputs=out)
29
 
30
- # Mount Gradio onto FastAPI app at /gradio so root FastAPI routes remain 100% clean
31
- app = gr.mount_gradio_app(fastapi_app, demo, path="/gradio")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  if __name__ == "__main__":
34
- print("🚀 Launching Alture AI ZeroGPU Engine via demo.launch()...")
35
- demo.launch()
 
1
  """
2
  Alture AI — Hugging Face ZeroGPU Production Backend Entrypoint
3
  ============================================================
4
+ Official Hugging Face ZeroGPU production runner using user's proven monkeypatching pattern.
5
  """
6
 
7
  import os
8
+ import sys
9
+ import torch
10
  import gradio as gr
11
+ from fastapi.middleware.cors import CORSMiddleware
12
+ from fastapi.responses import Response
13
 
14
  try:
15
  import spaces
16
  @spaces.GPU
17
+ def dummy_gpu_function():
18
+ return "ZeroGPU is active and ready"
19
  except ImportError:
20
+ def dummy_gpu_function():
21
+ return "Local CPU mode"
22
 
23
+ from deployment.backend.main import app as main_fastapi_app
24
 
25
+ # Create Gradio demo to satisfy ZeroGPU compiler
26
  with gr.Blocks(title="Alture AI Backend API") as demo:
27
  gr.Markdown("# Alture AI — Production ZeroGPU Backend Engine")
28
+ gr.Markdown("FastAPI REST endpoints available under `/api/v1` and `/v1`.")
29
  btn = gr.Button("⚡ Verify ZeroGPU Pipeline", variant="primary")
30
  out = gr.Textbox(label="Pipeline Output")
31
+ btn.click(dummy_gpu_function, outputs=out)
32
 
33
+ # Monkeypatch Gradio's internal FastAPI app creator (proven portfolio pattern)
34
+ original_create_app = gr.routes.App.create_app
35
+
36
+ def custom_create_app(*args, **kwargs):
37
+ app = original_create_app(*args, **kwargs)
38
+
39
+ # Configure CORS & preflight middleware
40
+ app.add_middleware(
41
+ CORSMiddleware,
42
+ allow_origins=["*"],
43
+ allow_credentials=True,
44
+ allow_methods=["*"],
45
+ allow_headers=["*"],
46
+ )
47
+
48
+ @app.middleware("http")
49
+ async def cors_preflight_middleware(request, call_next):
50
+ if request.method == "OPTIONS":
51
+ return Response(
52
+ status_code=200,
53
+ headers={
54
+ "Access-Control-Allow-Origin": "*",
55
+ "Access-Control-Allow-Methods": "*",
56
+ "Access-Control-Allow-Headers": "*",
57
+ }
58
+ )
59
+ response = await call_next(request)
60
+ response.headers["Access-Control-Allow-Origin"] = "*"
61
+ response.headers["Access-Control-Allow-Methods"] = "*"
62
+ response.headers["Access-Control-Allow-Headers"] = "*"
63
+ return response
64
+
65
+ # Inject main FastAPI app router
66
+ app.include_router(main_fastapi_app.router)
67
+
68
+ # Reorder routes so /api and /v1 routes take precedence
69
+ api_routes = []
70
+ other_routes = []
71
+ for route in app.router.routes:
72
+ path = getattr(route, 'path', '')
73
+ if path.startswith("/api") or path.startswith("/v1") or path.startswith("/health"):
74
+ api_routes.append(route)
75
+ else:
76
+ other_routes.append(route)
77
+
78
+ app.router.routes = api_routes + other_routes
79
+ return app
80
+
81
+ gr.routes.App.create_app = custom_create_app
82
 
83
  if __name__ == "__main__":
84
+ demo.launch(server_name="0.0.0.0", server_port=7860)