Create myapp.py
Browse files
myapp.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_file
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
import torch
|
| 5 |
+
import io
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
myapp = Flask(__name__)
|
| 9 |
+
CORS(myapp) # Enable CORS for all routes
|
| 10 |
+
|
| 11 |
+
# Initialize the Stable Diffusion pipeline using CPU
|
| 12 |
+
model_url = "https://huggingface.co/WarriorMama777/OrangeMixs/blob/main/Models/AbyssOrangeMix/AbyssOrangeMix.safetensors"
|
| 13 |
+
pipeline = StableDiffusionPipeline.from_single_file(model_url, torch_dtype=torch.float16)
|
| 14 |
+
pipeline = pipeline.to("cpu") # Set to use CPU
|
| 15 |
+
|
| 16 |
+
@myapp.route('/')
|
| 17 |
+
def home():
|
| 18 |
+
return "Stable Diffusion API is running!" # Basic message, or use render_template for HTML
|
| 19 |
+
|
| 20 |
+
@myapp.route('/generate', methods=['POST'])
|
| 21 |
+
def generate():
|
| 22 |
+
prompt = request.form.get('prompt')
|
| 23 |
+
if not prompt:
|
| 24 |
+
return jsonify({"error": "No prompt provided!"}), 400
|
| 25 |
+
|
| 26 |
+
# Generate an image based on the prompt
|
| 27 |
+
image = pipeline(prompt).images[0]
|
| 28 |
+
|
| 29 |
+
# Save the image to a bytes buffer instead of disk
|
| 30 |
+
img_io = io.BytesIO()
|
| 31 |
+
image.save(img_io, 'PNG')
|
| 32 |
+
img_io.seek(0)
|
| 33 |
+
|
| 34 |
+
# Send the image file as a response
|
| 35 |
+
return send_file(img_io, mimetype='image/png', as_attachment=True, attachment_filename='generated_image.png')
|
| 36 |
+
|
| 37 |
+
if __name__ == '__main__':
|
| 38 |
+
myapp.run(host="0.0.0.0", port=8080, debug=True)
|