jiadisu commited on
Commit
8820f69
·
1 Parent(s): 3c2e331

Remove Gradio queuing, fail fast on 503

Browse files
Files changed (2) hide show
  1. api_client.py +13 -21
  2. app.py +6 -7
api_client.py CHANGED
@@ -1,7 +1,7 @@
1
  """API client for daVinci-MagiHuman WebUI.
2
 
3
- Sends generation requests to the remote Router, which load-balances
4
- across multiple backend inference servers.
5
 
6
  Configure via environment variables:
7
  ROUTER_URL Router endpoint (e.g. http://your-server:7860)
@@ -16,27 +16,11 @@ import uuid
16
 
17
  import requests
18
  from PIL import Image
19
- from requests.adapters import HTTPAdapter
20
- from urllib3.util.retry import Retry
21
 
22
  ROUTER_URL = os.environ.get("ROUTER_URL", "http://localhost:7860").rstrip("/")
23
  ROUTER_TIMEOUT = int(os.environ.get("ROUTER_TIMEOUT", "660"))
24
 
25
 
26
- def _make_session() -> requests.Session:
27
- """Create a session with automatic retry on connection/server errors."""
28
- session = requests.Session()
29
- retries = Retry(
30
- total=3,
31
- backoff_factor=1,
32
- status_forcelist=[502, 503, 504],
33
- allowed_methods=["POST"],
34
- )
35
- session.mount("http://", HTTPAdapter(max_retries=retries))
36
- session.mount("https://", HTTPAdapter(max_retries=retries))
37
- return session
38
-
39
-
40
  def _pil_to_base64(image: Image.Image) -> str:
41
  """Encode a PIL Image to a base64 string (PNG format)."""
42
  buf = io.BytesIO()
@@ -54,6 +38,8 @@ def generate(
54
  """
55
  Send a generation request to the router and download the video.
56
 
 
 
57
  Returns:
58
  dict with keys: video_path (local), seed, error
59
  """
@@ -80,8 +66,7 @@ def generate(
80
  }
81
 
82
  try:
83
- session = _make_session()
84
- resp = session.post(
85
  f"{ROUTER_URL}/generate/file",
86
  json=payload,
87
  timeout=ROUTER_TIMEOUT,
@@ -100,7 +85,14 @@ def generate(
100
  detail = e.response.json().get("detail", "")
101
  except Exception:
102
  detail = e.response.text[:200] if e.response else ""
103
- result["error"] = f"HTTP {e.response.status_code}: {detail}" if e.response else str(e)
 
 
 
 
 
 
 
104
  except Exception as e:
105
  result["error"] = str(e)
106
 
 
1
  """API client for daVinci-MagiHuman WebUI.
2
 
3
+ Sends generation requests to the remote Router.
4
+ No retry — if the router rejects (503 busy), the error is returned immediately.
5
 
6
  Configure via environment variables:
7
  ROUTER_URL Router endpoint (e.g. http://your-server:7860)
 
16
 
17
  import requests
18
  from PIL import Image
 
 
19
 
20
  ROUTER_URL = os.environ.get("ROUTER_URL", "http://localhost:7860").rstrip("/")
21
  ROUTER_TIMEOUT = int(os.environ.get("ROUTER_TIMEOUT", "660"))
22
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def _pil_to_base64(image: Image.Image) -> str:
25
  """Encode a PIL Image to a base64 string (PNG format)."""
26
  buf = io.BytesIO()
 
38
  """
39
  Send a generation request to the router and download the video.
40
 
41
+ No retry. If the router returns 503 (busy), we immediately return the error.
42
+
43
  Returns:
44
  dict with keys: video_path (local), seed, error
45
  """
 
66
  }
67
 
68
  try:
69
+ resp = requests.post(
 
70
  f"{ROUTER_URL}/generate/file",
71
  json=payload,
72
  timeout=ROUTER_TIMEOUT,
 
85
  detail = e.response.json().get("detail", "")
86
  except Exception:
87
  detail = e.response.text[:200] if e.response else ""
88
+ if e.response is not None and e.response.status_code == 503:
89
+ result["error"] = "All servers are busy. Please try again later."
90
+ else:
91
+ result["error"] = f"HTTP {e.response.status_code}: {detail}" if e.response else str(e)
92
+ except requests.ConnectionError:
93
+ result["error"] = "Cannot reach the generation server. Please try again later."
94
+ except requests.Timeout:
95
+ result["error"] = "Request timed out. The server may be overloaded."
96
  except Exception as e:
97
  result["error"] = str(e)
98
 
app.py CHANGED
@@ -1,14 +1,11 @@
1
  """daVinci-MagiHuman WebUI — Gradio frontend for HF Spaces.
2
 
3
- A lightweight frontend that sends generation requests to a remote router,
4
- which load-balances across multiple backend inference servers.
 
5
 
6
  Architecture:
7
  HF Space (this app) ──HTTP──▶ Router (public IP) ──▶ 4x inference servers
8
-
9
- Configure via HF Space secrets:
10
- ROUTER_URL e.g. http://your-server:7860
11
- ROUTER_TIMEOUT request timeout in seconds (default 660)
12
  """
13
 
14
  import os
@@ -99,7 +96,9 @@ with gr.Blocks(title=TITLE, theme=gr.themes.Soft()) as demo:
99
  )
100
 
101
  if __name__ == "__main__":
102
- demo.queue(max_size=20, default_concurrency_limit=4).launch(
 
 
103
  server_name="0.0.0.0",
104
  server_port=7860,
105
  )
 
1
  """daVinci-MagiHuman WebUI — Gradio frontend for HF Spaces.
2
 
3
+ Pure frontend, no queuing, no load management.
4
+ All requests are sent immediately to the router.
5
+ If the router rejects (503), the error is shown directly to the user.
6
 
7
  Architecture:
8
  HF Space (this app) ──HTTP──▶ Router (public IP) ──▶ 4x inference servers
 
 
 
 
9
  """
10
 
11
  import os
 
96
  )
97
 
98
  if __name__ == "__main__":
99
+ # Gradio does NOT queue or throttle — every click goes straight to the router.
100
+ # default_concurrency_limit=None removes Gradio's concurrency gate.
101
+ demo.queue(default_concurrency_limit=None).launch(
102
  server_name="0.0.0.0",
103
  server_port=7860,
104
  )