Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +67 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load models locally on the Hugging Face Space server
|
| 5 |
+
# Note: In a real space, you can also use Inference Endpoints for even more speed
|
| 6 |
+
models = {
|
| 7 |
+
"Spanish": pipeline("translation_en_to_es", model="Helsinki-NLP/opus-mt-en-es"),
|
| 8 |
+
"German": pipeline("translation_en_to_de", model="Helsinki-NLP/opus-mt-en-de"),
|
| 9 |
+
"Japanese": pipeline("translation_en_to_ja", model="staka/fugumt-en-ja")
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
def translate(text, language):
|
| 13 |
+
if not text:
|
| 14 |
+
return "Please enter an English sentence."
|
| 15 |
+
|
| 16 |
+
# Perform translation using the selected model
|
| 17 |
+
result = models[language](text)
|
| 18 |
+
return result[0]['translation_text']
|
| 19 |
+
|
| 20 |
+
# Create the Gradio interface
|
| 21 |
+
with gr.Blocks(title="Short Translation Web App") as demo:
|
| 22 |
+
gr.Markdown("# Short Translation")
|
| 23 |
+
|
| 24 |
+
with gr.Row():
|
| 25 |
+
with gr.Column(scale=2):
|
| 26 |
+
input_text = gr.Textbox(
|
| 27 |
+
label="English Sentence",
|
| 28 |
+
placeholder="Type your English sentence here...",
|
| 29 |
+
lines=2
|
| 30 |
+
)
|
| 31 |
+
with gr.Row():
|
| 32 |
+
translate_btn = gr.Button("Translate", variant="primary")
|
| 33 |
+
clear_btn = gr.Button("Clear")
|
| 34 |
+
|
| 35 |
+
with gr.Column(scale=1):
|
| 36 |
+
lang_radio = gr.Radio(
|
| 37 |
+
choices=["Spanish", "German", "Japanese"],
|
| 38 |
+
label="Translation Language",
|
| 39 |
+
value="Spanish"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Output area styled to resemble the maroon box (using Gradio's color system)
|
| 43 |
+
output_text = gr.Textbox(
|
| 44 |
+
label="Translation",
|
| 45 |
+
interactive=False,
|
| 46 |
+
container=True,
|
| 47 |
+
elem_id="output-box"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Custom CSS to mimic the maroon look from the Tkinter app
|
| 51 |
+
demo.css = """
|
| 52 |
+
#output-box textarea {
|
| 53 |
+
background-color: #800000 !important;
|
| 54 |
+
color: white !important;
|
| 55 |
+
font-size: 1.2em !important;
|
| 56 |
+
font-weight: bold !important;
|
| 57 |
+
text-align: center !important;
|
| 58 |
+
}
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
# Define button actions
|
| 62 |
+
translate_btn.click(fn=translate, inputs=[input_text, lang_radio], outputs=output_text)
|
| 63 |
+
clear_btn.click(fn=lambda: ("", ""), inputs=None, outputs=[input_text, output_text])
|
| 64 |
+
|
| 65 |
+
# Launch the app
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
sentencepiece
|