File size: 1,316 Bytes
2ca2110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
from diffusers import StableDiffusionPipeline
import torch
import io
from PIL import Image

myapp = Flask(__name__)
CORS(myapp)  # Enable CORS for all routes

# Initialize the Stable Diffusion pipeline using CPU
model_url = "https://huggingface.co/WarriorMama777/OrangeMixs/blob/main/Models/AbyssOrangeMix/AbyssOrangeMix.safetensors"
pipeline = StableDiffusionPipeline.from_single_file(model_url, torch_dtype=torch.float16)
pipeline = pipeline.to("cpu")  # Set to use CPU

@myapp.route('/')
def home():
    return "Stable Diffusion API is running!"  # Basic message, or use render_template for HTML

@myapp.route('/generate', methods=['POST'])
def generate():
    prompt = request.form.get('prompt')
    if not prompt:
        return jsonify({"error": "No prompt provided!"}), 400
    
    # Generate an image based on the prompt
    image = pipeline(prompt).images[0]

    # Save the image to a bytes buffer instead of disk
    img_io = io.BytesIO()
    image.save(img_io, 'PNG')
    img_io.seek(0)

    # Send the image file as a response
    return send_file(img_io, mimetype='image/png', as_attachment=True, attachment_filename='generated_image.png')

if __name__ == '__main__':
    myapp.run(host="0.0.0.0", port=8080, debug=True)