imseldrith commited on
Commit
866f513
·
1 Parent(s): e926682

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +23 -21
run.py CHANGED
@@ -6,33 +6,34 @@ import aiohttp
6
  app = Flask(__name__)
7
 
8
  async def generate_image_async(prompt, style, ratio):
9
- imagine = AsyncImagine()
 
10
 
11
- try:
12
- img_data = await imagine.sdprem(
13
- prompt=prompt,
14
- style=Style[style],
15
- ratio=Ratio[ratio]
16
- )
17
- except Exception as e:
18
- return f"An error occurred while generating the image: {e}"
19
 
20
- if img_data is None:
21
- return "An error occurred while generating the image."
22
 
23
- img_data = await imagine.upscale(image=img_data)
24
 
25
- if img_data is None:
26
- return "An error occurred while upscaling the image."
27
 
28
- try:
29
- image_path = os.path.join(app.root_path, "static", "example.jpeg")
30
- with open(image_path, mode="wb") as img_file:
31
- img_file.write(img_data)
32
- except Exception as e:
33
- return f"An error occurred while writing the image to file: {e}"
34
 
35
- return image_path
36
 
37
  @app.route('/')
38
  def index():
@@ -67,3 +68,4 @@ async def api_generate_image():
67
 
68
  if __name__ == "__main__":
69
  app.run(host="0.0.0.0", port=7860)
 
 
6
  app = Flask(__name__)
7
 
8
  async def generate_image_async(prompt, style, ratio):
9
+ async with aiohttp.ClientSession() as session:
10
+ imagine = AsyncImagine(session=session)
11
 
12
+ try:
13
+ img_data = await imagine.sdprem(
14
+ prompt=prompt,
15
+ style=Style[style],
16
+ ratio=Ratio[ratio]
17
+ )
18
+ except Exception as e:
19
+ return f"An error occurred while generating the image: {e}"
20
 
21
+ if img_data is None:
22
+ return "An error occurred while generating the image."
23
 
24
+ img_data = await imagine.upscale(image=img_data)
25
 
26
+ if img_data is None:
27
+ return "An error occurred while upscaling the image."
28
 
29
+ try:
30
+ image_path = os.path.join(app.root_path, "static", "example.jpeg")
31
+ with open(image_path, mode="wb") as img_file:
32
+ img_file.write(img_data)
33
+ except Exception as e:
34
+ return f"An error occurred while writing the image to file: {e}"
35
 
36
+ return image_path
37
 
38
  @app.route('/')
39
  def index():
 
68
 
69
  if __name__ == "__main__":
70
  app.run(host="0.0.0.0", port=7860)
71
+