drdudddd commited on
Commit
19b551e
·
verified ·
1 Parent(s): a4382be

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from PIL import Image
5
+ import torch
6
+ from AnimeGANv2.test import inference, load_model
7
+
8
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
9
+
10
+ generator = None
11
+
12
+ def get_generator():
13
+ global generator
14
+ if generator is None:
15
+ generator = load_model(style="Hayao", device=DEVICE)
16
+ return generator
17
+
18
+ def ghibli_transform(image):
19
+ if image is None:
20
+ return None, "Kein Bild"
21
+
22
+ try:
23
+ gen = get_generator()
24
+ img_np = np.array(image)
25
+ img_bgr = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
26
+ cartoon_bgr = inference.inference(gen, img_bgr, device=DEVICE)
27
+ cartoon_rgb = cv2.cvtColor(cartoon_bgr, cv2.COLOR_BGR2RGB)
28
+ result = Image.fromarray(cartoon_rgb)
29
+ return result, "fertig"
30
+ except Exception as e:
31
+ return None, str(e)
32
+
33
+ css = """
34
+ .gradio-container { max-width: 1100px; margin: auto; padding: 1rem; }
35
+ h1 { color: #4CAF50; text-align: center; }
36
+ button { min-height: 50px; font-size: 1.1rem !important; }
37
+ """
38
+
39
+ with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="green")) as demo:
40
+ gr.Markdown("# Foto → Studio Ghibli Style (Hayao)")
41
+
42
+ with gr.Row():
43
+ input_image = gr.Image(type="pil", label="Original", sources=["upload", "clipboard"], height=480)
44
+
45
+ with gr.Column(min_width=280):
46
+ btn = gr.Button("In Ghibli-Stil umwandeln", variant="primary")
47
+
48
+ output_image = gr.Image(label="Ergebnis", height=480)
49
+
50
+ status = gr.Textbox(label="Status", interactive=False)
51
+
52
+ gr.Examples(
53
+ examples=[
54
+ "https://images.unsplash.com/photo-1506905925346-21bda4d32df4",
55
+ "https://images.unsplash.com/photo-1543466835-00a7907e9de1",
56
+ "https://images.unsplash.com/photo-1501785888041-af3ef285b470",
57
+ "https://images.unsplash.com/photo-1470071459604-3b5ec3a7fe05",
58
+ ],
59
+ inputs=input_image
60
+ )
61
+
62
+ btn.click(
63
+ fn=ghibli_transform,
64
+ inputs=input_image,
65
+ outputs=[output_image, status]
66
+ )
67
+
68
+ if __name__ == "__main__":
69
+ demo.launch()