Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,44 @@
|
|
1 |
-
import os
|
2 |
-
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
5 |
os.environ['HF_HOME'] = '/home/user/.cache/huggingface'
|
6 |
|
7 |
-
app = Flask(
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
@app.route("/
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
output_image = result.images[0]
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
output_image.
|
27 |
-
img_io.seek(0)
|
28 |
|
29 |
-
|
|
|
|
|
|
|
30 |
|
31 |
-
|
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)
|