Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from PIL import Image
|
|
| 8 |
from typing import Iterable
|
| 9 |
from gradio.themes import Soft
|
| 10 |
from gradio.themes.utils import colors, fonts, sizes
|
| 11 |
-
from deep_translator import GoogleTranslator
|
| 12 |
|
| 13 |
# --- تنظیمات تم ---
|
| 14 |
colors.steel_blue = colors.Color(
|
|
@@ -35,7 +35,7 @@ class SteelBlueTheme(Soft):
|
|
| 35 |
neutral_hue: colors.Color | str = colors.slate,
|
| 36 |
text_size: sizes.Size | str = sizes.text_lg,
|
| 37 |
font: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 38 |
-
fonts.GoogleFont("Vazirmatn"), "Arial", "sans-serif",
|
| 39 |
),
|
| 40 |
font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 41 |
fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace",
|
|
@@ -148,13 +148,12 @@ def translate_prompt(text):
|
|
| 148 |
if not text:
|
| 149 |
return ""
|
| 150 |
try:
|
| 151 |
-
# اگر متن تماماً انگلیسی باشد، ممکن است نیازی به ترجمه نباشد اما auto هندل میکند
|
| 152 |
translated = GoogleTranslator(source='auto', target='en').translate(text)
|
| 153 |
print(f"Original: {text} -> Translated: {translated}")
|
| 154 |
return translated
|
| 155 |
except Exception as e:
|
| 156 |
print(f"Translation Error: {e}")
|
| 157 |
-
return text
|
| 158 |
|
| 159 |
def update_dimensions_on_upload(image):
|
| 160 |
if image is None:
|
|
@@ -171,7 +170,6 @@ def update_dimensions_on_upload(image):
|
|
| 171 |
aspect_ratio = original_width / original_height
|
| 172 |
new_width = int(new_height * aspect_ratio)
|
| 173 |
|
| 174 |
-
# Ensure dimensions are multiples of 8
|
| 175 |
new_width = (new_width // 8) * 8
|
| 176 |
new_height = (new_height // 8) * 8
|
| 177 |
|
|
@@ -181,7 +179,7 @@ def update_dimensions_on_upload(image):
|
|
| 181 |
def infer(
|
| 182 |
input_image,
|
| 183 |
prompt,
|
| 184 |
-
lora_adapter_persian,
|
| 185 |
seed,
|
| 186 |
randomize_seed,
|
| 187 |
guidance_scale,
|
|
@@ -191,15 +189,9 @@ def infer(
|
|
| 191 |
if input_image is None:
|
| 192 |
raise gr.Error("لطفاً یک تصویر برای ویرایش بارگذاری کنید.")
|
| 193 |
|
| 194 |
-
# ترجمه متن کاربر
|
| 195 |
english_prompt = translate_prompt(prompt)
|
| 196 |
-
|
| 197 |
-
# انتخاب آداپتور بر اساس نام فارسی
|
| 198 |
adapter_internal_name = LORA_MAPPING.get(lora_adapter_persian)
|
| 199 |
|
| 200 |
-
# غیرفعال کردن همه آداپتورها ابتدا (برای اطمینان)
|
| 201 |
-
# pipe.disable_lora() # متد دقیق ممکن است متفاوت باشد اما set_adapters جایگزین میکند
|
| 202 |
-
|
| 203 |
if adapter_internal_name:
|
| 204 |
pipe.set_adapters([adapter_internal_name], adapter_weights=[1.0])
|
| 205 |
|
|
@@ -207,16 +199,14 @@ def infer(
|
|
| 207 |
seed = random.randint(0, MAX_SEED)
|
| 208 |
|
| 209 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 210 |
-
# پرامپت منفی به انگلیسی (ثابت)
|
| 211 |
negative_prompt = "worst quality, low quality, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, jpeg artifacts, signature, watermark, username, blurry"
|
| 212 |
|
| 213 |
original_image = input_image.convert("RGB")
|
| 214 |
-
|
| 215 |
width, height = update_dimensions_on_upload(original_image)
|
| 216 |
|
| 217 |
result = pipe(
|
| 218 |
image=original_image,
|
| 219 |
-
prompt=english_prompt,
|
| 220 |
negative_prompt=negative_prompt,
|
| 221 |
height=height,
|
| 222 |
width=width,
|
|
@@ -232,12 +222,11 @@ def infer_example(input_image, prompt, lora_adapter):
|
|
| 232 |
input_pil = input_image.convert("RGB")
|
| 233 |
guidance_scale = 1.0
|
| 234 |
steps = 4
|
| 235 |
-
# infer به صورت خودکار ترجمه را انجام میدهد
|
| 236 |
result, seed = infer(input_pil, prompt, lora_adapter, 0, True, guidance_scale, steps)
|
| 237 |
return result, seed
|
| 238 |
|
| 239 |
|
| 240 |
-
#
|
| 241 |
css="""
|
| 242 |
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;700&display=swap');
|
| 243 |
|
|
@@ -271,6 +260,15 @@ body, .gradio-container {
|
|
| 271 |
.gr-input-label {
|
| 272 |
text-align: right !important;
|
| 273 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
"""
|
| 275 |
|
| 276 |
with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
|
|
@@ -290,7 +288,7 @@ with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
|
|
| 290 |
label="دستور ویرایش (به فارسی)",
|
| 291 |
show_label=True,
|
| 292 |
placeholder="مثال: تصویر را به سبک انیمه تبدیل کن...",
|
| 293 |
-
rtl=True
|
| 294 |
)
|
| 295 |
|
| 296 |
run_button = gr.Button("شروع ویرایش تصویر", variant="primary")
|
|
@@ -301,16 +299,15 @@ with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
|
|
| 301 |
with gr.Row():
|
| 302 |
lora_adapter = gr.Dropdown(
|
| 303 |
label="انتخاب سبک ویرایش (LoRA)",
|
| 304 |
-
choices=list(LORA_MAPPING.keys()),
|
| 305 |
value="تبدیل عکس به انیمه"
|
| 306 |
)
|
| 307 |
-
with gr.Accordion("تنظیمات پیشرفته", open=False, visible=True):
|
| 308 |
seed = gr.Slider(label="دانه تصادفی (Seed)", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 309 |
randomize_seed = gr.Checkbox(label="استفاده از Seed تصادفی", value=True)
|
| 310 |
guidance_scale = gr.Slider(label="میزان وفاداری به متن (Guidance Scale)", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
|
| 311 |
steps = gr.Slider(label="تعداد مراحل پردازش (Steps)", minimum=1, maximum=50, step=1, value=4)
|
| 312 |
|
| 313 |
-
# مثالهای فارسی شده
|
| 314 |
gr.Examples(
|
| 315 |
examples=[
|
| 316 |
["examples/1.jpg", "تبدیل به انیمه کن.", "تبدیل عکس به انیمه"],
|
|
|
|
| 8 |
from typing import Iterable
|
| 9 |
from gradio.themes import Soft
|
| 10 |
from gradio.themes.utils import colors, fonts, sizes
|
| 11 |
+
from deep_translator import GoogleTranslator
|
| 12 |
|
| 13 |
# --- تنظیمات تم ---
|
| 14 |
colors.steel_blue = colors.Color(
|
|
|
|
| 35 |
neutral_hue: colors.Color | str = colors.slate,
|
| 36 |
text_size: sizes.Size | str = sizes.text_lg,
|
| 37 |
font: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 38 |
+
fonts.GoogleFont("Vazirmatn"), "Arial", "sans-serif",
|
| 39 |
),
|
| 40 |
font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 41 |
fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace",
|
|
|
|
| 148 |
if not text:
|
| 149 |
return ""
|
| 150 |
try:
|
|
|
|
| 151 |
translated = GoogleTranslator(source='auto', target='en').translate(text)
|
| 152 |
print(f"Original: {text} -> Translated: {translated}")
|
| 153 |
return translated
|
| 154 |
except Exception as e:
|
| 155 |
print(f"Translation Error: {e}")
|
| 156 |
+
return text
|
| 157 |
|
| 158 |
def update_dimensions_on_upload(image):
|
| 159 |
if image is None:
|
|
|
|
| 170 |
aspect_ratio = original_width / original_height
|
| 171 |
new_width = int(new_height * aspect_ratio)
|
| 172 |
|
|
|
|
| 173 |
new_width = (new_width // 8) * 8
|
| 174 |
new_height = (new_height // 8) * 8
|
| 175 |
|
|
|
|
| 179 |
def infer(
|
| 180 |
input_image,
|
| 181 |
prompt,
|
| 182 |
+
lora_adapter_persian,
|
| 183 |
seed,
|
| 184 |
randomize_seed,
|
| 185 |
guidance_scale,
|
|
|
|
| 189 |
if input_image is None:
|
| 190 |
raise gr.Error("لطفاً یک تصویر برای ویرایش بارگذاری کنید.")
|
| 191 |
|
|
|
|
| 192 |
english_prompt = translate_prompt(prompt)
|
|
|
|
|
|
|
| 193 |
adapter_internal_name = LORA_MAPPING.get(lora_adapter_persian)
|
| 194 |
|
|
|
|
|
|
|
|
|
|
| 195 |
if adapter_internal_name:
|
| 196 |
pipe.set_adapters([adapter_internal_name], adapter_weights=[1.0])
|
| 197 |
|
|
|
|
| 199 |
seed = random.randint(0, MAX_SEED)
|
| 200 |
|
| 201 |
generator = torch.Generator(device=device).manual_seed(seed)
|
|
|
|
| 202 |
negative_prompt = "worst quality, low quality, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, jpeg artifacts, signature, watermark, username, blurry"
|
| 203 |
|
| 204 |
original_image = input_image.convert("RGB")
|
|
|
|
| 205 |
width, height = update_dimensions_on_upload(original_image)
|
| 206 |
|
| 207 |
result = pipe(
|
| 208 |
image=original_image,
|
| 209 |
+
prompt=english_prompt,
|
| 210 |
negative_prompt=negative_prompt,
|
| 211 |
height=height,
|
| 212 |
width=width,
|
|
|
|
| 222 |
input_pil = input_image.convert("RGB")
|
| 223 |
guidance_scale = 1.0
|
| 224 |
steps = 4
|
|
|
|
| 225 |
result, seed = infer(input_pil, prompt, lora_adapter, 0, True, guidance_scale, steps)
|
| 226 |
return result, seed
|
| 227 |
|
| 228 |
|
| 229 |
+
# --- تنظیمات CSS برای فارسیسازی و حذف فوتر ---
|
| 230 |
css="""
|
| 231 |
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;700&display=swap');
|
| 232 |
|
|
|
|
| 260 |
.gr-input-label {
|
| 261 |
text-align: right !important;
|
| 262 |
}
|
| 263 |
+
|
| 264 |
+
/* --- مخفی کردن فوتر (ساخته شده با Gradio) --- */
|
| 265 |
+
footer {
|
| 266 |
+
visibility: hidden !important;
|
| 267 |
+
display: none !important;
|
| 268 |
+
}
|
| 269 |
+
.gradio-container footer {
|
| 270 |
+
display: none !important;
|
| 271 |
+
}
|
| 272 |
"""
|
| 273 |
|
| 274 |
with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
|
|
|
|
| 288 |
label="دستور ویرایش (به فارسی)",
|
| 289 |
show_label=True,
|
| 290 |
placeholder="مثال: تصویر را به سبک انیمه تبدیل کن...",
|
| 291 |
+
rtl=True
|
| 292 |
)
|
| 293 |
|
| 294 |
run_button = gr.Button("شروع ویرایش تصویر", variant="primary")
|
|
|
|
| 299 |
with gr.Row():
|
| 300 |
lora_adapter = gr.Dropdown(
|
| 301 |
label="انتخاب سبک ویرایش (LoRA)",
|
| 302 |
+
choices=list(LORA_MAPPING.keys()),
|
| 303 |
value="تبدیل عکس به انیمه"
|
| 304 |
)
|
| 305 |
+
with gr.Accordion("تنظیمات پیشرفته", open=False, visible=True):
|
| 306 |
seed = gr.Slider(label="دانه تصادفی (Seed)", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 307 |
randomize_seed = gr.Checkbox(label="استفاده از Seed تصادفی", value=True)
|
| 308 |
guidance_scale = gr.Slider(label="میزان وفاداری به متن (Guidance Scale)", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
|
| 309 |
steps = gr.Slider(label="تعداد مراحل پردازش (Steps)", minimum=1, maximum=50, step=1, value=4)
|
| 310 |
|
|
|
|
| 311 |
gr.Examples(
|
| 312 |
examples=[
|
| 313 |
["examples/1.jpg", "تبدیل به انیمه کن.", "تبدیل عکس به انیمه"],
|