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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -14
app.py CHANGED
@@ -1,26 +1,48 @@
1
  import gradio as gr
2
  from PIL import Image
3
  import torch
4
- from diffusers import StableDiffusionImg2ImgPipeline
 
5
 
6
- # بارگذاری مدل Toonify مدرن
7
- pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
8
- "lavaman131/cartoonify",
9
- torch_dtype=torch.float16
10
- ).to("cuda" if torch.cuda.is_available() else "cpu")
 
11
 
12
- def toonify_image(input_image):
13
- # تبدیل تصویر به سبک کارتونی
14
- output = pipe(image=input_image, strength=0.8)
15
- return output.images[0]
16
 
17
- # رابط کاربری با Gradio
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  iface = gr.Interface(
19
- fn=toonify_image,
20
  inputs=gr.Image(type="pil"),
21
  outputs=gr.Image(type="pil"),
22
- title="Modern Toonify",
23
- description="Upload your face photo → get a modern cartoon version. No prompt needed."
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()