Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,11 @@
|
|
| 1 |
# app.py (Translation Space)
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import pipeline
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
# Initialize models
|
| 7 |
try:
|
| 8 |
-
|
| 9 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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")
|