Kingoteam commited on
Commit
47dca73
·
verified ·
1 Parent(s): 1bca99d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -41
app.py CHANGED
@@ -1,50 +1,43 @@
1
  import gradio as gr
2
- from diffusers import StableDiffusionImg2ImgPipeline
3
  import torch
 
4
  from PIL import Image
5
 
6
- # مدل سبک و عمومی
7
- MODEL_ID = "nitrosocke/anything-mid" # سبک و img2img مناسب
8
-
9
- # لود مدل
10
- pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
11
- MODEL_ID,
12
- torch_dtype=torch.float16
13
- ).to("cuda" if torch.cuda.is_available() else "cpu") # اگر GPU باشد سریع‌تر
14
-
15
- # غیرفعال کردن فیلتر NSFW
16
- def dummy_checker(images, clip_input):
17
- return images, [False] * len(images)
18
- pipe.safety_checker = dummy_checker
19
 
20
- def convert(image, prompt, strength, guidance):
21
- if image is None:
22
- return None
23
 
24
- # تغییر اندازه عکس ورودی به 560x560
25
- image = image.convert("RGB").resize((560, 560))
26
-
27
- # اجرای مدل
28
- result = pipe(
29
- prompt=prompt,
30
- image=image,
31
- strength=strength,
32
- guidance_scale=guidance,
33
- num_inference_steps=15 # کم برای سرعت بالا
34
- ).images[0]
35
-
36
- return result
37
-
38
- demo = gr.Interface(
 
 
 
 
 
 
 
39
  fn=convert,
40
  inputs=[
41
- gr.Image(type="pil", label="عکس ورودی"),
42
- gr.Textbox(label="پرامپت", value="anime style, colorful, high contrast"),
43
- gr.Slider(0.1, 1.0, value=0.7, label="قدرت تغییر (strength)"),
44
- gr.Slider(1, 15, value=7.5, label="راهنمایی مدل (guidance scale)")
45
  ],
46
- outputs=gr.Image(label="خروجی"),
47
- title="تبدیل عکس به انیمه سبک و سریع (560×560)"
48
- )
49
-
50
- demo.launch()
 
1
  import gradio as gr
 
2
  import torch
3
+ from diffusers import StableDiffusionImg2ImgPipeline
4
  from PIL import Image
5
 
6
+ # مدل سبک و سریع waifu-diffusion-lite
7
+ model_id = "hakurei/waifu-diffusion-lite"
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
10
 
11
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
12
+ model_id,
13
+ torch_dtype=torch.float16 if device=="cuda" else torch.float32
14
+ ).to(device)
15
+
16
+ # کم کردن مراحل برای سرعت بیشتر
17
+ pipe.scheduler.num_inference_steps = 15
18
+
19
+ def convert(input_image, prompt, strength=0.7, output_resolution=560):
20
+ # تغییر اندازه ورودی برای سرعت
21
+ input_image = input_image.resize((output_resolution, output_resolution))
22
+
23
+ # تبدیل به انیمه
24
+ with torch.autocast(device) if device=="cuda" else torch.no_grad():
25
+ result = pipe(prompt=prompt, image=input_image, strength=strength).images[0]
26
+
27
+ # بزرگنمایی به 1024×1024 با خودکارشده (اختیاری)
28
+ final_image = result.resize((1024, 1024))
29
+ return final_image
30
+
31
+ # رابط Gradio
32
+ gr.Interface(
33
  fn=convert,
34
  inputs=[
35
+ gr.Image(type="pil"),
36
+ gr.Textbox(label="Prompt"),
37
+ gr.Slider(minimum=0.1, maximum=1.0, step=0.05, value=0.7, label="Strength"),
38
+ gr.Dropdown(choices=[360, 480, 560, 640, 800], value=560, label="Input Resolution")
39
  ],
40
+ outputs=gr.Image(type="pil"),
41
+ title="Img2Anime Fast",
42
+ description="تبدیل عکس به انیمه با مدل سبک و سریع. رزولوشن ورودی قابل انتخاب است."
43
+ ).launch()