Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ from transformers import pipeline
|
|
4 |
import os
|
5 |
import spaces
|
6 |
|
7 |
-
# Define models for each dialect
|
8 |
MODELS = {
|
9 |
"Moroccan": "BounharAbdelaziz/ModernBERT-Arabic-base-stage-3-decay-mx8192-ARY",
|
10 |
"Arabic": "BounharAbdelaziz/ModernBERT-Arabic-base-stage-3-decay-mx8192-MSA",
|
@@ -13,7 +12,32 @@ MODELS = {
|
|
13 |
"Algerian": "BounharAbdelaziz/ModernBERT-Arabic-base-stage-3-decay-mx8192-ALGERIAN",
|
14 |
}
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
18 |
|
19 |
def load_model(dialect):
|
@@ -22,77 +46,94 @@ def load_model(dialect):
|
|
22 |
|
23 |
pipe = None
|
24 |
|
25 |
-
@spaces.GPU
|
26 |
def predict(text, dialect):
|
27 |
global pipe
|
28 |
-
if pipe is None or dialect != predict.current_dialect:
|
29 |
pipe = load_model(dialect)
|
30 |
predict.current_dialect = dialect
|
31 |
outputs = pipe(text)
|
32 |
-
|
|
|
|
|
33 |
|
|
|
34 |
predict.current_dialect = None
|
35 |
|
36 |
-
# Create
|
37 |
with gr.Blocks() as demo:
|
38 |
with gr.Row():
|
39 |
with gr.Column():
|
|
|
40 |
dialect_dropdown = gr.Dropdown(
|
41 |
choices=["Arabic", "Tunisian", "Moroccan", "Algerian", "Egyptian"],
|
42 |
-
|
43 |
-
|
44 |
)
|
45 |
|
|
|
46 |
input_text = gr.Textbox(
|
47 |
label="Input",
|
48 |
-
placeholder="Enter text
|
49 |
rtl=True
|
50 |
)
|
51 |
|
52 |
-
#
|
53 |
-
examples_html = gr.HTML(
|
54 |
-
value="<div id='examples'>Examples: " +
|
55 |
-
" | ".join([f"<a href='#' onclick='return false;'>{ex}</a>" for ex in EXAMPLES["Arabic"]]) +
|
56 |
-
"</div>"
|
57 |
-
)
|
58 |
-
|
59 |
with gr.Row():
|
60 |
clear_btn = gr.Button("Clear")
|
61 |
submit_btn = gr.Button("Submit", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
with gr.Column():
|
64 |
-
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
#
|
67 |
-
|
68 |
-
|
69 |
-
None,
|
70 |
-
None,
|
71 |
-
_js="""
|
72 |
-
() => {
|
73 |
-
document.querySelectorAll('#examples a').forEach(a => {
|
74 |
-
a.addEventListener('click', (e) => {
|
75 |
-
const text = e.target.textContent;
|
76 |
-
document.querySelector('textarea[data-testid="textbox"]').value = text;
|
77 |
-
});
|
78 |
-
});
|
79 |
-
}
|
80 |
-
"""
|
81 |
-
)
|
82 |
|
83 |
-
#
|
84 |
-
def
|
85 |
-
|
86 |
-
html = "<div id='examples'>Examples: " + " | ".join([f"<a href='#' onclick='return false;'>{ex}</a>" for ex in examples]) + "</div>"
|
87 |
-
return gr.HTML.update(value=html)
|
88 |
|
|
|
89 |
dialect_dropdown.change(
|
90 |
-
|
91 |
inputs=dialect_dropdown,
|
92 |
-
outputs=
|
93 |
)
|
94 |
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
|
|
98 |
demo.launch()
|
|
|
4 |
import os
|
5 |
import spaces
|
6 |
|
|
|
7 |
MODELS = {
|
8 |
"Moroccan": "BounharAbdelaziz/ModernBERT-Arabic-base-stage-3-decay-mx8192-ARY",
|
9 |
"Arabic": "BounharAbdelaziz/ModernBERT-Arabic-base-stage-3-decay-mx8192-MSA",
|
|
|
12 |
"Algerian": "BounharAbdelaziz/ModernBERT-Arabic-base-stage-3-decay-mx8192-ALGERIAN",
|
13 |
}
|
14 |
|
15 |
+
EXAMPLES = {
|
16 |
+
"Moroccan": [
|
17 |
+
"الدار البيضاء <mask>",
|
18 |
+
"المغرب بلاد <mask>",
|
19 |
+
"كناكل <mask> فالمغرب",
|
20 |
+
"العاصمة د <mask> هي الرباط",
|
21 |
+
"المغرب <mask> زوين",
|
22 |
+
"انا سميتي مريم، و كنسكن ف<mask> العاصمة دفلسطين"
|
23 |
+
],
|
24 |
+
"Arabic": [
|
25 |
+
"العاصمة الرسمية لمصر هي <mask>.",
|
26 |
+
"أطول نهر في العالم هو نهر <mask>.",
|
27 |
+
"الشاعر العربي المشهور <mask> كتب قصيدة 'أراك عصي الدمع'.",
|
28 |
+
"عندما أستيقظ في الصباح، أشرب فنجان من <mask>.",
|
29 |
+
"في التأني <mask> وفي العجلة الندامة.",
|
30 |
+
"معركة <mask> كانت من أهم المعارك في تاريخ الإسلام.",
|
31 |
+
"يعتبر <mask> من أهم العلماء في مجال الفيزياء.",
|
32 |
+
"تقع جبال <mask> في شمال إفريقيا.",
|
33 |
+
"يعتبر <mask> من أركان الإسلام الخمسة."
|
34 |
+
],
|
35 |
+
"Egyptian": ["القاهرة مدينة <mask>", "مصر بلاد <mask>", "بنحب <mask> فمصر"],
|
36 |
+
"Tunisian": ["تونس بلاد <mask>", "المنستير مدينة <mask>", "عيشتي في <mask>"],
|
37 |
+
"Algerian": ["الجزائر بلاد <mask>", "قسنطينة مدينة <mask>", "نحبو <mask> ف الجزائر"],
|
38 |
+
}
|
39 |
+
|
40 |
+
TOKEN = os.environ["HF_TOKEN"]
|
41 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
42 |
|
43 |
def load_model(dialect):
|
|
|
46 |
|
47 |
pipe = None
|
48 |
|
49 |
+
@spaces.GPU
|
50 |
def predict(text, dialect):
|
51 |
global pipe
|
52 |
+
if pipe is None or dialect != predict.current_dialect: # Reload model if dialect changes
|
53 |
pipe = load_model(dialect)
|
54 |
predict.current_dialect = dialect
|
55 |
outputs = pipe(text)
|
56 |
+
scores = [x["score"] for x in outputs]
|
57 |
+
tokens = [x["token_str"] for x in outputs]
|
58 |
+
return {label: float(prob) for label, prob in zip(tokens, scores)}
|
59 |
|
60 |
+
# Initialize current dialect
|
61 |
predict.current_dialect = None
|
62 |
|
63 |
+
# Create Gradio interface
|
64 |
with gr.Blocks() as demo:
|
65 |
with gr.Row():
|
66 |
with gr.Column():
|
67 |
+
# Dropdown for dialect selection
|
68 |
dialect_dropdown = gr.Dropdown(
|
69 |
choices=["Arabic", "Tunisian", "Moroccan", "Algerian", "Egyptian"],
|
70 |
+
label="Select Dialect",
|
71 |
+
value="Arabic"
|
72 |
)
|
73 |
|
74 |
+
# Input text box
|
75 |
input_text = gr.Textbox(
|
76 |
label="Input",
|
77 |
+
placeholder="Enter text here...",
|
78 |
rtl=True
|
79 |
)
|
80 |
|
81 |
+
# Button row
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
with gr.Row():
|
83 |
clear_btn = gr.Button("Clear")
|
84 |
submit_btn = gr.Button("Submit", variant="primary")
|
85 |
+
|
86 |
+
# Examples section (stateful)
|
87 |
+
examples_state = gr.State(EXAMPLES["Arabic"]) # Store examples for the current dialect
|
88 |
+
|
89 |
+
example_dropdown = gr.Dropdown(
|
90 |
+
choices=EXAMPLES["Arabic"],
|
91 |
+
label="Select Example",
|
92 |
+
interactive=True
|
93 |
+
)
|
94 |
+
|
95 |
+
load_example_btn = gr.Button("Load Example")
|
96 |
|
97 |
with gr.Column():
|
98 |
+
# Output probabilities
|
99 |
+
output_labels = gr.Label(
|
100 |
+
label="Prediction Results",
|
101 |
+
show_label=False
|
102 |
+
)
|
103 |
|
104 |
+
# Function to update examples when dialect changes
|
105 |
+
def update_example_choices(dialect):
|
106 |
+
return gr.update(choices=EXAMPLES.get(dialect, EXAMPLES["Arabic"]), value=EXAMPLES[dialect][0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
+
# Function to load selected example into the text box
|
109 |
+
def load_example(selected_example):
|
110 |
+
return selected_example
|
|
|
|
|
111 |
|
112 |
+
# Update example dropdown choices dynamically
|
113 |
dialect_dropdown.change(
|
114 |
+
update_example_choices,
|
115 |
inputs=dialect_dropdown,
|
116 |
+
outputs=example_dropdown
|
117 |
)
|
118 |
|
119 |
+
# Load selected example into the input text box
|
120 |
+
load_example_btn.click(
|
121 |
+
load_example,
|
122 |
+
inputs=example_dropdown,
|
123 |
+
outputs=input_text
|
124 |
+
)
|
125 |
+
|
126 |
+
# Button actions
|
127 |
+
submit_btn.click(
|
128 |
+
predict,
|
129 |
+
inputs=[input_text, dialect_dropdown],
|
130 |
+
outputs=output_labels
|
131 |
+
)
|
132 |
+
|
133 |
+
clear_btn.click(
|
134 |
+
lambda: "",
|
135 |
+
outputs=input_text
|
136 |
+
)
|
137 |
|
138 |
+
# Launch the app
|
139 |
demo.launch()
|