Update myapp.py
Browse files
myapp.py
CHANGED
@@ -1,39 +1,50 @@
|
|
1 |
from flask import Flask, request, jsonify, send_file
|
2 |
-
from flask_cors import CORS
|
3 |
-
from
|
|
|
|
|
4 |
import torch
|
5 |
import io
|
6 |
-
from PIL import Image
|
7 |
|
8 |
-
|
9 |
-
CORS(
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def home():
|
19 |
-
return "Stable Diffusion API is running!"
|
20 |
|
21 |
-
@
|
22 |
def generate():
|
23 |
prompt = request.form.get('prompt')
|
24 |
if not prompt:
|
25 |
return jsonify({"error": "No prompt provided!"}), 400
|
26 |
|
27 |
-
# Generate
|
28 |
-
|
29 |
-
|
30 |
-
# Save the image to a bytes buffer instead of disk
|
31 |
-
img_io = io.BytesIO()
|
32 |
-
image.save(img_io, 'PNG')
|
33 |
-
img_io.seek(0)
|
34 |
|
35 |
-
#
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
if __name__ == '__main__':
|
39 |
-
|
|
|
1 |
from flask import Flask, request, jsonify, send_file
|
2 |
+
from flask_cors import CORS # Import CORS
|
3 |
+
from transformers import CLIPImageProcessor
|
4 |
+
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
|
5 |
+
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
|
6 |
import torch
|
7 |
import io
|
|
|
8 |
|
9 |
+
app = Flask(__name__)
|
10 |
+
CORS(app) # Enable CORS for all routes
|
11 |
|
12 |
+
# Load the pre-trained models
|
13 |
+
repo_id = "stabilityai/stable-diffusion-2"
|
14 |
+
pipe = DiffusionPipeline.from_pretrained(
|
15 |
+
repo_id,
|
16 |
+
safety_checker=StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-safety-checker"),
|
17 |
+
feature_extractor=CLIPImageProcessor.from_pretrained("openai/clip-vit-base-patch32"),
|
18 |
+
torch_dtype=torch.float32 # Use float32 for CPU
|
19 |
+
)
|
20 |
|
21 |
+
# Set the scheduler
|
22 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
23 |
+
|
24 |
+
# Move pipeline to CPU
|
25 |
+
pipe = pipe.to("cpu")
|
26 |
+
|
27 |
+
@app.route('/')
|
28 |
def home():
|
29 |
+
return "Stable Diffusion API is running!"
|
30 |
|
31 |
+
@app.route('/generate', methods=['POST'])
|
32 |
def generate():
|
33 |
prompt = request.form.get('prompt')
|
34 |
if not prompt:
|
35 |
return jsonify({"error": "No prompt provided!"}), 400
|
36 |
|
37 |
+
# Generate the image
|
38 |
+
results = pipe(prompt, guidance_scale=9, num_inference_steps=25, num_images_per_prompt=1)
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
# Check for NSFW content
|
41 |
+
if not results.nsfw_content_detected[0]:
|
42 |
+
img_io = io.BytesIO()
|
43 |
+
results.images[0].save(img_io, format='PNG')
|
44 |
+
img_io.seek(0) # Go to the beginning of the BytesIO buffer
|
45 |
+
return send_file(img_io, mimetype='image/png', as_attachment=True, attachment_filename='generated_image.png')
|
46 |
+
else:
|
47 |
+
return jsonify({"error": "NSFW content detected!"}), 400
|
48 |
|
49 |
if __name__ == '__main__':
|
50 |
+
app.run(host="0.0.0.0", port=8080, debug=True)
|