| import os |
| import sys |
| import subprocess |
| import time |
| import glob |
| import shutil |
| import json |
| import dataclasses |
| import gradio as gr |
| from PIL import Image |
|
|
| |
| BASE_DIR = os.getcwd() |
| APP_DIR = os.path.join(BASE_DIR, "BallonsTranslator") |
| |
| TEMP_DIR = os.path.join(BASE_DIR, "process_folder") |
|
|
| |
| if not os.path.exists(APP_DIR): |
| print("جاري تحميل محرك التبييض...") |
| subprocess.run(["git", "clone", "https://github.com/dmMaze/BallonsTranslator.git", APP_DIR], check=True) |
|
|
| if APP_DIR not in sys.path: |
| sys.path.insert(0, APP_DIR) |
|
|
| |
| def process_manga(input_img): |
| if input_img is None: |
| return None |
|
|
| |
| if os.path.exists(TEMP_DIR): |
| shutil.rmtree(TEMP_DIR) |
| os.makedirs(TEMP_DIR) |
| |
| |
| img_path = os.path.join(TEMP_DIR, "input_page.png") |
| |
| input_img.save(img_path) |
|
|
| |
| try: |
| from utils.config import ProgramConfig |
| cfg = ProgramConfig() |
| cfg.module.inpainter = "lama_large_512px" |
| cfg.module.enable_translate = False |
| cfg.module.enable_ocr = True |
| cfg.module.enable_inpaint = True |
| |
| cfg_path = os.path.join(APP_DIR, 'config/config.json') |
| os.makedirs(os.path.dirname(cfg_path), exist_ok=True) |
| with open(cfg_path, 'w', encoding='utf-8') as f: |
| json.dump(dataclasses.asdict(cfg), f, indent=2, ensure_ascii=False) |
| except Exception as e: |
| print(f"Config error: {e}") |
|
|
| |
| print("بدء عملية التبييض...") |
| subprocess.run([ |
| sys.executable, "launch.py", |
| "--headless", |
| "--exec_dirs", TEMP_DIR, |
| "--ldpi", "96" |
| ], cwd=APP_DIR) |
|
|
| |
| result_folder = os.path.join(TEMP_DIR, "inpainted") |
| results = glob.glob(os.path.join(result_folder, "*.*")) |
|
|
| if results: |
| return Image.open(results[0]) |
| else: |
| return None |
|
|
| |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: |
| gr.Markdown(""" |
| # 🎨 مبيّض المانجا الآلي (Manga Inpainter) |
| ارفع صفحة المانجا وسيقوم الذكاء الاصطناعي بإزالة النصوص منها مع الحفاظ على الرسوم. |
| """) |
| |
| with gr.Row(): |
| with gr.Column(): |
| input_file = gr.Image(type="pil", label="ارفع صفحة المانجا هنا") |
| btn = gr.Button("بدء التبييض ✨", variant="primary") |
| |
| with gr.Column(): |
| output_file = gr.Image(type="pil", label="النتيجة (بدون نص)") |
|
|
| btn.click(fn=process_manga, inputs=input_file, outputs=output_file) |
| |
| gr.Markdown("---") |
| gr.Markdown("💡 **ملاحظة:** العملية قد تستغرق من 20 إلى 60 ثانية حسب حجم الصورة.") |
|
|
| |
| if __name__ == "__main__": |
| demo.launch(server_name="0.0.0.0", server_port=7860) |