Update myapp.py
Browse files
myapp.py
CHANGED
@@ -1,11 +1,11 @@
|
|
|
|
|
|
|
|
|
|
1 |
from flask import Flask, jsonify, request, send_file
|
2 |
from flask_cors import CORS
|
3 |
-
import torch
|
4 |
from diffusers import DiffusionPipeline
|
5 |
import numpy as np
|
6 |
-
import random
|
7 |
-
import io
|
8 |
-
from PIL import Image
|
9 |
|
10 |
# Initialize the Flask app
|
11 |
myapp = Flask(__name__)
|
@@ -13,17 +13,14 @@ CORS(myapp) # Enable CORS if needed
|
|
13 |
|
14 |
# Load the model
|
15 |
device = "cpu"
|
16 |
-
dtype = torch.float16
|
17 |
-
|
18 |
repo = "prompthero/openjourney-v4"
|
19 |
-
pipe = DiffusionPipeline.from_pretrained(repo
|
20 |
|
21 |
MAX_SEED = np.iinfo(np.int32).max
|
22 |
-
MAX_IMAGE_SIZE = 1344
|
23 |
|
24 |
-
@myapp.route('/')
|
25 |
def home():
|
26 |
-
return "Welcome to the
|
27 |
|
28 |
@myapp.route('/generate_image', methods=['POST'])
|
29 |
def generate_image():
|
@@ -34,8 +31,15 @@ def generate_image():
|
|
34 |
negative_prompt = data.get('negative_prompt', None)
|
35 |
seed = data.get('seed', 0)
|
36 |
randomize_seed = data.get('randomize_seed', True)
|
|
|
|
|
37 |
width = data.get('width', 1024)
|
38 |
height = data.get('height', 1024)
|
|
|
|
|
|
|
|
|
|
|
39 |
guidance_scale = data.get('guidance_scale', 5.0)
|
40 |
num_inference_steps = data.get('num_inference_steps', 28)
|
41 |
|
@@ -47,6 +51,7 @@ def generate_image():
|
|
47 |
generator = torch.Generator().manual_seed(seed)
|
48 |
image = pipe(
|
49 |
prompt=prompt,
|
|
|
50 |
guidance_scale=guidance_scale,
|
51 |
num_inference_steps=num_inference_steps,
|
52 |
width=width,
|
@@ -64,4 +69,5 @@ def generate_image():
|
|
64 |
|
65 |
# Add this block to make sure your app runs when called
|
66 |
if __name__ == "__main__":
|
67 |
-
|
|
|
|
1 |
+
import os
|
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 numpy as np
|
|
|
|
|
|
|
9 |
|
10 |
# Initialize the Flask app
|
11 |
myapp = Flask(__name__)
|
|
|
13 |
|
14 |
# Load the model
|
15 |
device = "cpu"
|
|
|
|
|
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 home():
|
23 |
+
return "Welcome to the Image Generation API!" # Basic home response
|
24 |
|
25 |
@myapp.route('/generate_image', methods=['POST'])
|
26 |
def generate_image():
|
|
|
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 |
|
|
|
51 |
generator = torch.Generator().manual_seed(seed)
|
52 |
image = pipe(
|
53 |
prompt=prompt,
|
54 |
+
negative_prompt=negative_prompt,
|
55 |
guidance_scale=guidance_scale,
|
56 |
num_inference_steps=num_inference_steps,
|
57 |
width=width,
|
|
|
69 |
|
70 |
# Add this block to make sure your app runs when called
|
71 |
if __name__ == "__main__":
|
72 |
+
# Run the Flask app using Gunicorn
|
73 |
+
os.system("gunicorn -w 4 -b 0.0.0.0:7860 myapp:myapp") # 4 worker processes
|