Spaces:
Sleeping
Sleeping
Anurag Bhardwaj
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,34 @@
|
|
| 1 |
-
import subprocess
|
| 2 |
-
import sys
|
| 3 |
-
|
| 4 |
-
def install(package):
|
| 5 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 6 |
-
|
| 7 |
-
# Attempt to import transformers and install if missing
|
| 8 |
-
try:
|
| 9 |
-
import transformers
|
| 10 |
-
except ModuleNotFoundError:
|
| 11 |
-
install("transformers")
|
| 12 |
-
import transformers
|
| 13 |
-
|
| 14 |
-
# Then proceed with your imports
|
| 15 |
-
from diffusers import StableDiffusionImg2ImgPipeline
|
| 16 |
import gradio as gr
|
| 17 |
import torch
|
|
|
|
| 18 |
from PIL import Image
|
| 19 |
|
| 20 |
-
# Load the
|
| 21 |
-
model_id = "nitrosocke/Ghibli-Diffusion"
|
| 22 |
-
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 23 |
-
# Use GPU if available, otherwise fall back to CPU
|
| 24 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def transform_image(input_image: Image.Image) -> Image.Image:
|
| 28 |
-
# Resize input image to 512x512 for consistency
|
| 29 |
input_image = input_image.resize((512, 512))
|
| 30 |
prompt = "ghibli style"
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return output.images[0]
|
| 34 |
|
| 35 |
-
# Create Gradio interface: input is an image, output is the transformed image.
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn=transform_image,
|
| 38 |
inputs=gr.Image(type="pil", label="Upload your portrait/photo"),
|
| 39 |
outputs=gr.Image(type="pil", label="Studio Ghibli Style Output"),
|
| 40 |
title="Studio Ghibli Style Converter",
|
| 41 |
-
description="Upload a portrait or photo
|
| 42 |
)
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# Load the pre-trained Studio Ghibli style model
|
| 7 |
+
model_id = "nitrosocke/Ghibli-Diffusion"
|
|
|
|
|
|
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
|
| 10 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 11 |
+
model_id, torch_dtype=torch.float16
|
| 12 |
+
).to(device)
|
| 13 |
|
| 14 |
def transform_image(input_image: Image.Image) -> Image.Image:
|
|
|
|
| 15 |
input_image = input_image.resize((512, 512))
|
| 16 |
prompt = "ghibli style"
|
| 17 |
+
output = pipe(
|
| 18 |
+
prompt=prompt,
|
| 19 |
+
image=input_image,
|
| 20 |
+
strength=0.75,
|
| 21 |
+
guidance_scale=7.5,
|
| 22 |
+
num_inference_steps=50,
|
| 23 |
+
)
|
| 24 |
return output.images[0]
|
| 25 |
|
|
|
|
| 26 |
demo = gr.Interface(
|
| 27 |
fn=transform_image,
|
| 28 |
inputs=gr.Image(type="pil", label="Upload your portrait/photo"),
|
| 29 |
outputs=gr.Image(type="pil", label="Studio Ghibli Style Output"),
|
| 30 |
title="Studio Ghibli Style Converter",
|
| 31 |
+
description="Upload a portrait or photo to transform it into a Studio Ghibli-style image.",
|
| 32 |
)
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|