Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,12 @@
|
|
1 |
-
import torch
|
2 |
import gradio as gr
|
3 |
import json
|
4 |
from transformers import pipeline
|
5 |
|
6 |
-
|
7 |
-
# Initialize the pipeline
|
8 |
try:
|
9 |
-
text_translator = pipeline("translation", model=
|
10 |
except Exception as e:
|
11 |
-
raise ValueError(f"Error initializing the model
|
12 |
|
13 |
# Load the JSON data for language codes
|
14 |
try:
|
@@ -19,7 +17,6 @@ except FileNotFoundError:
|
|
19 |
except json.JSONDecodeError:
|
20 |
raise ValueError("Error decoding the JSON file. Ensure it is formatted correctly.")
|
21 |
|
22 |
-
|
23 |
# Create a function to get the FLORES-200 code for a given language
|
24 |
def get_FLORES_code_from_language(language):
|
25 |
for entry in language_data:
|
@@ -27,7 +24,6 @@ def get_FLORES_code_from_language(language):
|
|
27 |
return entry['FLORES-200 code']
|
28 |
return None
|
29 |
|
30 |
-
|
31 |
# Translation function
|
32 |
def translate_text(text, destination_language):
|
33 |
if not text.strip():
|
@@ -38,12 +34,12 @@ def translate_text(text, destination_language):
|
|
38 |
return f"Language '{destination_language}' is not supported."
|
39 |
|
40 |
try:
|
|
|
41 |
translation = text_translator(text, src_lang="eng_Latn", tgt_lang=dest_code)
|
42 |
return translation[0]["translation_text"]
|
43 |
except Exception as e:
|
44 |
return f"Error during translation: {e}"
|
45 |
|
46 |
-
|
47 |
# Extract available languages from JSON
|
48 |
available_languages = [entry['Language'] for entry in language_data]
|
49 |
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import json
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# Initialize the pipeline with the model
|
|
|
6 |
try:
|
7 |
+
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M")
|
8 |
except Exception as e:
|
9 |
+
raise ValueError(f"Error initializing the model: {e}")
|
10 |
|
11 |
# Load the JSON data for language codes
|
12 |
try:
|
|
|
17 |
except json.JSONDecodeError:
|
18 |
raise ValueError("Error decoding the JSON file. Ensure it is formatted correctly.")
|
19 |
|
|
|
20 |
# Create a function to get the FLORES-200 code for a given language
|
21 |
def get_FLORES_code_from_language(language):
|
22 |
for entry in language_data:
|
|
|
24 |
return entry['FLORES-200 code']
|
25 |
return None
|
26 |
|
|
|
27 |
# Translation function
|
28 |
def translate_text(text, destination_language):
|
29 |
if not text.strip():
|
|
|
34 |
return f"Language '{destination_language}' is not supported."
|
35 |
|
36 |
try:
|
37 |
+
# Perform translation
|
38 |
translation = text_translator(text, src_lang="eng_Latn", tgt_lang=dest_code)
|
39 |
return translation[0]["translation_text"]
|
40 |
except Exception as e:
|
41 |
return f"Error during translation: {e}"
|
42 |
|
|
|
43 |
# Extract available languages from JSON
|
44 |
available_languages = [entry['Language'] for entry in language_data]
|
45 |
|