Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
-
from
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
output = pipe(image=input_image, strength=0.8)
|
| 15 |
-
return output.images[0]
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
iface = gr.Interface(
|
| 19 |
-
fn=
|
| 20 |
inputs=gr.Image(type="pil"),
|
| 21 |
outputs=gr.Image(type="pil"),
|
| 22 |
-
title="Modern
|
| 23 |
-
description="Upload your
|
| 24 |
)
|
| 25 |
|
| 26 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
|
| 7 |
+
# -----------------------------
|
| 8 |
+
# بارگذاری مدل CodeFormer مستقیم از Hugging Face
|
| 9 |
+
# -----------------------------
|
| 10 |
+
model_repo = "sczhou/CodeFormer"
|
| 11 |
+
model_file = "CodeFormer.pth" # نام مدل داخل repo
|
| 12 |
+
model_path = hf_hub_download(repo_id=model_repo, filename=model_file)
|
| 13 |
|
| 14 |
+
# فرض: کلاس CodeFormer موجود باشد
|
| 15 |
+
from codeformer import CodeFormer
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
+
model = CodeFormer(pretrained=model_path, device=device)
|
| 19 |
+
|
| 20 |
+
# -----------------------------
|
| 21 |
+
# تابع تبدیل تصویر
|
| 22 |
+
# -----------------------------
|
| 23 |
+
def modern_cartoon(image):
|
| 24 |
+
image = image.convert("RGB")
|
| 25 |
+
transform = transforms.Compose([
|
| 26 |
+
transforms.Resize((512, 512)),
|
| 27 |
+
transforms.ToTensor()
|
| 28 |
+
])
|
| 29 |
+
img_tensor = transform(image).unsqueeze(0).to(device)
|
| 30 |
+
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
output_tensor = model.enhance(img_tensor, fidelity=0.8)
|
| 33 |
+
|
| 34 |
+
output_image = transforms.ToPILImage()(output_tensor.squeeze(0).cpu())
|
| 35 |
+
return output_image
|
| 36 |
+
|
| 37 |
+
# -----------------------------
|
| 38 |
+
# رابط Gradio
|
| 39 |
+
# -----------------------------
|
| 40 |
iface = gr.Interface(
|
| 41 |
+
fn=modern_cartoon,
|
| 42 |
inputs=gr.Image(type="pil"),
|
| 43 |
outputs=gr.Image(type="pil"),
|
| 44 |
+
title="AnimeGAN → Modern Cartoon",
|
| 45 |
+
description="Upload your AnimeGAN output → get a modern cartoon with restored face and background."
|
| 46 |
)
|
| 47 |
|
| 48 |
iface.launch()
|