Spaces:
Paused
Paused
Commit ·
86b64a1
1
Parent(s): b22c3ed
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, send_file, jsonify
|
| 2 |
+
from imaginepy import Imagine, Style, Ratio
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
@app.route('/')
|
| 7 |
+
def index():
|
| 8 |
+
return render_template('index.html')
|
| 9 |
+
|
| 10 |
+
@app.route('/generate', methods=['POST'])
|
| 11 |
+
def generate_image():
|
| 12 |
+
prompt = request.form['prompt']
|
| 13 |
+
style = request.form['style']
|
| 14 |
+
ratio = request.form['ratio']
|
| 15 |
+
|
| 16 |
+
imagine = Imagine()
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
img_data = imagine.sdprem(
|
| 20 |
+
prompt=prompt,
|
| 21 |
+
style=Style[style],
|
| 22 |
+
ratio=Ratio[ratio]
|
| 23 |
+
)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"An error occurred while generating the image: {e}"
|
| 26 |
+
|
| 27 |
+
if img_data is None:
|
| 28 |
+
return "An error occurred while generating the image."
|
| 29 |
+
|
| 30 |
+
img_data = imagine.upscale(image=img_data)
|
| 31 |
+
|
| 32 |
+
if img_data is None:
|
| 33 |
+
return "An error occurred while upscaling the image."
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
with open("static/example.jpeg", mode="wb") as img_file:
|
| 37 |
+
img_file.write(img_data)
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"An error occurred while writing the image to file: {e}"
|
| 40 |
+
|
| 41 |
+
return render_template('output.html')
|
| 42 |
+
|
| 43 |
+
@app.route('/api/generate', methods=['POST'])
|
| 44 |
+
def api_generate_image():
|
| 45 |
+
data = request.get_json()
|
| 46 |
+
prompt = data['prompt']
|
| 47 |
+
style = data['style']
|
| 48 |
+
ratio = data['ratio']
|
| 49 |
+
|
| 50 |
+
imagine = Imagine()
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
img_data = imagine.sdprem(
|
| 54 |
+
prompt=prompt,
|
| 55 |
+
style=Style[style],
|
| 56 |
+
ratio=Ratio[ratio]
|
| 57 |
+
)
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return jsonify({'error': f"An error occurred while generating the image: {e}"}), 500
|
| 60 |
+
|
| 61 |
+
if img_data is None:
|
| 62 |
+
return jsonify({'error': "An error occurred while generating the image."}), 500
|
| 63 |
+
|
| 64 |
+
img_data = imagine.upscale(image=img_data)
|
| 65 |
+
|
| 66 |
+
if img_data is None:
|
| 67 |
+
return jsonify({'error': "An error occurred while upscaling the image."}), 500
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
with open("static/example.jpeg", mode="wb") as img_file:
|
| 71 |
+
img_file.write(img_data)
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return jsonify({'error': f"An error occurred while writing the image to file: {e}"}), 500
|
| 74 |
+
|
| 75 |
+
return jsonify({'success': True})
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
app.run(host="0.0.0.0",port=7860)
|