Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,16 +4,36 @@ import requests
|
|
| 4 |
from gradio_client import Client
|
| 5 |
import base64
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
app = Flask(__name__, static_url_path='/static')
|
| 12 |
|
|
|
|
| 13 |
@app.route('/')
|
| 14 |
def index():
|
| 15 |
return app.send_static_file('index.html')
|
| 16 |
|
|
|
|
| 17 |
def save_base64_image(base64Image):
|
| 18 |
image_data = base64.b64decode(base64Image)
|
| 19 |
path = "input_image.jpg"
|
|
@@ -21,11 +41,28 @@ def save_base64_image(base64Image):
|
|
| 21 |
f.write(image_data)
|
| 22 |
return path
|
| 23 |
|
|
|
|
| 24 |
def encode_image_to_base64(filepath):
|
| 25 |
with open(filepath, "rb") as image_file:
|
| 26 |
encoded_image = base64.b64encode(image_file.read()).decode("utf-8")
|
| 27 |
return encoded_image
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
@app.route('/predict', methods=['POST'])
|
| 30 |
def predict():
|
| 31 |
global client
|
|
@@ -40,14 +77,9 @@ def predict():
|
|
| 40 |
seed = data['data'][3]
|
| 41 |
|
| 42 |
b64meta, b64_data = base64Image.split(',')
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
result = client.predict(
|
| 47 |
-
image_path, prompt, steps, seed, fn_index=0
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
return b64meta + ',' + encode_image_to_base64(result)
|
| 51 |
|
| 52 |
|
| 53 |
if __name__ == '__main__':
|
|
|
|
| 4 |
from gradio_client import Client
|
| 5 |
import base64
|
| 6 |
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
import base64
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
|
| 13 |
+
from diffusers.utils import load_image
|
| 14 |
+
import torch
|
| 15 |
+
|
| 16 |
+
import gradio as gr
|
| 17 |
+
|
| 18 |
+
controlnet = ControlNetModel.from_pretrained("rgres/sd-controlnet-aerialdreams", torch_dtype=torch.float16)
|
| 19 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 20 |
+
"stabilityai/stable-diffusion-2-1-base", controlnet=controlnet, torch_dtype=torch.float16
|
| 21 |
+
)
|
| 22 |
|
| 23 |
+
pipe = pipe.to("cuda")
|
| 24 |
+
|
| 25 |
+
# CPU offloading for faster inference times
|
| 26 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
| 27 |
+
pipe.enable_model_cpu_offload()
|
| 28 |
|
| 29 |
app = Flask(__name__, static_url_path='/static')
|
| 30 |
|
| 31 |
+
|
| 32 |
@app.route('/')
|
| 33 |
def index():
|
| 34 |
return app.send_static_file('index.html')
|
| 35 |
|
| 36 |
+
|
| 37 |
def save_base64_image(base64Image):
|
| 38 |
image_data = base64.b64decode(base64Image)
|
| 39 |
path = "input_image.jpg"
|
|
|
|
| 41 |
f.write(image_data)
|
| 42 |
return path
|
| 43 |
|
| 44 |
+
|
| 45 |
def encode_image_to_base64(filepath):
|
| 46 |
with open(filepath, "rb") as image_file:
|
| 47 |
encoded_image = base64.b64encode(image_file.read()).decode("utf-8")
|
| 48 |
return encoded_image
|
| 49 |
|
| 50 |
+
|
| 51 |
+
def generate_map(image, prompt, steps, seed):
|
| 52 |
+
#image = Image.open(BytesIO(base64.b64decode(image_base64)))
|
| 53 |
+
generator = torch.manual_seed(seed)
|
| 54 |
+
|
| 55 |
+
image = Image.fromarray(image)
|
| 56 |
+
|
| 57 |
+
image = pipe(
|
| 58 |
+
prompt=prompt,
|
| 59 |
+
num_inference_steps=steps,
|
| 60 |
+
image=image
|
| 61 |
+
).images[0]
|
| 62 |
+
|
| 63 |
+
return image
|
| 64 |
+
|
| 65 |
+
|
| 66 |
@app.route('/predict', methods=['POST'])
|
| 67 |
def predict():
|
| 68 |
global client
|
|
|
|
| 77 |
seed = data['data'][3]
|
| 78 |
|
| 79 |
b64meta, b64_data = base64Image.split(',')
|
| 80 |
+
image = Image.open(BytesIO(base64.b64decode(b64_data)))
|
| 81 |
+
|
| 82 |
+
return (image, prompt, steps, seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
if __name__ == '__main__':
|