Spaces:
Runtime error
Runtime error
import os | |
from flask import Flask, request | |
import requests | |
from gradio_client import Client | |
import base64 | |
from PIL import Image | |
from io import BytesIO | |
import base64 | |
import os | |
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler | |
from diffusers.utils import load_image | |
import torch | |
import gradio as gr | |
controlnet = ControlNetModel.from_pretrained("rgres/sd-controlnet-aerialdreams", torch_dtype=torch.float16) | |
pipe = StableDiffusionControlNetPipeline.from_pretrained( | |
"stabilityai/stable-diffusion-2-1-base", controlnet=controlnet, torch_dtype=torch.float16 | |
) | |
pipe = pipe.to("cuda") | |
# CPU offloading for faster inference times | |
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config) | |
pipe.enable_model_cpu_offload() | |
app = Flask(__name__, static_url_path='/static') | |
def index(): | |
return app.send_static_file('index.html') | |
def save_base64_image(base64Image): | |
image_data = base64.b64decode(base64Image) | |
path = "input_image.jpg" | |
with open(path, 'wb') as f: | |
f.write(image_data) | |
return path | |
def encode_image_to_base64(filepath): | |
with open(filepath, "rb") as image_file: | |
encoded_image = base64.b64encode(image_file.read()).decode("utf-8") | |
return encoded_image | |
def generate_map(image, prompt, steps, seed): | |
#image = Image.open(BytesIO(base64.b64decode(image_base64))) | |
generator = torch.manual_seed(seed) | |
image = pipe( | |
prompt=prompt, | |
num_inference_steps=steps, | |
image=image | |
).images[0] | |
return image | |
def predict(): | |
data = request.get_json() | |
base64Image = data['data'][0] | |
prompt = data['data'][1] | |
steps = data['data'][2] | |
seed = data['data'][3] | |
b64meta, b64_data = base64Image.split(',') | |
image = Image.open(BytesIO(base64.b64decode(b64_data))) | |
return generate_map(image, prompt, steps, seed) | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=int( | |
os.environ.get('D2M_PORT', 8000)), debug=True) | |