Update myapp.py
Browse files
myapp.py
CHANGED
|
@@ -1,72 +1,35 @@
|
|
| 1 |
-
import
|
| 2 |
-
import io
|
| 3 |
-
import random
|
| 4 |
-
import torch
|
| 5 |
-
from flask import Flask, jsonify, request, send_file
|
| 6 |
-
from flask_cors import CORS
|
| 7 |
from diffusers import DiffusionPipeline
|
| 8 |
-
import
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Initialize the Flask app
|
| 11 |
myapp = Flask(__name__)
|
| 12 |
-
CORS(myapp) # Enable CORS if needed
|
| 13 |
|
| 14 |
-
# Load the
|
| 15 |
-
|
| 16 |
-
repo = "prompthero/openjourney-v4"
|
| 17 |
-
pipe = DiffusionPipeline.from_pretrained(repo).to(device)
|
| 18 |
-
|
| 19 |
-
MAX_SEED = np.iinfo(np.int32).max
|
| 20 |
|
| 21 |
@myapp.route('/')
|
| 22 |
-
def
|
| 23 |
-
return "Welcome to the Image Generation API!"
|
| 24 |
|
| 25 |
@myapp.route('/generate_image', methods=['POST'])
|
| 26 |
def generate_image():
|
| 27 |
data = request.json
|
| 28 |
-
|
| 29 |
-
# Get inputs from request JSON
|
| 30 |
-
prompt = data.get('prompt', '')
|
| 31 |
-
negative_prompt = data.get('negative_prompt', None)
|
| 32 |
-
seed = data.get('seed', 0)
|
| 33 |
-
randomize_seed = data.get('randomize_seed', True)
|
| 34 |
-
|
| 35 |
-
# Get width and height and ensure they are divisible by 8
|
| 36 |
-
width = data.get('width', 1024)
|
| 37 |
-
height = data.get('height', 1024)
|
| 38 |
-
|
| 39 |
-
# Round width and height to the nearest multiple of 8
|
| 40 |
-
width = (width // 8) * 8
|
| 41 |
-
height = (height // 8) * 8
|
| 42 |
|
| 43 |
-
guidance_scale = data.get('guidance_scale', 5.0)
|
| 44 |
-
num_inference_steps = data.get('num_inference_steps', 28)
|
| 45 |
-
|
| 46 |
-
# Randomize seed if requested
|
| 47 |
-
if randomize_seed:
|
| 48 |
-
seed = random.randint(0, MAX_SEED)
|
| 49 |
-
|
| 50 |
# Generate the image
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
).images[0]
|
| 61 |
-
|
| 62 |
-
# Save the image to a byte array
|
| 63 |
-
img_byte_arr = io.BytesIO()
|
| 64 |
-
image.save(img_byte_arr, format='PNG')
|
| 65 |
-
img_byte_arr.seek(0) # Move the pointer to the start of the byte array
|
| 66 |
-
|
| 67 |
-
# Return the image as a response
|
| 68 |
-
return send_file(img_byte_arr, mimetype='image/png')
|
| 69 |
|
| 70 |
-
# Add this block to make sure your app runs when called
|
| 71 |
if __name__ == "__main__":
|
| 72 |
-
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from diffusers import DiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
# Initialize the Flask app
|
| 8 |
myapp = Flask(__name__)
|
|
|
|
| 9 |
|
| 10 |
+
# Load the Diffusion pipeline
|
| 11 |
+
pipe = DiffusionPipeline.from_pretrained("Yntec/PicXReal").to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@myapp.route('/')
|
| 14 |
+
def index():
|
| 15 |
+
return "Welcome to the Image Generation API!"
|
| 16 |
|
| 17 |
@myapp.route('/generate_image', methods=['POST'])
|
| 18 |
def generate_image():
|
| 19 |
data = request.json
|
| 20 |
+
prompt = data.get('prompt', 'Astronaut in a jungle, cold color palette, muted colors, detailed, 8k')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Generate the image
|
| 23 |
+
image = pipe(prompt).images[0]
|
| 24 |
+
|
| 25 |
+
# Convert to PIL Image and save
|
| 26 |
+
pil_image = Image.fromarray(image.numpy())
|
| 27 |
+
output_path = f"{prompt.replace(' ', '_')}.png" # Create a file name based on the prompt
|
| 28 |
+
pil_image.save(output_path)
|
| 29 |
+
|
| 30 |
+
# Return the path to the generated image
|
| 31 |
+
return jsonify({'image_path': output_path})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
|
|
|
| 33 |
if __name__ == "__main__":
|
| 34 |
+
# Set the host and port
|
| 35 |
+
myapp.run(host='0.0.0.0', port=7860)
|