Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -40,9 +40,19 @@ def save_image(img, suffix='.png'):
|
|
40 |
|
41 |
async def niji_api(prompt, progress=gr.Progress(), max_retries=5, backoff_factor=0.1):
|
42 |
iters = 1
|
43 |
-
progress(iters/
|
|
|
|
|
|
|
|
|
|
|
44 |
try:
|
45 |
-
response =
|
|
|
|
|
|
|
|
|
|
|
46 |
response.raise_for_status() # Check for HTTP errors.
|
47 |
except requests.exceptions.RequestException as e:
|
48 |
logger.error(f"Failed to make POST request")
|
@@ -55,7 +65,7 @@ async def niji_api(prompt, progress=gr.Progress(), max_retries=5, backoff_factor
|
|
55 |
|
56 |
def fetch_image(url):
|
57 |
try:
|
58 |
-
response = session.get(url)
|
59 |
return Image.open(BytesIO(response.content))
|
60 |
except requests.exceptions.RequestException as e:
|
61 |
logger.error(f"Failed to fetch image")
|
@@ -68,16 +78,17 @@ async def niji_api(prompt, progress=gr.Progress(), max_retries=5, backoff_factor
|
|
68 |
|
69 |
while prog < 100:
|
70 |
try:
|
71 |
-
response =
|
|
|
|
|
|
|
|
|
72 |
response.raise_for_status()
|
73 |
except requests.exceptions.RequestException as e:
|
74 |
logger.warning(f"Failure in getting message response")
|
75 |
continue
|
76 |
data = response.json()
|
77 |
prog = data.get('progress', 0)
|
78 |
-
if isinstance(prog, str) and not prog.isdigit():
|
79 |
-
prog = 0
|
80 |
-
|
81 |
if progress_image_url := data.get('progressImageUrl'):
|
82 |
iters = -100
|
83 |
yield [(img, f"{prog}% done") for img in download_and_split_image(progress_image_url)]
|
|
|
40 |
|
41 |
async def niji_api(prompt, progress=gr.Progress(), max_retries=5, backoff_factor=0.1):
|
42 |
iters = 1
|
43 |
+
progress(iters/32, desc="Sending request to MidJourney Server")
|
44 |
+
session = requests.Session() # Using Session to reuse the underlying TCP connection
|
45 |
+
retries = Retry(total=max_retries, backoff_factor=backoff_factor, status_forcelist=[429, 500, 502, 503, 504])
|
46 |
+
adapter = HTTPAdapter(max_retries=retries)
|
47 |
+
session.mount("http://", adapter)
|
48 |
+
session.mount("https://", adapter)
|
49 |
try:
|
50 |
+
response = session.post(
|
51 |
+
fetapi,
|
52 |
+
headers={'Content-Type': 'application/json'},
|
53 |
+
data=json.dumps({'msg': prompt}),
|
54 |
+
timeout=5.0 # Here, the timeout duration is set to 5 seconds
|
55 |
+
)
|
56 |
response.raise_for_status() # Check for HTTP errors.
|
57 |
except requests.exceptions.RequestException as e:
|
58 |
logger.error(f"Failed to make POST request")
|
|
|
65 |
|
66 |
def fetch_image(url):
|
67 |
try:
|
68 |
+
response = session.get(url, timeout=5.0)
|
69 |
return Image.open(BytesIO(response.content))
|
70 |
except requests.exceptions.RequestException as e:
|
71 |
logger.error(f"Failed to fetch image")
|
|
|
78 |
|
79 |
while prog < 100:
|
80 |
try:
|
81 |
+
response = session.get(
|
82 |
+
f'{odnapi}/message/{message_id}?expireMins=2',
|
83 |
+
headers={'Authorization': auth_token},
|
84 |
+
timeout=5.0
|
85 |
+
)
|
86 |
response.raise_for_status()
|
87 |
except requests.exceptions.RequestException as e:
|
88 |
logger.warning(f"Failure in getting message response")
|
89 |
continue
|
90 |
data = response.json()
|
91 |
prog = data.get('progress', 0)
|
|
|
|
|
|
|
92 |
if progress_image_url := data.get('progressImageUrl'):
|
93 |
iters = -100
|
94 |
yield [(img, f"{prog}% done") for img in download_and_split_image(progress_image_url)]
|