Benji Peng commited on
Commit
2e2cccd
·
1 Parent(s): 3adcd63

update files

Browse files
examples/call_space_gradio_client.py CHANGED
@@ -2,7 +2,10 @@ from __future__ import annotations
2
 
3
  import argparse
4
  import os
 
 
5
 
 
6
  from gradio_client import Client
7
 
8
 
@@ -16,17 +19,34 @@ def main() -> int:
16
  token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN") or None
17
  client = Client(args.space, hf_token=token)
18
 
19
- result = client.predict(
20
- {"url": args.image_url, "meta": {"_type": "gradio.FileData"}},
21
- args.prompt,
22
- 1008, # max_long_side
23
- 0.3, # score_threshold
24
- 0.5, # mask_threshold
25
- 8, # max_instances
26
- 8, # num_threads
27
- 0.5, # overlay_alpha
28
- api_name="/run_text_prompt",
29
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  overlay, detections = result
32
  print("overlay:", overlay)
@@ -36,4 +56,3 @@ def main() -> int:
36
 
37
  if __name__ == "__main__":
38
  raise SystemExit(main())
39
-
 
2
 
3
  import argparse
4
  import os
5
+ import tempfile
6
+ from pathlib import Path
7
 
8
+ import requests
9
  from gradio_client import Client
10
 
11
 
 
19
  token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN") or None
20
  client = Client(args.space, hf_token=token)
21
 
22
+ tmp_path: str | None = None
23
+ try:
24
+ # Gradio's backend does not fetch http(s) URLs for Image inputs.
25
+ # Download client-side, then let gradio_client upload the local file.
26
+ resp = requests.get(args.image_url, timeout=60)
27
+ resp.raise_for_status()
28
+ suffix = Path(args.image_url).suffix or ".jpg"
29
+ with tempfile.NamedTemporaryFile(prefix="gradio_", suffix=suffix, delete=False) as f:
30
+ f.write(resp.content)
31
+ tmp_path = f.name
32
+
33
+ result = client.predict(
34
+ tmp_path,
35
+ args.prompt,
36
+ 1008, # max_long_side
37
+ 0.3, # score_threshold
38
+ 0.5, # mask_threshold
39
+ 8, # max_instances
40
+ 8, # num_threads
41
+ 0.5, # overlay_alpha
42
+ api_name="/run_text_prompt",
43
+ )
44
+ finally:
45
+ if tmp_path:
46
+ try:
47
+ os.unlink(tmp_path)
48
+ except OSError:
49
+ pass
50
 
51
  overlay, detections = result
52
  print("overlay:", overlay)
 
56
 
57
  if __name__ == "__main__":
58
  raise SystemExit(main())
 
examples/call_space_requests.py CHANGED
@@ -1,17 +1,25 @@
1
  from __future__ import annotations
2
 
3
  import argparse
 
4
  import json
5
  import os
6
- import sys
7
  import urllib.parse
8
  from typing import Any, Dict, List, Tuple
9
 
10
  import requests
11
 
12
 
13
- def _image_data_from_url(url: str) -> Dict[str, Any]:
14
- return {"url": url, "meta": {"_type": "gradio.FileData"}}
 
 
 
 
 
 
 
 
15
 
16
 
17
  def _call_gradio_call_api(
@@ -84,7 +92,7 @@ def main() -> int:
84
  base_url=args.base.rstrip("/"),
85
  api_name=args.api_name.strip("/"),
86
  data=[
87
- _image_data_from_url(args.image_url),
88
  args.prompt,
89
  1008, # max_long_side
90
  0.3, # score_threshold
@@ -93,6 +101,7 @@ def main() -> int:
93
  8, # num_threads
94
  0.5, # overlay_alpha
95
  ],
 
96
  )
97
 
98
  overlay, detections = result
@@ -109,4 +118,3 @@ def main() -> int:
109
 
110
  if __name__ == "__main__":
111
  raise SystemExit(main())
112
-
 
1
  from __future__ import annotations
2
 
3
  import argparse
4
+ import base64
5
  import json
6
  import os
 
7
  import urllib.parse
8
  from typing import Any, Dict, List, Tuple
9
 
10
  import requests
11
 
12
 
13
+ def _image_data_from_http_url(url: str) -> Dict[str, Any]:
14
+ """
15
+ Gradio's backend image preprocessing accepts `url` only for base64 data URLs.
16
+ So we fetch the bytes client-side and send a `data:<mime>;base64,...` URL.
17
+ """
18
+ resp = requests.get(url, timeout=60)
19
+ resp.raise_for_status()
20
+ mime = (resp.headers.get("content-type") or "image/jpeg").split(";", 1)[0].strip()
21
+ b64 = base64.b64encode(resp.content).decode("utf-8")
22
+ return {"url": f"data:{mime};base64,{b64}", "meta": {"_type": "gradio.FileData"}}
23
 
24
 
25
  def _call_gradio_call_api(
 
92
  base_url=args.base.rstrip("/"),
93
  api_name=args.api_name.strip("/"),
94
  data=[
95
+ _image_data_from_http_url(args.image_url),
96
  args.prompt,
97
  1008, # max_long_side
98
  0.3, # score_threshold
 
101
  8, # num_threads
102
  0.5, # overlay_alpha
103
  ],
104
+ headers=headers,
105
  )
106
 
107
  overlay, detections = result
 
118
 
119
  if __name__ == "__main__":
120
  raise SystemExit(main())