s2049 commited on
Commit
c514c47
·
verified ·
1 Parent(s): 891e1b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -7
app.py CHANGED
@@ -1,13 +1,11 @@
1
  # app.py (Translation Space)
2
  import gradio as gr
3
- from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
4
- import os
5
 
6
  # Initialize models
7
  try:
8
- tokenizer = AutoTokenizer.from_pretrained("marefa-nlp/allamalang-mt5-large")
9
- model = AutoModelForSeq2SeqLM.from_pretrained("marefa-nlp/allamalang-mt5-large")
10
- translator = pipeline("translation", model=model, tokenizer=tokenizer)
11
  print("Translation model loaded successfully!")
12
  except Exception as e:
13
  print(f"Error loading translation model: {e}")
@@ -15,7 +13,12 @@ except Exception as e:
15
 
16
  def translate(input_text, source_lang, target_lang):
17
  try:
18
- translation = translator(input_text, src_lang=source_lang, tgt_lang=target_lang)[0]['translation_text']
 
 
 
 
 
19
  return translation
20
  except Exception as e:
21
  print(f"Error in translation: {e}")
@@ -23,7 +26,7 @@ def translate(input_text, source_lang, target_lang):
23
 
24
  # Gradio interface
25
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
26
- gr.Markdown("# 🌍 Language Translation (Allamalang)")
27
 
28
  with gr.Row():
29
  source_lang = gr.Dropdown(["ar", "en"], label="Source Language")
 
1
  # app.py (Translation Space)
2
  import gradio as gr
3
+ from transformers import pipeline
 
4
 
5
  # Initialize models
6
  try:
7
+ translator_ar_en = pipeline("translation_ar_to_en", model="Helsinki-NLP/opus-mt-ar-en")
8
+ translator_en_ar = pipeline("translation_en_to_ar", model="Helsinki-NLP/opus-mt-en-ar")
 
9
  print("Translation model loaded successfully!")
10
  except Exception as e:
11
  print(f"Error loading translation model: {e}")
 
13
 
14
  def translate(input_text, source_lang, target_lang):
15
  try:
16
+ if source_lang == "ar" and target_lang == "en":
17
+ translation = translator_ar_en(input_text)[0]['translation_text']
18
+ elif source_lang == "en" and target_lang == "ar":
19
+ translation = translator_en_ar(input_text)[0]['translation_text']
20
+ else:
21
+ return "Invalid language combination"
22
  return translation
23
  except Exception as e:
24
  print(f"Error in translation: {e}")
 
26
 
27
  # Gradio interface
28
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
29
+ gr.Markdown("# 🌍 Language Translation")
30
 
31
  with gr.Row():
32
  source_lang = gr.Dropdown(["ar", "en"], label="Source Language")