Geek7 commited on
Commit
cccc096
·
verified ·
1 Parent(s): 4f6f053

Update myapp.py

Browse files
Files changed (1) hide show
  1. myapp.py +17 -17
myapp.py CHANGED
@@ -1,56 +1,56 @@
1
- from flask import Flask, request, send_file
2
  from flask_cors import CORS
3
  import torch
4
  from diffusers import DiffusionPipeline
5
- import io
6
  import numpy as np
7
  import random
 
 
8
 
9
  # Initialize the Flask app
10
  myapp = Flask(__name__)
11
  CORS(myapp) # Enable CORS if needed
12
 
13
  # Load the model
14
- device = "cuda" if torch.cuda.is_available() else "cpu"
 
15
 
16
- # Load the DiffusionPipeline for "prompthero/openjourney-v4"
17
- pipe = DiffusionPipeline.from_pretrained("prompthero/openjourney-v4").to(device)
18
 
19
- # Define max values for seed and image size
20
  MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 1344
22
 
23
- @app.route('/')
24
  def home():
25
- return "Welcome to the OpenJourney Image Generation API!"
26
 
27
- @app.route('/generate_image', methods=['POST'])
28
  def generate_image():
29
  data = request.json
30
 
31
  # Get inputs from request JSON
32
- prompt = data.get('prompt', 'Astronaut in a jungle, cold color palette, muted colors, detailed, 8k')
33
  negative_prompt = data.get('negative_prompt', None)
34
  seed = data.get('seed', 0)
35
  randomize_seed = data.get('randomize_seed', True)
36
  width = data.get('width', 1024)
37
  height = data.get('height', 1024)
38
- guidance_scale = data.get('guidance_scale', 7.5) # Default to a higher guidance scale for better results
39
- num_inference_steps = data.get('num_inference_steps', 50) # Default number of steps
40
-
41
  # Randomize seed if requested
42
  if randomize_seed:
43
  seed = random.randint(0, MAX_SEED)
44
-
45
  # Generate the image
46
  generator = torch.Generator().manual_seed(seed)
47
  image = pipe(
48
  prompt=prompt,
49
- negative_prompt=negative_prompt,
50
- width=width,
51
- height=height,
52
  guidance_scale=guidance_scale,
53
  num_inference_steps=num_inference_steps,
 
 
54
  generator=generator
55
  ).images[0]
56
 
 
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__)
12
  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, torch_dtype=dtype).to(device)
20
 
 
21
  MAX_SEED = np.iinfo(np.int32).max
22
  MAX_IMAGE_SIZE = 1344
23
 
24
+ @myapp.route('/') # Use 'myapp' instead of 'app'
25
  def home():
26
+ return "Welcome to the Diffusion Image Generation API!" # Basic home response
27
 
28
+ @myapp.route('/generate_image', methods=['POST'])
29
  def generate_image():
30
  data = request.json
31
 
32
  # Get inputs from request JSON
33
+ prompt = data.get('prompt', '')
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
+
42
  # Randomize seed if requested
43
  if randomize_seed:
44
  seed = random.randint(0, MAX_SEED)
45
+
46
  # Generate the 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,
53
+ height=height,
54
  generator=generator
55
  ).images[0]
56