Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,50 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
+
# Load the model (this runs when the Space starts)
|
| 7 |
+
model_id = "nitrosocke/Ghibli-Diffusion"
|
| 8 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 9 |
+
pipe = pipe.to("cuda") # Use GPU if available in the Space
|
| 10 |
+
|
| 11 |
+
# Define the inference function
|
| 12 |
+
def ghibli_transform(input_image, prompt="ghibli style", strength=0.75, guidance_scale=7.5):
|
| 13 |
+
# Convert Gradio image (numpy array) to PIL Image and resize
|
| 14 |
+
init_image = Image.fromarray(input_image).convert("RGB").resize((768, 768))
|
| 15 |
+
|
| 16 |
+
# Generate the Ghibli-style image
|
| 17 |
+
output = pipe(
|
| 18 |
+
prompt=prompt,
|
| 19 |
+
init_image=init_image,
|
| 20 |
+
strength=strength,
|
| 21 |
+
guidance_scale=guidance_scale,
|
| 22 |
+
num_inference_steps=50
|
| 23 |
+
).images[0]
|
| 24 |
+
|
| 25 |
+
return output
|
| 26 |
+
|
| 27 |
+
# Create the Gradio interface
|
| 28 |
+
with gr.Blocks(title="Ghibli Diffusion Image Transformer") as demo:
|
| 29 |
+
gr.Markdown("# Ghibli Diffusion Image Transformer")
|
| 30 |
+
gr.Markdown("Upload an image and transform it into Studio Ghibli style using nitrosocke/Ghibli-Diffusion!")
|
| 31 |
+
|
| 32 |
+
with gr.Row():
|
| 33 |
+
with gr.Column():
|
| 34 |
+
input_img = gr.Image(label="Upload Image", type="numpy")
|
| 35 |
+
prompt = gr.Textbox(label="Prompt", value="ghibli style")
|
| 36 |
+
strength = gr.Slider(0, 1, value=0.75, step=0.05, label="Strength (How much to transform)")
|
| 37 |
+
guidance = gr.Slider(1, 20, value=7.5, step=0.5, label="Guidance Scale")
|
| 38 |
+
submit_btn = gr.Button("Transform")
|
| 39 |
+
with gr.Column():
|
| 40 |
+
output_img = gr.Image(label="Ghibli-Style Output")
|
| 41 |
|
| 42 |
+
# Connect the button to the function
|
| 43 |
+
submit_btn.click(
|
| 44 |
+
fn=ghibli_transform,
|
| 45 |
+
inputs=[input_img, prompt, strength, guidance],
|
| 46 |
+
outputs=output_img
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Launch the Space
|
| 50 |
demo.launch()
|