giblianimation / app.py
kamranmu890's picture
Update app.py
7b84cb6 verified
import gradio as gr
from PIL import Image
import torch
from transformers import pipeline
import os
# Load the AnimeGANv2_Shinkai model
try:
pipe = pipeline("image-to-image", model="Jovie/Anime")
except Exception as e:
print(f"Error loading model: {e}")
pipe = None # Handle the case where the model fails to load
def transform_to_anime(image):
if pipe is None:
print("Model not loaded. Returning original image.")
return image # Return the original image if the model failed to load
try:
anime_image = pipe(image)[0]
return anime_image
except Exception as e:
print(f"Error processing image: {e}")
return image
def process_image(image_input):
if image_input is None:
return None
transformed_image = transform_to_anime(image_input)
return transformed_image
# Load example images
example_images = []
example_dir = "images" # Assuming your example images are in a folder named 'images'
if os.path.exists(example_dir):
for filename in os.listdir(example_dir):
if filename.endswith((".jpg", ".png")):
try:
img_path = os.path.join(example_dir, filename)
img = Image.open(img_path)
example_images.append([img]) # Gradio expects a list of input values
except Exception as e:
print(f"Error loading example image {filename}: {e}")
iface = gr.Interface(
fn=process_image,
inputs=gr.Image(type="pil", label="Upload an Image"),
outputs=gr.Image(type="pil", label="Anime-Style Image"),
title="Anime Style Image Transformation",
description="Upload an image to transform it into an anime cartoon style.",
examples=example_images,
)
iface.launch()