nyanko7 commited on
Commit
5fc53b0
1 Parent(s): 6331924

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -29,10 +29,9 @@ def save_to_s3(image_data, payload, file_name):
29
  payload_data = io.BytesIO(payload.encode('utf-8'))
30
  s3.upload_fileobj(payload_data, bucket_name, payload_key, ExtraArgs={'ContentType': 'application/json'})
31
 
32
-
33
  # Function to handle the NovelAI API request
34
- def generate_novelai_image(input_text, quality_tags, seed, negative_prompt, scale, ratio, sampler):
35
- jwt_token = os.environ.get('NAI_API_KEY')
36
  if ratio == "Landscape (1216x832)":
37
  width = 1216
38
  height = 832
@@ -92,6 +91,20 @@ def generate_novelai_image(input_text, quality_tags, seed, negative_prompt, scal
92
  }
93
  }
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  # Send the POST request
96
  response = requests.post(url, json=payload, headers=headers)
97
 
@@ -108,7 +121,7 @@ def generate_novelai_image(input_text, quality_tags, seed, negative_prompt, scal
108
  buffered = io.BytesIO()
109
  image.save(buffered, format="WEBP", quality=98)
110
  file_name = str(int(datetime.datetime.now().timestamp()))
111
- save_to_s3(buffered, json.dumps(payload, indent=4), file_name)
112
 
113
  return np.array(image), json.dumps(payload, indent=4)
114
 
@@ -117,6 +130,7 @@ def generate_novelai_image(input_text, quality_tags, seed, negative_prompt, scal
117
  else:
118
  return "The response is not a zip file.", json.dumps(payload, indent=4)
119
 
 
120
  # Create Gradio interface
121
  iface = gr.Interface(
122
  fn=generate_novelai_image,
 
29
  payload_data = io.BytesIO(payload.encode('utf-8'))
30
  s3.upload_fileobj(payload_data, bucket_name, payload_key, ExtraArgs={'ContentType': 'application/json'})
31
 
32
+ import concurrent
33
  # Function to handle the NovelAI API request
34
+ def generate_novelai_image(input_text, quality_tags, seed, negative_prompt, scale, ratio, sampler, progress=gr.Progress()):
 
35
  if ratio == "Landscape (1216x832)":
36
  width = 1216
37
  height = 832
 
91
  }
92
  }
93
 
94
+ futures = []
95
+ with concurrent.futures.ThreadPoolExecutor() as executor:
96
+ future = executor.submit(request_novelai, url, payload, headers)
97
+ futures.append(future)
98
+
99
+ for _ in progress.tqdm([None] * 15, desc="Generating"):
100
+ done, not_done = concurrent.futures.wait(futures, timeout=np.random.uniform(0.8, 1.4))
101
+ if len(not_done) == 0:
102
+ break
103
+ return futures[0].result()
104
+
105
+
106
+ def request_novelai(url, payload, headers):
107
+
108
  # Send the POST request
109
  response = requests.post(url, json=payload, headers=headers)
110
 
 
121
  buffered = io.BytesIO()
122
  image.save(buffered, format="WEBP", quality=98)
123
  file_name = str(int(datetime.datetime.now().timestamp()))
124
+ # save_to_s3(buffered, json.dumps(payload, indent=4), file_name)
125
 
126
  return np.array(image), json.dumps(payload, indent=4)
127
 
 
130
  else:
131
  return "The response is not a zip file.", json.dumps(payload, indent=4)
132
 
133
+
134
  # Create Gradio interface
135
  iface = gr.Interface(
136
  fn=generate_novelai_image,