doublelotus commited on
Commit
5091b36
·
1 Parent(s): 1ff8dbd
Files changed (2) hide show
  1. main.py +43 -10
  2. requirements.txt +5 -1
main.py CHANGED
@@ -1,7 +1,11 @@
1
  import os
2
- from flask import Flask, jsonify, request
 
 
3
  from flask_cors import CORS
4
- from transformers import pipeline
 
 
5
 
6
  # Set the cache directory to a location with write permissions
7
  os.environ['TRANSFORMERS_CACHE'] = './transformers_cache'
@@ -10,20 +14,49 @@ os.environ['HF_HOME'] = './transformers_cache'
10
  app = Flask(__name__)
11
  CORS(app)
12
 
13
- # Load the sentiment-analysis pipeline from Hugging Face
14
- sentiment_analyzer = pipeline('sentiment-analysis')
 
 
 
15
 
16
  @app.route('/')
17
  def hello():
18
  return {"Goes Wrong": "Keeping it real"}
19
 
20
- @app.route('/analyze', methods=['GET'])
21
- def analyze():
22
- sentence = request.args.get('sentence', default='hey there', type=str)
23
- result = sentiment_analyzer(sentence)
24
- print(result)
25
- return jsonify(result)
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  if __name__ == '__main__':
29
  app.run(debug=True)
 
1
  import os
2
+ import io
3
+ import torch
4
+ from flask import Flask, request, jsonify, send_file
5
  from flask_cors import CORS
6
+ from diffusers import AutoPipelineForImage2Image
7
+ from diffusers.utils import load_image, make_image_grid
8
+ from PIL import Image
9
 
10
  # Set the cache directory to a location with write permissions
11
  os.environ['TRANSFORMERS_CACHE'] = './transformers_cache'
 
14
  app = Flask(__name__)
15
  CORS(app)
16
 
17
+ # Load the image-to-image pipeline from Hugging Face
18
+ pipe = AutoPipelineForImage2Image.from_pretrained("RunDiffusion/Juggernaut-X-v10", torch_dtype=torch.float16).to("cuda")
19
+ pipe.enable_xformers_memory_efficient_attention()
20
+ pipe.enable_vae_tiling() # Improve performance on large images
21
+ pipe.enable_vae_slicing() # Improve performance on large batches
22
 
23
  @app.route('/')
24
  def hello():
25
  return {"Goes Wrong": "Keeping it real"}
26
 
27
+ @app.route('/generate', methods=['POST'])
28
+ def generate():
29
+ if 'image' not in request.files:
30
+ return jsonify({"error": "No image file provided"}), 400
 
 
31
 
32
+ image_file = request.files['image']
33
+ prompt = request.form.get('prompt', 'fleece hoodie, front zip, abstract pattern, GAP logo, high quality, photo')
34
+ negative_prompt = request.form.get('negative_prompt', 'low quality, bad quality, sketches, hanger')
35
+ guidance_scale = float(request.form.get('guidance_scale', 7))
36
+ num_images = int(request.form.get('num_images', 2))
37
+
38
+ sketch = Image.open(image_file)
39
+
40
+ with torch.inference_mode():
41
+ images = pipe(
42
+ prompt=prompt,
43
+ negative_prompt=negative_prompt,
44
+ image=sketch,
45
+ num_inference_steps=35,
46
+ guidance_scale=guidance_scale,
47
+ strength=0.5,
48
+ generator=torch.manual_seed(69),
49
+ num_images_per_prompt=num_images,
50
+ ).images
51
+
52
+ grid = make_image_grid(images, rows=1, cols=num_images)
53
+
54
+ # Save the generated grid to a BytesIO object
55
+ img_byte_arr = io.BytesIO()
56
+ grid.save(img_byte_arr, format='PNG')
57
+ img_byte_arr.seek(0)
58
+
59
+ return send_file(img_byte_arr, mimetype='image/png')
60
 
61
  if __name__ == '__main__':
62
  app.run(debug=True)
requirements.txt CHANGED
@@ -14,4 +14,8 @@ onnxruntime
14
  onnx
15
  pycocotools
16
  transformers
17
- torch
 
 
 
 
 
14
  onnx
15
  pycocotools
16
  transformers
17
+ torch
18
+ diffusers
19
+ PIL
20
+ cv2
21
+ numpy