Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|