ginnigarg commited on
Commit
743d582
·
verified ·
1 Parent(s): 82fd0e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -30
app.py CHANGED
@@ -8,10 +8,10 @@ from fastapi.middleware.cors import CORSMiddleware
8
  from fastapi.responses import StreamingResponse
9
  import httpx
10
 
11
- # Create FastAPI app first
12
- app = FastAPI(title="Argilla Server")
13
 
14
- # Add CORS middleware
15
  app.add_middleware(
16
  CORSMiddleware,
17
  allow_origins=["*"],
@@ -23,57 +23,57 @@ app.add_middleware(
23
  def run_argilla_server():
24
  """Run Argilla server in the background on port 6900"""
25
  subprocess.run([
26
- "python", "-m", "uvicorn",
27
- "argilla.server:app",
28
- "--host", "0.0.0.0",
29
  "--port", "6900"
30
  ])
31
 
32
- # Start Argilla backend in separate thread
33
  threading.Thread(target=run_argilla_server, daemon=True).start()
34
- time.sleep(5)
35
 
36
- # Initialize Argilla client (optional)
 
 
 
37
  try:
38
  rg.init(
39
- api_url="http://localhost:6900",
40
  api_key="admin.apikey",
41
  workspace="admin"
42
  )
43
- except Exception:
44
- pass
45
 
46
- # ✅ Proxy /api/* requests to Argilla server (port 6900)
47
  @app.api_route("/api/{path:path}", methods=["GET", "POST", "PUT", "PATCH", "DELETE"])
48
  async def proxy_to_argilla(request: Request, path: str):
49
  url = f"http://localhost:6900/api/{path}"
50
- async with httpx.AsyncClient() as client:
51
  resp = await client.request(
52
- request.method,
53
- url,
54
  headers={k: v for k, v in request.headers.items() if k.lower() != "host"},
55
  content=await request.body()
56
  )
57
- return StreamingResponse(resp.aiter_raw(), status_code=resp.status_code, headers=resp.headers)
 
 
58
 
59
- # Create Gradio interface
60
  with gr.Blocks() as demo:
61
  gr.Markdown("# Argilla Server + Client")
62
- status_box = gr.Textbox("Server running on port 6900", label="Status")
63
- api_info = gr.Textbox("API available at /api/health and /api/version", label="API Info")
64
 
65
- def get_server_status():
66
- return "Server is running", "API endpoints: /api/health, /api/version"
67
 
68
- status_btn = gr.Button("Check Server Status")
69
- status_output = gr.Textbox(label="Status Output")
70
- status_btn.click(
71
- fn=get_server_status,
72
- outputs=[status_box, status_output],
73
- api_name="server_status"
74
- )
75
 
76
- # Mount Gradio app at /
77
  app = gr.mount_gradio_app(app, demo, path="/")
78
 
79
  if __name__ == "__main__":
 
8
  from fastapi.responses import StreamingResponse
9
  import httpx
10
 
11
+ # Create FastAPI app
12
+ app = FastAPI(title="Argilla + Gradio Space")
13
 
14
+ # Add CORS
15
  app.add_middleware(
16
  CORSMiddleware,
17
  allow_origins=["*"],
 
23
  def run_argilla_server():
24
  """Run Argilla server in the background on port 6900"""
25
  subprocess.run([
26
+ "uvicorn",
27
+ "argilla.server:app",
28
+ "--host", "0.0.0.0",
29
  "--port", "6900"
30
  ])
31
 
32
+ # Start Argilla in background thread
33
  threading.Thread(target=run_argilla_server, daemon=True).start()
 
34
 
35
+ # Wait longer for Argilla to boot
36
+ time.sleep(15)
37
+
38
+ # Initialize Argilla client (optional, just for verification)
39
  try:
40
  rg.init(
41
+ api_url="http://localhost:6900",
42
  api_key="admin.apikey",
43
  workspace="admin"
44
  )
45
+ except Exception as e:
46
+ print("Argilla init failed:", e)
47
 
48
+ # ✅ Proxy /api/* to Argilla
49
  @app.api_route("/api/{path:path}", methods=["GET", "POST", "PUT", "PATCH", "DELETE"])
50
  async def proxy_to_argilla(request: Request, path: str):
51
  url = f"http://localhost:6900/api/{path}"
52
+ async with httpx.AsyncClient(follow_redirects=True) as client:
53
  resp = await client.request(
54
+ method=request.method,
55
+ url=url,
56
  headers={k: v for k, v in request.headers.items() if k.lower() != "host"},
57
  content=await request.body()
58
  )
59
+ # Clean up headers that break FastAPI
60
+ headers = {k: v for k, v in resp.headers.items() if k.lower() not in ["content-encoding", "transfer-encoding", "content-length"]}
61
+ return StreamingResponse(resp.aiter_raw(), status_code=resp.status_code, headers=headers)
62
 
63
+ # Gradio UI
64
  with gr.Blocks() as demo:
65
  gr.Markdown("# Argilla Server + Client")
66
+ status_box = gr.Textbox("Server proxy at /api", label="Status")
67
+ api_info = gr.Textbox("Try /api/health or /api/v1/me", label="API Info")
68
 
69
+ def get_status():
70
+ return "Server is running", "API endpoints: /api/health, /api/v1/me"
71
 
72
+ btn = gr.Button("Check Status")
73
+ out = gr.Textbox(label="Output")
74
+ btn.click(fn=get_status, outputs=[status_box, out])
 
 
 
 
75
 
76
+ # Mount Gradio UI
77
  app = gr.mount_gradio_app(app, demo, path="/")
78
 
79
  if __name__ == "__main__":