Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
|
| 3 |
+
|
| 4 |
+
# Load the translation model and tokenizer from Hugging Face
|
| 5 |
+
# Here, we use the "Helsinki-NLP" translation models
|
| 6 |
+
model_name = "Helsinki-NLP/opus-mt-en-fr" # Example model for English to French
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 9 |
+
translator = pipeline("translation", model=model, tokenizer=tokenizer)
|
| 10 |
+
|
| 11 |
+
# Define the translation function
|
| 12 |
+
def translate_text(text, target_language):
|
| 13 |
+
model_name = f"Helsinki-NLP/opus-mt-en-{target_language}"
|
| 14 |
+
translator = pipeline("translation", model=model_name)
|
| 15 |
+
translation = translator(text)[0]['translation_text']
|
| 16 |
+
return translation
|
| 17 |
+
|
| 18 |
+
# Set up the Gradio interface
|
| 19 |
+
languages = {
|
| 20 |
+
"en": "English",
|
| 21 |
+
"es": "Spanish",
|
| 22 |
+
"fr": "French",
|
| 23 |
+
"de": "German",
|
| 24 |
+
"zh": "Chinese (Mandarin)",
|
| 25 |
+
"ja": "Japanese",
|
| 26 |
+
"ko": "Korean",
|
| 27 |
+
"it": "Italian",
|
| 28 |
+
"pt": "Portuguese",
|
| 29 |
+
"ru": "Russian",
|
| 30 |
+
"hi": "Hindi",
|
| 31 |
+
"ar": "Arabic",
|
| 32 |
+
"nl": "Dutch",
|
| 33 |
+
"tr": "Turkish",
|
| 34 |
+
"el": "Greek",
|
| 35 |
+
"ur": "Urdu"
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
iface = gr.Interface(
|
| 40 |
+
fn=translate_text,
|
| 41 |
+
inputs=[
|
| 42 |
+
gr.inputs.Textbox(label="Enter text to translate"),
|
| 43 |
+
gr.inputs.Dropdown(list(languages.keys()), label="Target Language", type="value")
|
| 44 |
+
],
|
| 45 |
+
outputs="text",
|
| 46 |
+
live=True,
|
| 47 |
+
title="Text Translator",
|
| 48 |
+
description="Translate text into multiple languages using Hugging Face models."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
iface.launch()
|