Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import json
|
|
6 |
import os
|
7 |
import tempfile
|
8 |
import logging
|
|
|
9 |
from PIL import Image
|
10 |
from io import BytesIO
|
11 |
|
@@ -47,30 +48,50 @@ def download_and_split_image(url):
|
|
47 |
return [save_image(i) for i in images]
|
48 |
|
49 |
def niji_api(prompt):
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
raise ValueError("Invalid Response")
|
56 |
-
data = response.json()
|
57 |
-
message_id = data['messageId']
|
58 |
-
progress = 0
|
59 |
-
while progress < 100:
|
60 |
try:
|
61 |
-
response = requests.
|
62 |
-
response.raise_for_status()
|
63 |
except requests.exceptions.RequestException as e:
|
64 |
-
logger.
|
65 |
-
|
66 |
data = response.json()
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
with gr.Blocks() as demo:
|
76 |
gr.HTML('''
|
|
|
6 |
import os
|
7 |
import tempfile
|
8 |
import logging
|
9 |
+
import threading
|
10 |
from PIL import Image
|
11 |
from io import BytesIO
|
12 |
|
|
|
48 |
return [save_image(i) for i in images]
|
49 |
|
50 |
def niji_api(prompt):
|
51 |
+
message_id = None
|
52 |
+
images = []
|
53 |
+
|
54 |
+
def function():
|
55 |
+
nonlocal message_id, images
|
|
|
|
|
|
|
|
|
|
|
56 |
try:
|
57 |
+
response = requests.post(fetapi, headers={'Content-Type': 'application/json'}, data=json.dumps({'msg': prompt}))
|
58 |
+
response.raise_for_status() # Check for HTTP errors.
|
59 |
except requests.exceptions.RequestException as e:
|
60 |
+
logger.error(f"Failed to make POST request")
|
61 |
+
raise ValueError("Invalid Response")
|
62 |
data = response.json()
|
63 |
+
message_id = data['messageId']
|
64 |
+
|
65 |
+
progress = 0
|
66 |
+
while progress < 100:
|
67 |
+
try:
|
68 |
+
response = requests.get(f'{odnapi}/message/{message_id}', headers={'Authorization': auth_token})
|
69 |
+
response.raise_for_status()
|
70 |
+
except requests.exceptions.RequestException as e:
|
71 |
+
logger.warning(f"Failure in getting message response")
|
72 |
+
continue
|
73 |
+
|
74 |
+
data = response.json()
|
75 |
+
progress = data.get('progress', 0)
|
76 |
+
|
77 |
+
if progress_image_url:= data.get('progressImageUrl'):
|
78 |
+
images.extend([(img, f"{progress}% done") for img in download_and_split_image(progress_image_url)])
|
79 |
+
time.sleep(1)
|
80 |
+
|
81 |
+
# Process the final image urls
|
82 |
+
image_url = data['response']['imageUrl']
|
83 |
+
images.extend([(img, f"image {idx+1}/4") for idx, img in enumerate(download_and_split_image(image_url))])
|
84 |
+
|
85 |
+
t = threading.Timer(180, function=function)
|
86 |
+
t.start()
|
87 |
+
t.join()
|
88 |
+
|
89 |
+
if t.is_alive():
|
90 |
+
# Thread is still executing, raise a TimeoutError
|
91 |
+
t.cancel()
|
92 |
+
raise TimeoutError("Operation timed out after 3 minutes")
|
93 |
+
|
94 |
+
return images
|
95 |
|
96 |
with gr.Blocks() as demo:
|
97 |
gr.HTML('''
|