malkhuzanie commited on
Commit
278d33b
·
1 Parent(s): fff87f6

Add app file

Browse files
Files changed (2) hide show
  1. app.py +70 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import hf_hub_download
3
+ import importlib.util
4
+ import sys
5
+
6
+ # =========================================
7
+ # 1. Load the Inference Script Dynamically
8
+ # =========================================
9
+ REPO_ID = "malkhuzanie/arabic-punctuation-checkpoints"
10
+ FILENAME = "inference.py"
11
+
12
+ print(f"Downloading {FILENAME} from {REPO_ID}...")
13
+ script_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
14
+
15
+ # Load the script as a module
16
+ spec = importlib.util.spec_from_file_location("inference", script_path)
17
+ inference = importlib.util.module_from_spec(spec)
18
+
19
+ # CRITICAL: Register the module so pickle can find 'Vocabulary' class
20
+ sys.modules["inference"] = inference
21
+
22
+ spec.loader.exec_module(inference)
23
+ print("Inference script loaded successfully!")
24
+
25
+ # =========================================
26
+ # 2. Initialize Model
27
+ # =========================================
28
+ # The class inside inference.py handles downloading the weights/vocab automatically
29
+ restorer = inference.PunctuationRestorer(repo_id=REPO_ID)
30
+
31
+ # =========================================
32
+ # 3. Define Gradio Interface
33
+ # =========================================
34
+ def gradio_predict(text):
35
+ if not text or not text.strip():
36
+ return ""
37
+ return restorer.predict(text)
38
+
39
+ # Custom CSS to force Right-to-Left (RTL) alignment for Arabic
40
+ css = """
41
+ textarea { direction: rtl; }
42
+ #component-0 { direction: rtl; }
43
+ #component-1 { direction: rtl; }
44
+ """
45
+
46
+ iface = gr.Interface(
47
+ fn=gradio_predict,
48
+ inputs=gr.Textbox(
49
+ lines=3,
50
+ placeholder="اكتب النص العربي هنا...",
51
+ label="Input Text (النص المدخل)",
52
+ text_align="right",
53
+ rtl=True
54
+ ),
55
+ outputs=gr.Textbox(
56
+ label="Punctuated Text (النص المرقّم)",
57
+ text_align="right",
58
+ rtl=True,
59
+ show_copy_button=True
60
+ ),
61
+ title="Arabic Punctuation Restoration",
62
+ description="Enter raw Arabic text without punctuation to see the model predictions.",
63
+ examples=[
64
+ ["هل تساءلت يوما عن معنى الحياة ما هي الأسئلة التي تشغل بالك"],
65
+ ["الجو جميل اليوم لا اعتقد انها ستمطر"]
66
+ ],
67
+ theme="default"
68
+ )
69
+
70
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ huggingface-hub
3
+ numpy