Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,43 @@
|
|
| 1 |
import spaces
|
| 2 |
-
from transformers import
|
| 3 |
import gradio as grad
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
@spaces.GPU
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
input_textbox = grad.Textbox(lines=5, placeholder="اكتب النص هنا بالعربية الفصحى أو باللهجة المصرية...", label="Input Text")
|
| 21 |
output_textbox = grad.Textbox(lines=5, label="النص المترجم")
|
| 22 |
-
grad.Interface(
|
| 23 |
-
choices=["MSA → Egyptian", "Egyptian → MSA"],
|
| 24 |
-
value="MSA → Egyptian",
|
| 25 |
-
label="اتجاه الترجمة"
|
| 26 |
-
)], outputs=output_textbox, title="Masrawy: MSA ↔ Egyptian Translator",
|
| 27 |
description="اختر اتجاه الترجمة وأدخل النص بالعربية الفصحى أو باللهجة المصرية.").launch()
|
|
|
|
| 1 |
import spaces
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import gradio as grad
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
model_id = "oddadmix/arabic-summarization"
|
| 7 |
+
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_id,
|
| 10 |
+
device_map="auto",
|
| 11 |
+
torch_dtype="bfloat16",
|
| 12 |
+
attn_implementation="flash_attention_2"
|
| 13 |
+
)
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
|
| 19 |
@spaces.GPU
|
| 20 |
+
def summarize(text):
|
| 21 |
+
input_ids = tokenizer.apply_chat_template(
|
| 22 |
+
[{"role": "user", "content": prompt}],
|
| 23 |
+
add_generation_prompt=True,
|
| 24 |
+
return_tensors="pt",
|
| 25 |
+
tokenize=True,
|
| 26 |
+
).to(model.device)
|
| 27 |
+
|
| 28 |
+
output = model.generate(
|
| 29 |
+
input_ids,
|
| 30 |
+
do_sample=True,
|
| 31 |
+
temperature=0.3,
|
| 32 |
+
min_p=0.15,
|
| 33 |
+
repetition_penalty=1.05,
|
| 34 |
+
max_new_tokens=512,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
response = tokenizer.decode(output[0], skip_special_tokens=False)
|
| 38 |
+
return response.split("<|im_start|>assistant")[1]
|
| 39 |
|
| 40 |
input_textbox = grad.Textbox(lines=5, placeholder="اكتب النص هنا بالعربية الفصحى أو باللهجة المصرية...", label="Input Text")
|
| 41 |
output_textbox = grad.Textbox(lines=5, label="النص المترجم")
|
| 42 |
+
grad.Interface(summarize, inputs=[input_textbox], outputs=output_textbox, title="Masrawy: MSA ↔ Egyptian Translator",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
description="اختر اتجاه الترجمة وأدخل النص بالعربية الفصحى أو باللهجة المصرية.").launch()
|