Geek7 commited on
Commit
4158094
·
verified ·
1 Parent(s): 45af9e6

Update myapp.py

Browse files
Files changed (1) hide show
  1. myapp.py +8 -1
myapp.py CHANGED
@@ -1,5 +1,6 @@
1
  from flask import Flask, request, jsonify
2
  from diffusers import DiffusionPipeline
 
3
  import torch
4
  from PIL import Image
5
  import os
@@ -10,6 +11,9 @@ myapp = Flask(__name__)
10
  # Load the Diffusion pipeline
11
  pipe = DiffusionPipeline.from_pretrained("prompthero/openjourney-v4").to("cpu")
12
 
 
 
 
13
  @myapp.route('/')
14
  def index():
15
  return '''
@@ -53,12 +57,15 @@ def generate_image():
53
  data = request.json
54
  prompt = data.get('prompt', 'Astronaut in a jungle, cold color palette, muted colors, detailed, 8k')
55
 
 
 
 
56
  # Generate the image
57
  image = pipe(prompt).images[0]
58
 
59
  # Convert to PIL Image and save
60
  pil_image = Image.fromarray(image.numpy())
61
- output_path = f"{prompt.replace(' ', '_')}.png" # Create a file name based on the prompt
62
  pil_image.save(output_path)
63
 
64
  # Return the path to the generated image
 
1
  from flask import Flask, request, jsonify
2
  from diffusers import DiffusionPipeline
3
+ from transformers import AutoTokenizer
4
  import torch
5
  from PIL import Image
6
  import os
 
11
  # Load the Diffusion pipeline
12
  pipe = DiffusionPipeline.from_pretrained("prompthero/openjourney-v4").to("cpu")
13
 
14
+ # Load the tokenizer
15
+ tokenizer = AutoTokenizer.from_pretrained("prompthero/openjourney-v4", clean_up_tokenization_spaces=False)
16
+
17
  @myapp.route('/')
18
  def index():
19
  return '''
 
57
  data = request.json
58
  prompt = data.get('prompt', 'Astronaut in a jungle, cold color palette, muted colors, detailed, 8k')
59
 
60
+ # Tokenize the prompt
61
+ tokens = tokenizer(prompt, return_tensors="pt")
62
+
63
  # Generate the image
64
  image = pipe(prompt).images[0]
65
 
66
  # Convert to PIL Image and save
67
  pil_image = Image.fromarray(image.numpy())
68
+ output_path = f"{prompt.replace(' ', '_')}.png"
69
  pil_image.save(output_path)
70
 
71
  # Return the path to the generated image