cutiee82 commited on
Commit
e415f91
·
verified ·
1 Parent(s): 763a7cb

Create client.py

Browse files
Files changed (1) hide show
  1. client.py +49 -0
client.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import time
3
+ from io import BytesIO
4
+ from urllib.parse import quote
5
+
6
+ import requests
7
+ from PIL import Image
8
+
9
+ MAX_SEED = 2 ** 31 - 1
10
+
11
+ class Inference:
12
+ def __init__(self):
13
+ self.sesion = requests.Session()
14
+
15
+ def _fetch(self, *args, retries=5, **kwargs):
16
+ for retry in range(retries + 1):
17
+ try:
18
+ response = session.get(*args, **kwargs)
19
+ response.raise_for_status()
20
+ return response.content
21
+ except requests.RequestException:
22
+ if response.status_code in {429, 500, 502, 503, 504}:
23
+ delay = min(2 ** retry, 60)
24
+ time.sleep(delay)
25
+ else:
26
+ return None
27
+ return None
28
+
29
+ def __call__(
30
+ self,
31
+ prompt,
32
+ seed=None,
33
+ randomize_seed=True,
34
+ width=1024,
35
+ height=1024
36
+ ):
37
+ if seed is None or randomize_seed:
38
+ seed = random.randint(0, MAX_SEED)
39
+ url = f"https://image.pollinations.ai/p/{prompt}"
40
+ params = {
41
+ "prompt": prompt,
42
+ "seed": seed,
43
+ "width": width,
44
+ "height": height
45
+ }
46
+ image = self._fetch(url, params=params)
47
+ if image:
48
+ return Image.open(BytesIO(image))
49
+ return None