imseldrith commited on
Commit
f1412e2
·
1 Parent(s): ed572d5

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +66 -9
run.py CHANGED
@@ -1,12 +1,69 @@
1
- import subprocess
 
2
  import os
 
3
 
4
- def setup_imaginepy():
5
- subprocess.run(["git", "clone", "https://github.com/ItsCEED/Imaginepy"])
6
- os.chdir('Imaginepy')
7
- subprocess.run("python -m venv env && source env/bin/activate && python setup.py install", shell=True)
8
- print("Done")
9
- #os.chdir("..")
10
 
11
- setup_imaginepy()
12
- subprocess.run(["python", "app.py"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ 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():
39
+ return render_template('index.html')
40
+
41
+ @app.route('/generate', methods=['POST'])
42
+ async def generate_image():
43
+ prompt = request.form['prompt']
44
+ style = request.form['style']
45
+ ratio = request.form['ratio']
46
+
47
+ image_path = await generate_image_async(prompt, style, ratio)
48
+
49
+ if isinstance(image_path, str):
50
+ return image_path
51
+
52
+ return render_template('output.html')
53
+
54
+ @app.route('/api/generate', methods=['POST'])
55
+ async def api_generate_image():
56
+ data = request.get_json()
57
+ prompt = data['prompt']
58
+ style = data['style']
59
+ ratio = data['ratio']
60
+
61
+ image_path = await generate_image_async(prompt, style, ratio)
62
+
63
+ if isinstance(image_path, str):
64
+ return jsonify({'error': image_path}), 500
65
+
66
+ return send_file(image_path, mimetype='image/jpeg', as_attachment=True)
67
+
68
+ if __name__ == "__main__":
69
+ app.run(host="0.0.0.0", port=7860)