MaiquelQuerales1977 commited on
Commit
124c955
·
verified ·
1 Parent(s): fd2d450

Upload 2 files

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