Subbu1304 commited on
Commit
26ab88d
verified
1 Parent(s): f513356

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -1,10 +1,12 @@
1
  from flask import Flask, request, jsonify
2
- from transformers import pipeline
 
3
 
4
  app = Flask(__name__)
5
 
6
- # Load Stable Diffusion model (You can use OpenAI DALL路E API instead if preferred)
7
- generator = pipeline('text-to-image', model='CompVis/stable-diffusion-v-1-4-original')
 
8
 
9
  @app.route('/generate-image', methods=['POST'])
10
  def generate_image():
@@ -12,19 +14,20 @@ def generate_image():
12
  if not description:
13
  return jsonify({'error': 'No description provided'}), 400
14
 
15
- # Generate image from description
16
- image = generator(description)
17
 
18
- # Assuming image is returned as a URL or base64-encoded string
19
- image_url = save_image(image) # Implement a function to save or serve the image
20
 
21
- return jsonify({'image_url': image_url})
22
 
23
  def save_image(image):
24
- # Save image and return the URL or path for it
25
- image_path = '/path/to/save/image.jpg'
26
- image[0].save(image_path)
27
  return image_path
28
 
29
  if __name__ == '__main__':
30
  app.run(debug=True)
 
 
1
  from flask import Flask, request, jsonify
2
+ from diffusers import StableDiffusionPipeline
3
+ import torch
4
 
5
  app = Flask(__name__)
6
 
7
+ # Load Stable Diffusion model from Hugging Face
8
+ model_id = "CompVis/stable-diffusion-v-1-4-original"
9
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
10
 
11
  @app.route('/generate-image', methods=['POST'])
12
  def generate_image():
 
14
  if not description:
15
  return jsonify({'error': 'No description provided'}), 400
16
 
17
+ # Generate image from description using Stable Diffusion
18
+ image = pipe(description).images[0]
19
 
20
+ # Save or serve the image
21
+ image_path = save_image(image) # Implement a function to save or serve the image
22
 
23
+ return jsonify({'image_url': image_path})
24
 
25
  def save_image(image):
26
+ # Save the generated image to a path
27
+ image_path = '/path/to/save/generated_image.png'
28
+ image.save(image_path)
29
  return image_path
30
 
31
  if __name__ == '__main__':
32
  app.run(debug=True)
33
+