Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
-
#
|
| 2 |
from transformers import pipeline # Hugging Face transformers for translation
|
| 3 |
import gradio as gr # For building the web interface
|
| 4 |
|
| 5 |
-
#
|
| 6 |
MODELS = {
|
| 7 |
"T5-small (Fast)": "t5-small", # Smaller, faster model
|
| 8 |
"T5-base (Better Quality)": "t5-base" # Larger, more accurate model
|
|
@@ -20,18 +20,18 @@ def translate_text(text, model_name, max_length=50):
|
|
| 20 |
Returns:
|
| 21 |
str: Translated French text
|
| 22 |
"""
|
| 23 |
-
#
|
| 24 |
translator = pipeline(
|
| 25 |
"translation_en_to_fr",
|
| 26 |
model=MODELS[model_name], # Select model based on user choice
|
| 27 |
truncation=True # Ensure long texts are truncated properly
|
| 28 |
)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
result = translator(text, max_length=max_length)[0]["translation_text"]
|
| 32 |
return result
|
| 33 |
|
| 34 |
-
#
|
| 35 |
with gr.Blocks(title="Advanced English→French Translator") as demo:
|
| 36 |
# ===== HEADER SECTION =====
|
| 37 |
gr.Markdown("## 🌍 English to French Translator")
|
|
|
|
| 1 |
+
# Importing required libraries
|
| 2 |
from transformers import pipeline # Hugging Face transformers for translation
|
| 3 |
import gradio as gr # For building the web interface
|
| 4 |
|
| 5 |
+
# Defining available models with descriptive names
|
| 6 |
MODELS = {
|
| 7 |
"T5-small (Fast)": "t5-small", # Smaller, faster model
|
| 8 |
"T5-base (Better Quality)": "t5-base" # Larger, more accurate model
|
|
|
|
| 20 |
Returns:
|
| 21 |
str: Translated French text
|
| 22 |
"""
|
| 23 |
+
# Initializing the translation pipeline
|
| 24 |
translator = pipeline(
|
| 25 |
"translation_en_to_fr",
|
| 26 |
model=MODELS[model_name], # Select model based on user choice
|
| 27 |
truncation=True # Ensure long texts are truncated properly
|
| 28 |
)
|
| 29 |
|
| 30 |
+
# Performing translation with length control
|
| 31 |
result = translator(text, max_length=max_length)[0]["translation_text"]
|
| 32 |
return result
|
| 33 |
|
| 34 |
+
# Creating Gradio interface using Blocks for advanced layout
|
| 35 |
with gr.Blocks(title="Advanced English→French Translator") as demo:
|
| 36 |
# ===== HEADER SECTION =====
|
| 37 |
gr.Markdown("## 🌍 English to French Translator")
|