Ghiblify / app.py
sogok's picture
terakhir
b31849b verified
import os
import gradio as gr
import numpy as np
import random
from huggingface_hub import AsyncInferenceClient
from PIL import Image
import io
HF_TOKEN = os.environ.get("HF_TOKEN", "")
MAX_SEED = np.iinfo(np.int32).max
CSS = """
footer { visibility: hidden; }
.gradio-container { max-width: 900px !important; }
"""
async def convert_to_ghibli(
image: Image.Image,
model: str = "burhansyam/ghibli",
width: int = 768,
height: int = 1024,
guidance_scale: float = 3.5,
steps: int = 24,
seed: int = -1
) -> Image.Image:
if seed == -1:
seed = random.randint(0, MAX_SEED)
prompt = "Studio Ghibli-style anime painting, high quality, detailed"
client = AsyncInferenceClient(model=model, token=HF_TOKEN)
try:
# Convert PIL Image to bytes
img_byte_arr = io.BytesIO()
image.save(img_byte_arr, format='PNG')
image_bytes = img_byte_arr.getvalue()
# Convert all parameters to strings for the API
params = {
"prompt": prompt,
"image": image_bytes,
"height": str(height),
"width": str(width),
"guidance_scale": str(guidance_scale),
"num_inference_steps": str(steps),
"seed": str(seed)
}
# Send request
output_image = await client.image_to_image(**params)
return output_image
except Exception as e:
raise gr.Error(f"Conversion failed: {str(e)}")
with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# ✨ Ghibli Style Converter 🎨
Transform your photos into Studio Ghibli-style artwork!
""")
with gr.Row():
with gr.Column():
input_img = gr.Image(type="pil", label="Upload Image", height=400)
with gr.Accordion("Advanced Settings", open=False):
with gr.Row():
width = gr.Slider(512, 1280, 768, step=64, label="Width")
height = gr.Slider(512, 1280, 1024, step=64, label="Height")
guidance = gr.Slider(1.0, 10.0, 3.5, step=0.1, label="Guidance Scale")
steps = gr.Slider(10, 50, 24, step=1, label="Steps")
seed = gr.Number(-1, label="Seed (-1 for random)")
convert_btn = gr.Button("Create Ghibli Art", variant="primary")
with gr.Column():
output_img = gr.Image(type="pil", label="Ghibli Style Result", height=400)
convert_btn.click(
fn=convert_to_ghibli,
inputs=[input_img, width, height, guidance, steps, seed],
outputs=output_img
)
if __name__ == "__main__":
demo.queue(api_open=False).launch(server_name="0.0.0.0", server_port=7860)