Update app.py
Browse files
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 |
-
|
| 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 |
-
|
| 21 |
-
if image is None:
|
| 22 |
-
return None
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
fn=convert,
|
| 40 |
inputs=[
|
| 41 |
-
gr.Image(type="pil"
|
| 42 |
-
gr.Textbox(label="
|
| 43 |
-
gr.Slider(0.1, 1.0, value=0.7, label="
|
| 44 |
-
gr.
|
| 45 |
],
|
| 46 |
-
outputs=gr.Image(
|
| 47 |
-
title="
|
| 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()
|
|
|