crabbly commited on
Commit
b179032
·
verified ·
1 Parent(s): 4dd0a2d

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +74 -0
main.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ from fastapi import FastAPI, UploadFile, File, Form, Query, Request
4
+ from fastapi.middleware.cors import CORSMiddleware
5
+ from fastapi.concurrency import run_in_threadpool # <--- FIX: Added Threadpool
6
+ import uvicorn
7
+
8
+ app = FastAPI()
9
+
10
+ app.add_middleware(
11
+ CORSMiddleware,
12
+ allow_origins=["*"],
13
+ allow_credentials=True,
14
+ allow_methods=["*"],
15
+ allow_headers=["*"],
16
+ )
17
+
18
+ HF_TOKEN = os.getenv("HF_TOKEN")
19
+
20
+ DEV_URL = "https://PPAL-SongLab-UGA-fruit-analyzer-dev.hf.space"
21
+ PROD_URL = "https://PPAL-SongLab-UGA-fruit-analyzer.hf.space"
22
+
23
+ @app.get("/")
24
+ def read_root():
25
+ return {"status": "Render Proxy is awake!"}
26
+
27
+ @app.get("/proxy_status")
28
+ async def proxy_status(username: str = Query("")):
29
+ base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
30
+ status_url = f"{base_url}/queue_status"
31
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
32
+
33
+ # FIX: Define the blocking request
34
+ def fetch_status():
35
+ return requests.get(status_url, headers=headers, timeout=5)
36
+
37
+ try:
38
+ # FIX: Run it in a background thread so the server doesn't freeze
39
+ response = await run_in_threadpool(fetch_status)
40
+ response.raise_for_status()
41
+ return response.json()
42
+ except Exception as e:
43
+ return {"active_requests": 0, "max_concurrent": 2}
44
+
45
+
46
+ @app.post("/proxy_process")
47
+ async def proxy_process(request: Request, file: UploadFile = File(...), password: str = Form(""), username: str = Form("")):
48
+ base_url = DEV_URL if username.strip().lower() == "devtest" else PROD_URL
49
+ target_url = f"{base_url}/process_single"
50
+
51
+ query_str = request.url.query
52
+ if query_str: target_url += f"?{query_str}"
53
+
54
+ file_bytes = await file.read()
55
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
56
+ files = {"file": (file.filename, file_bytes, file.content_type)}
57
+ data = {"password": password, "username": username}
58
+
59
+ # FIX: Define the blocking request
60
+ def make_post():
61
+ return requests.post(target_url, headers=headers, files=files, data=data, timeout=120)
62
+
63
+ try:
64
+ # FIX: Run it in a background thread so other users can check the status!
65
+ response = await run_in_threadpool(make_post)
66
+ response.raise_for_status()
67
+ return response.json()
68
+ except requests.exceptions.RequestException as e:
69
+ return {"success": False, "message": f"Proxy Error (Hugging Face): {str(e)}"}
70
+ except Exception as e:
71
+ return {"success": False, "message": f"Proxy Error (Internal): {str(e)}"}
72
+
73
+ if __name__ == "__main__":
74
+ uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 7860)))