Spaces:
Sleeping
Sleeping
imseldrith
commited on
Commit
·
f424b43
1
Parent(s):
6a53e9c
Update test.py
Browse files
test.py
CHANGED
@@ -1,7 +1,7 @@
|
|
|
|
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 |
|
@@ -15,33 +15,34 @@ async def generate_image():
|
|
15 |
style = request.form['style']
|
16 |
ratio = request.form['ratio']
|
17 |
|
18 |
-
imagine = AsyncImagine()
|
19 |
-
|
20 |
try:
|
|
|
|
|
21 |
img_data = await imagine.sdprem(
|
22 |
prompt=prompt,
|
23 |
style=Style[style],
|
24 |
ratio=Ratio[ratio]
|
25 |
)
|
26 |
-
except Exception as e:
|
27 |
-
return f"An error occurred while generating the image: {e}", 500
|
28 |
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
with open("static/example.jpeg", mode="wb") as img_file:
|
40 |
img_file.write(img_data)
|
|
|
|
|
|
|
41 |
except Exception as e:
|
42 |
-
return f"An error occurred
|
43 |
|
44 |
-
|
|
|
45 |
|
46 |
@app.route('/api/generate', methods=['POST'])
|
47 |
async def api_generate_image():
|
@@ -50,34 +51,34 @@ async def api_generate_image():
|
|
50 |
style = data['style']
|
51 |
ratio = data['ratio']
|
52 |
|
53 |
-
imagine = AsyncImagine()
|
54 |
-
|
55 |
try:
|
|
|
|
|
56 |
img_data = await imagine.sdprem(
|
57 |
prompt=prompt,
|
58 |
style=Style[style],
|
59 |
ratio=Ratio[ratio]
|
60 |
)
|
61 |
-
except Exception as e:
|
62 |
-
return jsonify({'error': f"An error occurred while generating the image: {e}"}), 500
|
63 |
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
|
68 |
|
69 |
-
|
70 |
-
|
71 |
|
72 |
-
imagine.close()
|
73 |
-
try:
|
74 |
image_path = os.path.join(app.root_path, "generated.jpeg")
|
75 |
with open(image_path, mode="wb") as img_file:
|
76 |
img_file.write(img_data)
|
|
|
|
|
|
|
77 |
except Exception as e:
|
78 |
-
return jsonify({'error': f"An error occurred
|
79 |
|
80 |
-
|
|
|
81 |
|
82 |
if __name__ == "__main__":
|
83 |
app.run(host="0.0.0.0", port=7860)
|
|
|
1 |
+
import asyncio
|
2 |
from flask import Flask, render_template, request, send_file, jsonify
|
3 |
from imaginepy import AsyncImagine, Style, Ratio
|
4 |
import os
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
|
|
15 |
style = request.form['style']
|
16 |
ratio = request.form['ratio']
|
17 |
|
|
|
|
|
18 |
try:
|
19 |
+
imagine = AsyncImagine()
|
20 |
+
|
21 |
img_data = await imagine.sdprem(
|
22 |
prompt=prompt,
|
23 |
style=Style[style],
|
24 |
ratio=Ratio[ratio]
|
25 |
)
|
|
|
|
|
26 |
|
27 |
+
if img_data is None:
|
28 |
+
return "An error occurred while generating the image.", 500
|
29 |
|
30 |
+
img_data = await imagine.upscale(image=img_data)
|
31 |
|
32 |
+
if img_data is None:
|
33 |
+
return "An error occurred while upscaling the image.", 500
|
34 |
+
|
35 |
+
image_path = os.path.join(app.root_path, "static", "example.jpeg")
|
36 |
+
with open(image_path, mode="wb") as img_file:
|
|
|
37 |
img_file.write(img_data)
|
38 |
+
|
39 |
+
return render_template('output.html')
|
40 |
+
|
41 |
except Exception as e:
|
42 |
+
return f"An error occurred: {e}", 500
|
43 |
|
44 |
+
finally:
|
45 |
+
await imagine.close()
|
46 |
|
47 |
@app.route('/api/generate', methods=['POST'])
|
48 |
async def api_generate_image():
|
|
|
51 |
style = data['style']
|
52 |
ratio = data['ratio']
|
53 |
|
|
|
|
|
54 |
try:
|
55 |
+
imagine = AsyncImagine()
|
56 |
+
|
57 |
img_data = await imagine.sdprem(
|
58 |
prompt=prompt,
|
59 |
style=Style[style],
|
60 |
ratio=Ratio[ratio]
|
61 |
)
|
|
|
|
|
62 |
|
63 |
+
if img_data is None:
|
64 |
+
return jsonify({'error': "An error occurred while generating the image."}), 500
|
65 |
|
66 |
+
img_data = await imagine.upscale(image=img_data)
|
67 |
|
68 |
+
if img_data is None:
|
69 |
+
return jsonify({'error': "An error occurred while upscaling the image."}), 500
|
70 |
|
|
|
|
|
71 |
image_path = os.path.join(app.root_path, "generated.jpeg")
|
72 |
with open(image_path, mode="wb") as img_file:
|
73 |
img_file.write(img_data)
|
74 |
+
|
75 |
+
return send_file(image_path, mimetype='image/jpeg', as_attachment=True)
|
76 |
+
|
77 |
except Exception as e:
|
78 |
+
return jsonify({'error': f"An error occurred: {e}"}), 500
|
79 |
|
80 |
+
finally:
|
81 |
+
await imagine.close()
|
82 |
|
83 |
if __name__ == "__main__":
|
84 |
app.run(host="0.0.0.0", port=7860)
|