Geek7 commited on
Commit
1674dce
1 Parent(s): b31139a

Update myapp.py

Browse files
Files changed (1) hide show
  1. myapp.py +34 -23
myapp.py CHANGED
@@ -1,39 +1,50 @@
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.float32)
14
- pipeline = pipeline.to("cpu") # Set to use CPU
15
- pipeline.safety_checker = None
 
 
 
16
 
17
- @myapp.route('/')
 
 
 
 
 
 
18
  def home():
19
- return "Stable Diffusion API is running!" # Basic message, or use render_template for HTML
20
 
21
- @myapp.route('/generate', methods=['POST'])
22
  def generate():
23
  prompt = request.form.get('prompt')
24
  if not prompt:
25
  return jsonify({"error": "No prompt provided!"}), 400
26
 
27
- # Generate an image based on the prompt
28
- image = pipeline(prompt).images[0]
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
- # Send the image file as a response
36
- return send_file(img_io, mimetype='image/png', as_attachment=True, attachment_filename='generated_image.png')
 
 
 
 
 
 
37
 
38
  if __name__ == '__main__':
39
- myapp.run(host="0.0.0.0", port=8080, debug=True)
 
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)