Spaces:
Sleeping
Sleeping
imseldrith
commited on
Commit
•
fa1f9f1
1
Parent(s):
c04c52a
Create tapp.py
Browse files
tapp.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, send_file, jsonify
|
2 |
+
from imaginepy import AsyncImagine #, Style, Ratio, Model
|
3 |
+
from imaginepy.constants import *
|
4 |
+
import os
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
@app.route('/')
|
9 |
+
def index():
|
10 |
+
return render_template('index.html')
|
11 |
+
|
12 |
+
@app.route('/generate', methods=['POST'])
|
13 |
+
async def generate_image():
|
14 |
+
prompt = request.form['prompt']
|
15 |
+
style = request.form['style']
|
16 |
+
ratio = request.form['ratio']
|
17 |
+
model = request.form['model']
|
18 |
+
|
19 |
+
imagine = AsyncImagine()
|
20 |
+
|
21 |
+
try:
|
22 |
+
img_data = await imagine.sdprem(
|
23 |
+
prompt=prompt,
|
24 |
+
style=Style[style],
|
25 |
+
ratio=Ratio[ratio],
|
26 |
+
seed="",
|
27 |
+
cfg=16,
|
28 |
+
model=Model[model],
|
29 |
+
asbase64=False
|
30 |
+
)
|
31 |
+
except Exception as e:
|
32 |
+
return f"An error occurred while generating the image: {e}"
|
33 |
+
|
34 |
+
if img_data is None:
|
35 |
+
return "An error occurred while generating the image."
|
36 |
+
|
37 |
+
img_data = await imagine.upscale(img_data)
|
38 |
+
|
39 |
+
if img_data is None:
|
40 |
+
return "An error occurred while upscaling the image."
|
41 |
+
|
42 |
+
try:
|
43 |
+
with open("static/example.jpeg", mode="wb") as img_file:
|
44 |
+
img_file.write(img_data)
|
45 |
+
except Exception as e:
|
46 |
+
return f"An error occurred while writing the image to file: {e}"
|
47 |
+
finally:
|
48 |
+
await imagine.close()
|
49 |
+
return render_template('output.html')
|
50 |
+
|
51 |
+
|
52 |
+
@app.route('/api/generate', methods=['POST'])
|
53 |
+
def api_generate_image():
|
54 |
+
data = request.get_json()
|
55 |
+
prompt = data['prompt']
|
56 |
+
style = data['style']
|
57 |
+
ratio = data['ratio']
|
58 |
+
model = data['model']
|
59 |
+
|
60 |
+
imagine = Imagine()
|
61 |
+
|
62 |
+
try:
|
63 |
+
img_data = imagine.sdprem(
|
64 |
+
prompt=prompt,
|
65 |
+
style=Style[style],
|
66 |
+
ratio=Ratio[ratio],
|
67 |
+
seed='',
|
68 |
+
cfg=16,
|
69 |
+
model=Model[model],
|
70 |
+
asbase64=False
|
71 |
+
)
|
72 |
+
except Exception as e:
|
73 |
+
return jsonify({'error': f"An error occurred while generating the image: {e}"}), 500
|
74 |
+
|
75 |
+
if img_data is None:
|
76 |
+
return jsonify({'error': "An error occurred while generating the image."}), 500
|
77 |
+
|
78 |
+
img_data = imagine.upscale(img_data)
|
79 |
+
|
80 |
+
if img_data is None:
|
81 |
+
return jsonify({'error': "An error occurred while upscaling the image."}), 500
|
82 |
+
|
83 |
+
try:
|
84 |
+
image_path = os.path.join(app.root_path, "generated.jpeg")
|
85 |
+
with open(image_path, mode="wb") as img_file:
|
86 |
+
img_file.write(img_data)
|
87 |
+
except Exception as e:
|
88 |
+
return jsonify({'error': f"An error occurred while writing the image to file: {e}"}), 500
|
89 |
+
|
90 |
+
finally:
|
91 |
+
await imagine.close()
|
92 |
+
|
93 |
+
return send_file(image_path, mimetype='image/jpeg', as_attachment=True)
|
94 |
+
|
95 |
+
@app.errorhandler(404)
|
96 |
+
def page_not_found(e):
|
97 |
+
# Render the 404.html template
|
98 |
+
return render_template('404.html'), 404
|
99 |
+
|
100 |
+
@app.route('/api-docs')
|
101 |
+
def api_docs():
|
102 |
+
return render_template('api_docs.html')
|
103 |
+
|
104 |
+
if __name__ == "__main__":
|
105 |
+
app.run(host="0.0.0.0", port=7860,debug=True)
|
106 |
+
|