Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,68 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
import modin.pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
from diffusers import DiffusionPipeline, DDIMScheduler
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import base64
|
| 7 |
+
import io
|
| 8 |
+
import gc
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
app = Flask(__name__)
|
| 12 |
+
|
| 13 |
+
# Device set to CPU and optimize threading
|
| 14 |
+
device = "cpu"
|
| 15 |
+
torch.set_num_threads(max(1, os.cpu_count() or 4))
|
| 16 |
+
torch.set_num_interop_threads(max(1, os.cpu_count() or 4))
|
| 17 |
+
|
| 18 |
+
# Load model with accelerate and disable safety checker
|
| 19 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 20 |
+
"stabilityai/sdxl-turbo",
|
| 21 |
+
use_safetensors=True,
|
| 22 |
+
low_cpu_mem_usage=True
|
| 23 |
+
)
|
| 24 |
+
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config) # Faster scheduler
|
| 25 |
+
pipe.safety_checker = None # Disable safety checker
|
| 26 |
+
pipe = pipe.to(device)
|
| 27 |
+
pipe.unet.enable_model_cpu_offload()
|
| 28 |
+
|
| 29 |
+
def infer(prompt, steps=1, seed=0):
|
| 30 |
+
if not prompt or len(prompt.split()) > 77:
|
| 31 |
+
return "Prompt missing or exceeds 77 tokens!", 0
|
| 32 |
|
| 33 |
+
generator = torch.Generator(device=device).manual_seed(seed) if seed != 0 else torch.Generator(device=device).manual_seed(np.random.randint(0, 2**32 - 1))
|
| 34 |
+
|
| 35 |
+
with torch.no_grad():
|
| 36 |
+
image = pipe(
|
| 37 |
+
prompt=prompt,
|
| 38 |
+
num_inference_steps=steps,
|
| 39 |
+
guidance_scale=0.0,
|
| 40 |
+
height=512,
|
| 41 |
+
width=512,
|
| 42 |
+
generator=generator,
|
| 43 |
+
output_type="pil",
|
| 44 |
+
num_images_per_prompt=1
|
| 45 |
+
).images[0]
|
| 46 |
+
|
| 47 |
+
gc.collect()
|
| 48 |
+
return image, seed
|
| 49 |
+
|
| 50 |
+
@app.route('/generate', methods=['POST'])
|
| 51 |
+
def generate():
|
| 52 |
+
prompt = request.form.get('prompt')
|
| 53 |
+
steps = int(request.form.get('steps', 1))
|
| 54 |
+
seed = int(request.form.get('seed', 0))
|
| 55 |
+
|
| 56 |
+
result, seed_used = infer(prompt, steps, seed)
|
| 57 |
+
|
| 58 |
+
if isinstance(result, str):
|
| 59 |
+
return jsonify({'error': result, 'seed': seed_used}), 400
|
| 60 |
+
|
| 61 |
+
buffered = io.BytesIO()
|
| 62 |
+
result.save(buffered, format="PNG")
|
| 63 |
+
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 64 |
+
|
| 65 |
+
return jsonify({'image': img_str, 'seed': seed_used})
|
| 66 |
+
|
| 67 |
+
if __name__ == '__main__':
|
| 68 |
+
app.run(host='0.0.0.0', port=8000)
|