s2049 commited on
Commit
9e40ee9
·
verified ·
1 Parent(s): c514c47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -12
app.py CHANGED
@@ -1,28 +1,34 @@
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}")
12
- raise
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}")
25
- return "Translation Error"
26
 
27
  # Gradio interface
28
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
@@ -32,13 +38,18 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
32
  source_lang = gr.Dropdown(["ar", "en"], label="Source Language")
33
  target_lang = gr.Dropdown(["ar", "en"], label="Target Language")
34
 
35
- text_input = gr.Textbox(label="Input Text", lines=5)
36
 
37
- translation_output = gr.Textbox(label="Translated Text")
38
 
39
  submit = gr.Button("Translate", variant="primary")
40
 
41
- submit.click(translate,
 
 
 
 
 
42
  inputs=[text_input, source_lang, target_lang],
43
  outputs=[translation_output])
44
 
 
1
  # app.py (Translation Space)
2
  import gradio as gr
3
  from transformers import pipeline
4
+ import asyncio
5
 
6
  # Initialize models
7
  try:
8
  translator_ar_en = pipeline("translation_ar_to_en", model="Helsinki-NLP/opus-mt-ar-en")
9
  translator_en_ar = pipeline("translation_en_to_ar", model="Helsinki-NLP/opus-mt-en-ar")
10
+ print("Translation models loaded successfully!")
11
  except Exception as e:
12
+ print(f"Error loading translation models: {e}")
13
+ raise # Stop execution if models fail to load
14
 
15
+ # Translation function (Batched)
16
+ async def translate(texts, source_lang, target_lang):
17
  try:
18
+ if not texts:
19
+ return [] # Handle empty input
20
+
21
  if source_lang == "ar" and target_lang == "en":
22
+ translations = translator_ar_en(texts)
23
+ return [translation['translation_text'] for translation in translations]
24
  elif source_lang == "en" and target_lang == "ar":
25
+ translations = translator_en_ar(texts)
26
+ return [translation['translation_text'] for translation in translations]
27
  else:
28
+ return ["Invalid language combination"] * len(texts) # Error for each input
 
29
  except Exception as e:
30
  print(f"Error in translation: {e}")
31
+ return ["Translation Error"] * len(texts) # Indicate error for each input
32
 
33
  # Gradio interface
34
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
38
  source_lang = gr.Dropdown(["ar", "en"], label="Source Language")
39
  target_lang = gr.Dropdown(["ar", "en"], label="Target Language")
40
 
41
+ text_input = gr.Textbox(label="Input Text (Separate multiple texts with newlines)", lines=5)
42
 
43
+ translation_output = gr.Textbox(label="Translated Text", lines=5)
44
 
45
  submit = gr.Button("Translate", variant="primary")
46
 
47
+ async def process_translation(text_input, source_lang, target_lang):
48
+ texts = text_input.split("\n") # Split input into multiple texts
49
+ translations = await translate(texts, source_lang, target_lang)
50
+ return "\n".join(translations) # Join translations with newlines
51
+
52
+ submit.click(process_translation,
53
  inputs=[text_input, source_lang, target_lang],
54
  outputs=[translation_output])
55