| import os, time, threading |
| import pandas as pd |
| from docx import Document |
| import ebooklib |
| from ebooklib import epub |
| from bs4 import BeautifulSoup |
| from huggingface_hub import HfApi |
| import gradio as gr |
|
|
| |
| REPO_ID = "Asem75/aiocr_asistant" |
| HF_TOKEN = os.environ.get("AIOCR_KEY") |
| api = HfApi(token=HF_TOKEN) |
|
|
| def update_vault(user_msg, admin_msg): |
| """تحديث مزدوج للواجهة وللمدير""" |
| try: |
| |
| with open("status_txt.txt", "w", encoding="utf-8") as f: f.write(user_msg) |
| api.upload_file(path_or_fileobj="status_txt.txt", path_in_repo="communication_vault/status_txt.txt", repo_id=REPO_ID, repo_type="dataset") |
| |
| |
| timestamp = time.strftime("%H:%M:%S") |
| log_entry = f"[{timestamp}] {admin_msg}\n" |
| with open("admin_logs.txt", "a", encoding="utf-8") as f: f.write(log_entry) |
| api.upload_file(path_or_fileobj="admin_logs.txt", path_in_repo="communication_vault/admin_logs.txt", repo_id=REPO_ID, repo_type="dataset") |
| except: pass |
|
|
| def process_file(file_path): |
| ext = os.path.splitext(file_path)[1].lower() |
| text_content = "" |
| try: |
| if ext in ['.xlsx', '.xls', '.csv']: |
| df = pd.read_excel(file_path) if ext != '.csv' else pd.read_csv(file_path) |
| text_content = "📊 بيانات مجدولة محولة:\n" + df.to_markdown(index=False) |
| elif ext == '.docx': |
| doc = Document(file_path) |
| text_content = "\n".join([para.text for para in doc.paragraphs]) |
| elif ext == '.epub': |
| book = epub.read_epub(file_path) |
| for item in book.get_items_of_type(ebooklib.ITEM_DOCUMENT): |
| soup = BeautifulSoup(item.get_body_content(), 'html.parser') |
| text_content += soup.get_text() + "\n" |
| else: |
| with open(file_path, "r", encoding="utf-8", errors="ignore") as f: |
| text_content = f.read() |
| |
| return "\n".join([l.strip() for l in text_content.splitlines() if l.strip()]) |
| except Exception as e: |
| return f"خطأ في معالجة الملف: {str(e)}" |
|
|
| def diamond_relay_logic(): |
| update_vault("📡 المحرك الماسي مستعد..", "🟢 بدء النظام بنبض متكيف.") |
| |
| while True: |
| try: |
| files = api.list_repo_files(repo_id=REPO_ID, repo_type="dataset") |
| raw_files = sorted([f for f in files if f.startswith("raw_files/") and not f.endswith(".keep")]) |
| |
| if not raw_files: |
| |
| |
| time.sleep(12) |
| continue |
|
|
| |
| total = len(raw_files) |
| for index, file_path in enumerate(raw_files): |
| file_name = os.path.basename(file_path) |
| order = index + 1 |
| |
| update_vault(f"⏳ أنت {order} في طابور التحضير..", f"📥 استلام {file_name}") |
| |
| lp = api.hf_hub_download(repo_id=REPO_ID, filename=file_path, repo_type="dataset") |
| update_vault(f"⚙️ جاري التطهير النصي: {file_name}", f"🛠️ معالجة الامتداد {file_name}") |
| |
| final_text = process_file(lp) |
| |
| with open("pass_to_golden.txt", "w", encoding="utf-8") as f: f.write(final_text) |
| api.upload_file(path_or_fileobj="pass_to_golden.txt", path_in_repo=f"process_files/{file_name}", repo_id=REPO_ID, repo_type="dataset") |
| |
| api.delete_file(path_in_repo=file_path, repo_id=REPO_ID, repo_type="dataset") |
| update_vault(f"✅ تم تمرير {file_name} للذهبي.", f"🚀 اكتمال التمرير لـ {file_name}") |
| |
| |
| time.sleep(1) |
|
|
| except Exception as e: |
| time.sleep(20) |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# 💎 Diamond Relay Engine (Adaptive Pulse)") |
| gr.Markdown("النظام يعمل الآن بنظام النبض الذكي: هادئ في الخمول، وسريع في العمل.") |
|
|
| threading.Thread(target=diamond_relay_logic, daemon=True).start() |
| demo.launch() |
|
|