Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,37 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import MarianMTModel, MarianTokenizer
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
return "Translation between the same languages is not supported."
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 16 |
-
model = MarianMTModel.from_pretrained(model_name)
|
| 17 |
-
except Exception as e:
|
| 18 |
-
return f"Failed to load model for {source_language_code} to {target_language_code}: {str(e)}"
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
return translated_text
|
| 25 |
-
|
| 26 |
-
# Define language options (ISO 639-1 codes and names)
|
| 27 |
-
language_options = [
|
| 28 |
-
('en', 'English (en)'),
|
| 29 |
-
('es', 'Spanish (es)'),
|
| 30 |
-
('fr', 'French (fr)'),
|
| 31 |
-
('de', 'German (de)'),
|
| 32 |
-
('zh', 'Chinese (zh)'),
|
| 33 |
-
('ru', 'Russian (ru)'),
|
| 34 |
-
('ar', 'Arabic (ar)'),
|
| 35 |
-
('it', 'Italian (it)'),
|
| 36 |
-
('pt', 'Portuguese (pt)'),
|
| 37 |
-
('nl', 'Dutch (nl)'),
|
| 38 |
-
# Add more languages as needed
|
| 39 |
-
]
|
| 40 |
|
| 41 |
# Create dropdowns for source and target languages, using only the codes for value
|
| 42 |
-
source_language_dropdown = gr.Dropdown(choices=
|
| 43 |
-
target_language_dropdown = gr.Dropdown(choices=
|
| 44 |
|
| 45 |
# Define the interface
|
| 46 |
iface = gr.Interface(
|
| 47 |
fn=translate_text,
|
| 48 |
inputs=[gr.Textbox(lines=2, placeholder="Enter text to translate..."), source_language_dropdown, target_language_dropdown],
|
| 49 |
outputs=gr.Textbox(),
|
| 50 |
-
title="Text Translator with Dynamic
|
| 51 |
-
description="Select source and target languages to translate text
|
| 52 |
)
|
| 53 |
|
| 54 |
# Launch the app
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import MarianMTModel, MarianTokenizer
|
| 5 |
|
| 6 |
+
# Fetch and parse language options from the provided URL
|
| 7 |
+
url = "https://huggingface.co/Lenylvt/LanguageISO/resolve/main/iso.md"
|
| 8 |
+
response = requests.get(url)
|
| 9 |
+
# Assuming the response content is a Markdown table, convert it to a DataFrame
|
| 10 |
+
df = pd.read_csv(url, delimiter="|", skiprows=2, header=None).dropna(axis=1, how='all')
|
| 11 |
+
df.columns = ['ISO 639-1', 'ISO 639-2', 'Language Name', 'Native Name']
|
| 12 |
+
df['ISO 639-1'] = df['ISO 639-1'].str.strip()
|
| 13 |
|
| 14 |
+
# Prepare language options for the dropdown
|
| 15 |
+
language_options = [(row['ISO 639-1'], f"{row['Language Name']} ({row['ISO 639-1']})") for index, row in df.iterrows()]
|
|
|
|
| 16 |
|
| 17 |
+
# Your translate_text function and Gradio interface setup goes here
|
| 18 |
+
# Replace the previous static language_options list with the dynamic one created above
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Example translate_text function placeholder
|
| 21 |
+
def translate_text(text, source_language, target_language):
|
| 22 |
+
return "Translation function implementation"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Create dropdowns for source and target languages, using only the codes for value
|
| 25 |
+
source_language_dropdown = gr.Dropdown(choices=language_options, label="Source Language")
|
| 26 |
+
target_language_dropdown = gr.Dropdown(choices=language_options, label="Target Language")
|
| 27 |
|
| 28 |
# Define the interface
|
| 29 |
iface = gr.Interface(
|
| 30 |
fn=translate_text,
|
| 31 |
inputs=[gr.Textbox(lines=2, placeholder="Enter text to translate..."), source_language_dropdown, target_language_dropdown],
|
| 32 |
outputs=gr.Textbox(),
|
| 33 |
+
title="Text Translator with Dynamic Language Options",
|
| 34 |
+
description="Select source and target languages to translate text."
|
| 35 |
)
|
| 36 |
|
| 37 |
# Launch the app
|