CalamityJon commited on
Commit
3554820
·
verified ·
1 Parent(s): 1c5dbb8

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py CHANGED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "Ukranian": pipeline("translation_en_to_uk", model="Helsinki-NLP/opus-mt-en-uk"),
11
+ "Russian": pipeline("translation_en_to_ru", model="Helsinki-NLP/opus-mt-en-ru"),
12
+ }
13
+
14
+ def translate(text, language):
15
+ if not text:
16
+ return "Please enter an English sentence."
17
+
18
+ # Perform translation using the selected model
19
+ result = models[language](text)
20
+ return result[0]['translation_text']
21
+
22
+ # Create the Gradio interface
23
+ with gr.Blocks(title="Short Translation Web App") as demo:
24
+ gr.Markdown("# Short Translation")
25
+
26
+ with gr.Row():
27
+ with gr.Column(scale=2):
28
+ input_text = gr.Textbox(
29
+ label="English Sentence",
30
+ placeholder="Type your English sentence here...",
31
+ lines=2
32
+ )
33
+ with gr.Row():
34
+ translate_btn = gr.Button("Translate", variant="primary")
35
+ clear_btn = gr.Button("Clear")
36
+
37
+ with gr.Column(scale=1):
38
+ lang_radio = gr.Radio(
39
+ choices=["Spanish", "German", "Japanese", "Ukranian", "Russian"],
40
+ label="Translation Language",
41
+ value="Spanish"
42
+ )
43
+
44
+ # Output area styled to resemble the maroon box (using Gradio's color system)
45
+ output_text = gr.Textbox(
46
+ label="Translation",
47
+ interactive=False,
48
+ container=True,
49
+ elem_id="output-box"
50
+ )
51
+
52
+ # Custom CSS to mimic the maroon look from the Tkinter app
53
+ demo.css = """
54
+ #output-box textarea {
55
+ background-color: #800000 !important;
56
+ color: white !important;
57
+ font-size: 1.2em !important;
58
+ font-weight: bold !important;
59
+ text-align: center !important;
60
+ }
61
+ """
62
+
63
+ # Define button actions
64
+ translate_btn.click(fn=translate, inputs=[input_text, lang_radio], outputs=output_text)
65
+ clear_btn.click(fn=lambda: ("", ""), inputs=None, outputs=[input_text, output_text])
66
+
67
+ # Launch the app
68
+ if __name__ == "__main__":
69
+ demo.launch()