Update app.py
Browse files
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
|
12 |
-
app = FastAPI(title="Argilla
|
13 |
|
14 |
-
# Add CORS
|
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 |
-
"
|
27 |
-
"argilla.server:app",
|
28 |
-
"--host", "0.0.0.0",
|
29 |
"--port", "6900"
|
30 |
])
|
31 |
|
32 |
-
# Start Argilla
|
33 |
threading.Thread(target=run_argilla_server, daemon=True).start()
|
34 |
-
time.sleep(5)
|
35 |
|
36 |
-
#
|
|
|
|
|
|
|
37 |
try:
|
38 |
rg.init(
|
39 |
-
api_url="http://localhost:6900",
|
40 |
api_key="admin.apikey",
|
41 |
workspace="admin"
|
42 |
)
|
43 |
-
except Exception:
|
44 |
-
|
45 |
|
46 |
-
# ✅ Proxy /api/*
|
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 |
-
|
|
|
|
|
58 |
|
59 |
-
#
|
60 |
with gr.Blocks() as demo:
|
61 |
gr.Markdown("# Argilla Server + Client")
|
62 |
-
status_box = gr.Textbox("Server
|
63 |
-
api_info = gr.Textbox("
|
64 |
|
65 |
-
def
|
66 |
-
return "Server is running", "API endpoints: /api/health, /api/
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
fn=get_server_status,
|
72 |
-
outputs=[status_box, status_output],
|
73 |
-
api_name="server_status"
|
74 |
-
)
|
75 |
|
76 |
-
# Mount Gradio
|
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__":
|