Geek7 commited on
Commit
a180961
·
verified ·
1 Parent(s): 19d367a

Update myapp.py

Browse files
Files changed (1) hide show
  1. myapp.py +5 -8
myapp.py CHANGED
@@ -1,19 +1,19 @@
 
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
7
 
 
 
 
8
  # Initialize the Flask app
9
  myapp = Flask(__name__)
10
 
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,15 +57,12 @@ def generate_image():
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
 
1
+ from transformers.utils import move_cache
2
  from flask import Flask, request, jsonify
3
  from diffusers import DiffusionPipeline
 
4
  import torch
5
  from PIL import Image
6
  import os
7
 
8
+ # Call the move_cache function to migrate the old cache
9
+ move_cache()
10
+
11
  # Initialize the Flask app
12
  myapp = Flask(__name__)
13
 
14
  # Load the Diffusion pipeline
15
  pipe = DiffusionPipeline.from_pretrained("prompthero/openjourney-v4").to("cpu")
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
  # Generate the image
61
  image = pipe(prompt).images[0]
62
 
63
  # Convert to PIL Image and save
64
  pil_image = Image.fromarray(image.numpy())
65
+ output_path = f"{prompt.replace(' ', '_')}.png" # Create a file name based on the prompt
66
  pil_image.save(output_path)
67
 
68
  # Return the path to the generated image