oddadmix commited on
Commit
dd624c6
·
verified ·
1 Parent(s): 5ce4580

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -19
app.py CHANGED
@@ -1,27 +1,43 @@
1
  import spaces
2
- from transformers import AutoModelForSeq2SeqLM,AutoTokenizer, pipeline
3
  import gradio as grad
4
- mdl_name = "oddadmix/Masrawy-BiLingual-v1"
5
- pipe = pipeline("translation", model=mdl_name, device = 'cuda')
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  @spaces.GPU
8
- def translate(text, direction):
9
- # inputs = my_tkn(text, return_tensors="pt")
10
- # trans_output = mdl.generate(**inputs)
11
- if direction == "MSA → Egyptian":
12
- prompt = text + " <arz>"
13
- else:
14
- prompt = text + " <ar>"
15
- response = pipe(prompt)[0]['translation_text']
16
-
17
- #response = opus_translator(text)
18
- return response
 
 
 
 
 
 
 
 
19
 
20
  input_textbox = grad.Textbox(lines=5, placeholder="اكتب النص هنا بالعربية الفصحى أو باللهجة المصرية...", label="Input Text")
21
  output_textbox = grad.Textbox(lines=5, label="النص المترجم")
22
- grad.Interface(translate, inputs=[input_textbox, grad.Radio(
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()