mike23415 commited on
Commit
1087492
·
verified ·
1 Parent(s): 300dec4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -40
app.py CHANGED
@@ -1,40 +1,32 @@
1
- from flask import Flask, request, jsonify, send_file
2
- import torch
3
- from diffusers import DiffusionPipeline
4
- from PIL import Image
5
- import io
6
- import os
7
-
8
- app = Flask(__name__)
9
-
10
- # Load the model once when the app starts
11
- pipe = DiffusionPipeline.from_pretrained(
12
- "ashawkey/zero123-xl-diffusers",
13
- torch_dtype=torch.float32
14
- )
15
- pipe.to("cpu") # or "cuda" if you have GPU
16
-
17
- @app.route('/')
18
- def index():
19
- return jsonify({"status": "Zero123 API is running"})
20
-
21
- @app.route('/generate', methods=['POST'])
22
- def generate():
23
- if 'image' not in request.files:
24
- return jsonify({"error": "No image uploaded"}), 400
25
-
26
- file = request.files['image']
27
- input_image = Image.open(file).convert("RGB")
28
-
29
- output = pipe(input_image, num_inference_steps=75)
30
- output_image = output.images[0]
31
-
32
- # Save image to a buffer
33
- img_io = io.BytesIO()
34
- output_image.save(img_io, 'PNG')
35
- img_io.seek(0)
36
-
37
- return send_file(img_io, mimetype='image/png')
38
-
39
- if __name__ == '__main__':
40
- app.run(host='0.0.0.0', port=7860)
 
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
+