mike23415 commited on
Commit
48056a7
·
verified ·
1 Parent(s): 8375c48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -22
app.py CHANGED
@@ -1,32 +1,44 @@
1
- import os from flask import Flask, request, jsonify, send_file from diffusers import DiffusionPipeline import torch from PIL import Image import io
2
-
3
- Set Hugging Face cache directory to a writable path
4
-
 
 
 
 
5
  os.environ['HF_HOME'] = '/home/user/.cache/huggingface'
6
 
7
- app = Flask(name)
8
-
9
- Load the model on CPU
10
-
11
- pipe = DiffusionPipeline.from_pretrained( "sudo-ai/zero123plus-v1.2", torch_dtype=torch.float32 ) pipe.to("cpu")
12
 
13
- @app.route("/", methods=["GET"]) def index(): return jsonify({"message": "Zero123Plus API is running."})
 
 
 
 
 
14
 
15
- @app.route("/generate", methods=["POST"]) def generate(): if 'image' not in request.files: return jsonify({"error": "No image uploaded"}), 400
 
 
16
 
17
- image_file = request.files['image']
18
- input_image = Image.open(image_file).convert("RGB")
 
 
19
 
20
- # Generate new views
21
- result = pipe(image=input_image, num_inference_steps=25)
22
- output_image = result.images[0]
23
 
24
- # Save to a BytesIO object
25
- img_io = io.BytesIO()
26
- output_image.save(img_io, 'PNG')
27
- img_io.seek(0)
28
 
29
- return send_file(img_io, mimetype='image/png')
 
 
 
30
 
31
- if name == "main": app.run(host="0.0.0.0", port=7860)
32
 
 
 
 
1
+ import os
2
+ from flask import Flask, request, jsonify, send_file
3
+ from diffusers import DiffusionPipeline
4
+ import torch
5
+ from PIL import Image
6
+ import io
7
+
8
+ # Set Hugging Face cache directory to a writable path
9
  os.environ['HF_HOME'] = '/home/user/.cache/huggingface'
10
 
11
+ app = Flask(__name__)
 
 
 
 
12
 
13
+ # Load the model on CPU
14
+ pipe = DiffusionPipeline.from_pretrained(
15
+ "sudo-ai/zero123plus-v1.2",
16
+ torch_dtype=torch.float32
17
+ )
18
+ pipe.to("cpu")
19
 
20
+ @app.route("/", methods=["GET"])
21
+ def index():
22
+ return jsonify({"message": "Zero123Plus API is running."})
23
 
24
+ @app.route("/generate", methods=["POST"])
25
+ def generate():
26
+ if 'image' not in request.files:
27
+ return jsonify({"error": "No image uploaded"}), 400
28
 
29
+ image_file = request.files['image']
30
+ input_image = Image.open(image_file).convert("RGB")
 
31
 
32
+ # Generate new views
33
+ result = pipe(image=input_image, num_inference_steps=25)
34
+ output_image = result.images[0]
 
35
 
36
+ # Save to a BytesIO object
37
+ img_io = io.BytesIO()
38
+ output_image.save(img_io, 'PNG')
39
+ img_io.seek(0)
40
 
41
+ return send_file(img_io, mimetype='image/png')
42
 
43
+ if __name__ == "__main__":
44
+ app.run(host="0.0.0.0", port=7860)