Download app.py from Bombog944/Trans1: direct link, hf CLI and curl.
- Browser
- Download file 2.16 kB
-
https://huggingface.co/spaces/Bombog944/Trans1/resolve/main/app.py
- Command line
-
hf download hf://spaces/Bombog944/Trans1/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Bombog944/Trans1/resolve/main/app.py
2.16 kB
| import gradio as gr | |
| from PIL import Image, ImageDraw, ImageFont | |
| import pytesseract | |
| from googletrans import Translator | |
| import os | |
| import zipfile | |
| translator = Translator() | |
| def translate_image(imgs): | |
| results = [] | |
| zip_path = "translated_results.zip" | |
| # لو الملف القديم موجود نحذفه | |
| if os.path.exists(zip_path): | |
| os.remove(zip_path) | |
| with zipfile.ZipFile(zip_path, "w") as zipf: | |
| for i, img in enumerate(imgs): | |
| # استخراج النص من الصورة | |
| text = pytesseract.image_to_string(Image.open(img.name)) | |
| # ترجمة النص للعربية | |
| translated = translator.translate(text, src='auto', dest='ar').text | |
| # تجهيز الصورة المقارنة | |
| img = Image.open(img.name).convert("RGB") | |
| w, h = img.size | |
| result = Image.new("RGB", (w * 2, h), "white") | |
| result.paste(img, (0, 0)) | |
| # جزء الترجمة | |
| translated_img = Image.new("RGB", (w, h), "white") | |
| draw = ImageDraw.Draw(translated_img) | |
| font = ImageFont.load_default() | |
| draw.multiline_text((10, 10), translated, fill="black", font=font) | |
| result.paste(translated_img, (w, 0)) | |
| out_name = f"translated_{i+1}.jpg" | |
| result.save(out_name) | |
| zipf.write(out_name) | |
| results.append(result) | |
| return results, zip_path | |
| # واجهة Gradio | |
| demo = gr.Interface( | |
| fn=translate_image, | |
| inputs=gr.Files(file_count="multiple", type="file", label="📁 ارفع صورك للترجمة"), | |
| outputs=[ | |
| gr.Gallery(label="📸 الصور بعد الترجمة (نص أصلي + نص مترجم)"), | |
| gr.File(label="📦 تحميل كل الصور في ملف ZIP") | |
| ], | |
| title="🈶 مترجم الصور (نص أصلي + ترجمة بالعربية)", | |
| description="يرفع المستخدم صور تحتوي على نصوص بأي لغة، ويعرض البرنامج مقارنة بين النص الأصلي والترجمة بالعربية جنبًا إلى جنب." | |
| ) | |
| demo.launch() |