Spaces:
Runtime error
Runtime error
initial commit
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
def create_demo(process):
|
| 4 |
+
with gr.Blocks() as demo:
|
| 5 |
+
with gr.Column():
|
| 6 |
+
prompt = gr.Textbox(label='Prompt')
|
| 7 |
+
n_prompt = gr.Textbox(
|
| 8 |
+
label='Negative Prompt',
|
| 9 |
+
value=
|
| 10 |
+
'low quality, ugly, disfigured, deformed'
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
run_button = gr.Button('Run')
|
| 14 |
+
result = gr.Gallery(label='Output',
|
| 15 |
+
show_label=False,
|
| 16 |
+
elem_id='gallery').style(columns=1, rows=1, preview=True)
|
| 17 |
+
|
| 18 |
+
inputs = [
|
| 19 |
+
prompt,
|
| 20 |
+
n_prompt
|
| 21 |
+
]
|
| 22 |
+
prompt.submit(
|
| 23 |
+
fn=process,
|
| 24 |
+
inputs=inputs,
|
| 25 |
+
outputs=result
|
| 26 |
+
)
|
| 27 |
+
prompt.submit(
|
| 28 |
+
fn=process,
|
| 29 |
+
inputs=inputs,
|
| 30 |
+
outputs=result
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
run_button.click(
|
| 34 |
+
fn=process,
|
| 35 |
+
inputs=inputs,
|
| 36 |
+
outputs=result
|
| 37 |
+
)
|
| 38 |
+
return demo
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
if __name__ == '__main__':
|
| 42 |
+
from model import Model
|
| 43 |
+
model = Model()
|
| 44 |
+
demo = create_demo(model.process)
|
| 45 |
+
demo.queue().launch()
|
model.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
|
| 2 |
+
import torch
|
| 3 |
+
import PIL.Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
class Model:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
modelID = "runwayml/stable-diffusion-v1-5"
|
| 9 |
+
#pipeline = StableDiffusionPipeline.from_pretrained(modelID, torch_dtype=torch.float16)
|
| 10 |
+
self.pipe = StableDiffusionPipeline.from_pretrained(modelID)
|
| 11 |
+
#prompt = "a photo of an astronaut riding a horse on mars"
|
| 12 |
+
#n_prompt = "deformed, disfigured"
|
| 13 |
+
|
| 14 |
+
def process(self,
|
| 15 |
+
prompt: str,
|
| 16 |
+
negative_prompt: str,
|
| 17 |
+
guidance_scale:int = 7,
|
| 18 |
+
num_images:int = 1,
|
| 19 |
+
num_steps:int = 2,
|
| 20 |
+
) -> list[PIL.Image.Image]:
|
| 21 |
+
seed = np.random.randint(0, np.iinfo(np.int64).max)
|
| 22 |
+
generator = torch.Generator().manual_seed(seed)
|
| 23 |
+
return self.pipe(prompt=prompt,
|
| 24 |
+
negative_prompt=negative_prompt,
|
| 25 |
+
guidance_scale=guidance_scale,
|
| 26 |
+
num_images_per_prompt=num_images,
|
| 27 |
+
num_inference_steps=num_steps,
|
| 28 |
+
generator=generator).images
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# image = pipeline(prompt=prompt,
|
| 34 |
+
# negative_prompt = n_prompt,
|
| 35 |
+
# num_inference_steps = 2,
|
| 36 |
+
# guidance_scale = 7).images
|
| 37 |
+
|
| 38 |
+
|