Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from huggingface_hub import hf_hub_download | |
| import importlib.util | |
| import sys | |
| # ========================================= | |
| # 1. Load the Inference Script Dynamically | |
| # ========================================= | |
| REPO_ID = "malkhuzanie/arabic-punctuation-checkpoints" | |
| FILENAME = "inference.py" | |
| print(f"Downloading {FILENAME} from {REPO_ID}...") | |
| script_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME) | |
| # Load the script as a module | |
| spec = importlib.util.spec_from_file_location("inference", script_path) | |
| inference = importlib.util.module_from_spec(spec) | |
| # CRITICAL: Register the module so pickle can find 'Vocabulary' class | |
| sys.modules["inference"] = inference | |
| spec.loader.exec_module(inference) | |
| print("Inference script loaded successfully!") | |
| # ========================================= | |
| # 2. Initialize Model | |
| # ========================================= | |
| # The class inside inference.py handles downloading the weights/vocab automatically | |
| restorer = inference.PunctuationRestorer(repo_id=REPO_ID) | |
| # ========================================= | |
| # 3. Define Gradio Interface | |
| # ========================================= | |
| def gradio_predict(text): | |
| if not text or not text.strip(): | |
| return "" | |
| return restorer.predict(text) | |
| # Custom CSS to force Right-to-Left (RTL) alignment for Arabic | |
| css = """ | |
| textarea { direction: rtl; } | |
| #component-0 { direction: rtl; } | |
| #component-1 { direction: rtl; } | |
| """ | |
| iface = gr.Interface( | |
| fn=gradio_predict, | |
| inputs=gr.Textbox( | |
| lines=3, | |
| placeholder="اكتب النص العربي هنا...", | |
| label="Input Text (النص المدخل)", | |
| text_align="right", | |
| rtl=True | |
| ), | |
| outputs=gr.Textbox( | |
| label="Punctuated Text (النص المرقّم)", | |
| text_align="right", | |
| rtl=True | |
| ), | |
| title="Arabic Punctuation Restoration", | |
| description="Enter raw Arabic text without punctuation to see the model predictions.", | |
| examples=[ | |
| ["هل تساءلت يوما عن معنى الحياة ما هي الأسئلة التي تشغل بالك"], | |
| ["الجو جميل اليوم لا اعتقد انها ستمطر"] | |
| ], | |
| theme="default" | |
| ) | |
| iface.launch() | |