imseldrith commited on
Commit
b36d5cd
1 Parent(s): 6eba982

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +26 -23
run.py CHANGED
@@ -1,39 +1,40 @@
1
  from flask import Flask, render_template, request, send_file, jsonify
2
  from imaginepy import AsyncImagine, Style, Ratio
3
  import os
4
- import aiohttp
5
 
6
  app = Flask(__name__)
7
 
8
  async def generate_image_async(prompt, style, ratio):
9
- async with aiohttp.ClientSession() as session:
10
- imagine = AsyncImagine()
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():
@@ -67,5 +68,7 @@ async def api_generate_image():
67
  return send_file(image_path, mimetype='image/jpeg', as_attachment=True)
68
 
69
  if __name__ == "__main__":
 
 
70
  app.run(host="0.0.0.0", port=7860)
71
 
 
1
  from flask import Flask, render_template, request, send_file, jsonify
2
  from imaginepy import AsyncImagine, Style, Ratio
3
  import os
4
+ import asyncio
5
 
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
+ await imagine.close()
 
 
 
 
 
29
 
30
+ try:
31
+ image_path = os.path.join(app.root_path, "static", "example.jpeg")
32
+ with open(image_path, mode="wb") as img_file:
33
+ img_file.write(img_data)
34
+ except Exception as e:
35
+ return f"An error occurred while writing the image to file: {e}"
36
+
37
+ return image_path
38
 
39
  @app.route('/')
40
  def index():
 
68
  return send_file(image_path, mimetype='image/jpeg', as_attachment=True)
69
 
70
  if __name__ == "__main__":
71
+ loop = asyncio.get_event_loop()
72
+ loop.run_until_complete(main())
73
  app.run(host="0.0.0.0", port=7860)
74