import os import sys import subprocess import traceback # 1. تسطيب المكتبات عشان السيرفر مايعيطش print("جاري تسطيب المكتبات...") try: subprocess.check_call([sys.executable, "-m", "pip", "install", "einops", "json5", "jaconv", "openai", "httpx", "requests", "betterproto", "bs4", "shapely"]) except Exception as e: print("خطأ في التسطيب:", e) import gradio as gr import json import numpy as np # التأكد إن بايثون شايف المجلدات اللي أنت رفعتها sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) ocr_model = None # ========================================== # دالة الاستخراج (OCR) # ========================================== def extract_text(image, boxes_json): try: boxes_data = json.loads(boxes_json) if not boxes_data: return "لم يتم العثور على مربعات." # دلوقتي هيقراها من مجلد utils اللي أنت رفعه بشكل طبيعي from utils.textblock import TextBlock blk_list = [] for b in boxes_data: blk = TextBlock() blk.box = [b['x'], b['y'], b['w'], b['h']] blk_list.append(blk) global ocr_model if ocr_model is None: # هيقرا الموديلات من مجلد modules اللي أنت رفعه from modules.base import load_modules load_modules() from modules.ocr import OCR if not OCR.module_dict: return "===== خطأ: مفيش ولا موديل اتقرى من مجلد modules =====" # هيدور على الموديل العربي أو أي موديل شغال target_key = None for key in OCR.module_dict.keys(): if 'ar' in key.lower() or '48' in key.lower() or 'mit' in key.lower(): target_key = key break if not target_key: target_key = list(OCR.module_dict.keys())[0] ocr_model = OCR.module_dict[target_key]() if hasattr(ocr_model, 'all_model_loaded') and not ocr_model.all_model_loaded(): ocr_model.load_model() results = ocr_model.run_ocr(image, blk_list) final_text = [] if results: for r in results: if isinstance(r.text, list): final_text.append(" ".join(r.text)) else: final_text.append(str(r.text)) return "\n\n".join(final_text) except Exception as e: return f"===== خطأ برمجي =====\n\n{traceback.format_exc()}" # سايبلك دالة التحديد التلقائي فاضية عشان نركز على استخراج المربعات اليدوية الأول def detect_boxes(image): return json.dumps([]) with gr.Blocks() as demo: img_input = gr.Image(type="numpy") boxes_input = gr.Textbox() detect_out = gr.Textbox() extract_out = gr.Textbox() detect_btn = gr.Button("detect") extract_btn = gr.Button("extract") detect_btn.click(fn=detect_boxes, inputs=img_input, outputs=detect_out, api_name="detect") extract_btn.click(fn=extract_text, inputs=[img_input, boxes_input], outputs=extract_out, api_name="extract") if __name__ == "__main__": demo.launch()