Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -13,123 +13,86 @@ MODELS = {
|
|
13 |
"Algerian": "BounharAbdelaziz/ModernBERT-Arabic-base-stage-3-decay-mx8192-ALGERIAN",
|
14 |
}
|
15 |
|
16 |
-
TOKEN = os.environ
|
17 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
18 |
|
19 |
-
# Load the pipeline dynamically based on the selected dialect
|
20 |
def load_model(dialect):
|
21 |
-
model_path = MODELS.get(dialect, MODELS["Arabic"])
|
22 |
-
|
23 |
-
task="fill-mask",
|
24 |
-
model=model_path,
|
25 |
-
token=TOKEN,
|
26 |
-
device=device
|
27 |
-
)
|
28 |
-
return pipe
|
29 |
|
30 |
-
# Store the pipeline globally
|
31 |
pipe = None
|
32 |
|
33 |
-
# Define examples for each dialect
|
34 |
-
EXAMPLES = {
|
35 |
-
"Moroccan": [
|
36 |
-
"الدار البيضاء [MASK]",
|
37 |
-
"المغرب بلاد [MASK]",
|
38 |
-
"كناكل [MASK] فالمغرب",
|
39 |
-
"العاصمة د [MASK] هي الرباط",
|
40 |
-
"المغرب [MASK] زوين",
|
41 |
-
"انا سميتي مريم، و كنسكن ف[MASK] العاصمة دفلسطين"
|
42 |
-
],
|
43 |
-
"Arabic": [
|
44 |
-
"العاصمة الرسمية لمصر هي [MASK].",
|
45 |
-
"أطول نهر في العالم هو نهر [MASK].",
|
46 |
-
"الشاعر العربي المشهور [MASK] كتب قصيدة 'أراك عصي الدمع'.",
|
47 |
-
"عندما أستيقظ في الصباح، أشرب فنجان من [MASK].",
|
48 |
-
"في التأني [MASK] وفي العجلة الندامة.",
|
49 |
-
"معركة [MASK] كانت من أهم المعارك في تاريخ الإسلام.",
|
50 |
-
"يعتبر [MASK] من أهم العلماء في مجال الفيزياء.",
|
51 |
-
"تقع جبال [MASK] في شمال إفريقيا.",
|
52 |
-
"يعتبر [MASK] من أركان الإسلام الخمسة."
|
53 |
-
],
|
54 |
-
"Egyptian": ["القاهرة مدينة [MASK]", "مصر بلاد [MASK]", "بنحب [MASK] فمصر"],
|
55 |
-
"Tunisian": ["تونس بلاد [MASK]", "المنستير مدينة [MASK]", "عيشتي في [MASK]"],
|
56 |
-
"Algerian": ["الجزائر بلاد [MASK]", "قسنطينة مدينة [MASK]", "نحبو [MASK] ف الجزائر"],
|
57 |
-
}
|
58 |
-
|
59 |
-
# Predict function
|
60 |
@spaces.GPU
|
61 |
def predict(text, dialect):
|
62 |
global pipe
|
63 |
-
if pipe is None or dialect != predict.current_dialect:
|
64 |
pipe = load_model(dialect)
|
65 |
predict.current_dialect = dialect
|
66 |
outputs = pipe(text)
|
67 |
-
|
68 |
-
tokens = [x["token_str"] for x in outputs]
|
69 |
-
return {label: float(prob) for label, prob in zip(tokens, scores)}
|
70 |
|
71 |
-
# Initialize current dialect
|
72 |
predict.current_dialect = None
|
73 |
|
74 |
-
# Create
|
75 |
with gr.Blocks() as demo:
|
76 |
with gr.Row():
|
77 |
with gr.Column():
|
78 |
-
# Dropdown for dialect selection
|
79 |
dialect_dropdown = gr.Dropdown(
|
80 |
choices=["Arabic", "Tunisian", "Moroccan", "Algerian", "Egyptian"],
|
81 |
-
|
82 |
-
|
83 |
)
|
84 |
|
85 |
-
# Input text box
|
86 |
input_text = gr.Textbox(
|
87 |
label="Input",
|
88 |
-
placeholder="Enter text
|
89 |
rtl=True
|
90 |
)
|
91 |
|
92 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
with gr.Row():
|
94 |
clear_btn = gr.Button("Clear")
|
95 |
submit_btn = gr.Button("Submit", variant="primary")
|
96 |
-
|
97 |
-
# Examples section (dynamic based on dialect)
|
98 |
-
examples = gr.Examples(
|
99 |
-
examples=EXAMPLES["Arabic"], # Default to Arabic examples
|
100 |
-
inputs=input_text,
|
101 |
-
label="Examples"
|
102 |
-
)
|
103 |
-
|
104 |
with gr.Column():
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
# Update examples when dialect changes
|
112 |
def update_examples(dialect):
|
113 |
-
|
|
|
|
|
114 |
|
115 |
-
# Update examples dynamically
|
116 |
dialect_dropdown.change(
|
117 |
-
|
118 |
inputs=dialect_dropdown,
|
119 |
-
outputs=
|
120 |
)
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
predict,
|
125 |
-
inputs=[input_text, dialect_dropdown],
|
126 |
-
outputs=output_labels
|
127 |
-
)
|
128 |
-
|
129 |
-
clear_btn.click(
|
130 |
-
lambda: "",
|
131 |
-
outputs=input_text
|
132 |
-
)
|
133 |
|
134 |
-
# Launch the app
|
135 |
demo.launch()
|
|
|
13 |
"Algerian": "BounharAbdelaziz/ModernBERT-Arabic-base-stage-3-decay-mx8192-ALGERIAN",
|
14 |
}
|
15 |
|
16 |
+
TOKEN = os.environ.get("HF_TOKEN", "")
|
17 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
18 |
|
|
|
19 |
def load_model(dialect):
|
20 |
+
model_path = MODELS.get(dialect, MODELS["Arabic"])
|
21 |
+
return pipeline(task="fill-mask", model=model_path, token=TOKEN, device=device)
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
return {x["token_str"]: float(x["score"]) for x in outputs}
|
|
|
|
|
33 |
|
|
|
34 |
predict.current_dialect = None
|
35 |
|
36 |
+
# Create the app
|
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 |
+
value="Arabic",
|
43 |
+
label="Select Dialect"
|
44 |
)
|
45 |
|
|
|
46 |
input_text = gr.Textbox(
|
47 |
label="Input",
|
48 |
+
placeholder="Enter text with <mask>...",
|
49 |
rtl=True
|
50 |
)
|
51 |
|
52 |
+
# Use a HTML component to display clickable examples
|
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 |
+
output_labels = gr.Label(label="Predictions")
|
65 |
+
|
66 |
+
# JavaScript to handle example clicks
|
67 |
+
demo.load(
|
68 |
+
None,
|
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 |
# Update examples when dialect changes
|
84 |
def update_examples(dialect):
|
85 |
+
examples = EXAMPLES.get(dialect, EXAMPLES["Arabic"])
|
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 |
+
update_examples,
|
91 |
inputs=dialect_dropdown,
|
92 |
+
outputs=examples_html
|
93 |
)
|
94 |
|
95 |
+
submit_btn.click(predict, inputs=[input_text, dialect_dropdown], outputs=output_labels)
|
96 |
+
clear_btn.click(lambda: "", outputs=input_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
|
|
98 |
demo.launch()
|