3v324v23 commited on
Commit
6032a52
Β·
1 Parent(s): 088f355

Fix Phase 1 Validation: Implement FastAPI Gateway on port 7860

Browse files
Files changed (3) hide show
  1. app.py +1 -1
  2. gateway.py +59 -0
  3. start.sh +7 -6
app.py CHANGED
@@ -132,7 +132,7 @@ st.markdown("""
132
  """, unsafe_allow_html=True)
133
 
134
  # ─── Constants ───────────────────────────────────────────────────────────────
135
- ENV_BASE_URL = "http://localhost:8000"
136
  TASKS = ["single-pass-review", "iterative-negotiation", "escalation-judgment"]
137
 
138
  # ─── Helper Functions ─────────────────────────────────────────────────────────
 
132
  """, unsafe_allow_html=True)
133
 
134
  # ─── Constants ───────────────────────────────────────────────────────────────
135
+ ENV_BASE_URL = "http://localhost:7860"
136
  TASKS = ["single-pass-review", "iterative-negotiation", "escalation-judgment"]
137
 
138
  # ─── Helper Functions ─────────────────────────────────────────────────────────
gateway.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import httpx
3
+ from fastapi import FastAPI, Request, Response
4
+ from fastapi.responses import StreamingResponse
5
+ from server.app import app as env_app
6
+
7
+ # The Gateway App
8
+ app = FastAPI(title="PR Review Gateway")
9
+
10
+ # 1. Mount Environment API Routes (Primary for OpenEnv Validator)
11
+ # These will be available on the main port 7860
12
+ app.include_router(env_app.router)
13
+
14
+ # 2. Proxy for Streamlit Dashboard (Internal port 8501)
15
+ STREAMLIT_URL = "http://localhost:8501"
16
+ client = httpx.AsyncClient(base_url=STREAMLIT_URL)
17
+
18
+ @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
19
+ async def proxy_to_streamlit(request: Request, path: str):
20
+ """
21
+ Catch-all route to proxy everything else to the Streamlit dashboard on port 8501.
22
+ """
23
+ url = f"{STREAMLIT_URL}/{path}"
24
+
25
+ # Forward the request headers and body
26
+ headers = dict(request.headers)
27
+ # Remove 'host' to avoid proxy confusion
28
+ headers.pop("host", None)
29
+
30
+ # Handle Streamlit's WebSocket and long-polling if needed
31
+ # (Note: For full WebSocket support, a more complex proxy is usually needed,
32
+ # but for standard Streamlit on HF, this basic forwarder often works for the UI)
33
+
34
+ # Get request body if it exists
35
+ body = await request.body()
36
+
37
+ # Send request to Streamlit
38
+ try:
39
+ response = await client.request(
40
+ method=request.method,
41
+ url=url,
42
+ headers=headers,
43
+ params=request.query_params,
44
+ content=body,
45
+ timeout=30.0
46
+ )
47
+
48
+ # Return the response back to the user
49
+ return Response(
50
+ content=response.content,
51
+ status_code=response.status_code,
52
+ headers=dict(response.headers)
53
+ )
54
+ except Exception as e:
55
+ return Response(content=f"Gateway Error: {e}", status_code=502)
56
+
57
+ if __name__ == "__main__":
58
+ import uvicorn
59
+ uvicorn.run(app, host="0.0.0.0", port=7860)
start.sh CHANGED
@@ -1,12 +1,13 @@
1
  #!/bin/bash
2
 
3
- # Start the FastAPI backend on port 8000
4
- echo "Starting FastAPI Backend on port 8000..."
5
- uvicorn server.app:app --host 0.0.0.0 --port 8000 &
6
 
7
- # Start the Streamlit dashboard on port 7860 (default HF Spaces port)
8
- echo "Starting Streamlit Dashboard on port 7860..."
9
- streamlit run app.py --server.port 7860 --server.address 0.0.0.0 &
 
10
 
11
  # Wait for all processes to finish
12
  wait
 
1
  #!/bin/bash
2
 
3
+ # Start the Streamlit dashboard internally on port 8501
4
+ echo "Starting Streamlit Dashboard on port 8501..."
5
+ streamlit run app.py --server.port 8501 --server.address 0.0.0.0 --server.headless true &
6
 
7
+ # Start the FastAPI Gateway on the primary port 7860
8
+ # This gateway handles /reset, /step, and proxies everything else to Streamlit
9
+ echo "Starting FastAPI Gateway on port 7860..."
10
+ uvicorn gateway:app --host 0.0.0.0 --port 7860 &
11
 
12
  # Wait for all processes to finish
13
  wait