Kingoteam commited on
Commit
91865ff
·
verified ·
1 Parent(s): be0884f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -31
app.py CHANGED
@@ -1,48 +1,36 @@
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()
 
1
+ from gfpgan import GFPGANer
2
  from PIL import Image
3
+ import gradio as gr
 
 
4
 
5
  # -----------------------------
6
+ # بارگذاری مدل GFPGAN مومی)
7
  # -----------------------------
8
+ # مدل به صورت خودکار از HF دانلود می‌شود و عمومی است
9
+ restorer = GFPGANer(
10
+ model_path=None, # None → مدل عمومی
11
+ upscale=1, # بزرگنمایی 1x
12
+ arch='clean', # معماری مدل
13
+ channel_multiplier=2,
14
+ bg_upsampler=None
15
+ )
 
16
 
17
  # -----------------------------
18
+ # تابع ترمیم کارتونی
19
  # -----------------------------
20
+ def restore_cartoon(image):
21
  image = image.convert("RGB")
22
+ restored_image, _ = restorer.enhance(image, has_aligned=False)
23
+ return restored_image
 
 
 
 
 
 
 
 
 
24
 
25
  # -----------------------------
26
+ # رابط کاربری Gradio
27
  # -----------------------------
28
  iface = gr.Interface(
29
+ fn=restore_cartoon,
30
  inputs=gr.Image(type="pil"),
31
  outputs=gr.Image(type="pil"),
32
+ title="Cartoon Face Restorer",
33
+ description="Upload your AnimeGAN output → get a restored modern cartoon with face and background fixed."
34
  )
35
 
36
  iface.launch()