playmak3r commited on
Commit
c893725
·
1 Parent(s): 076192d
Files changed (5) hide show
  1. .gitignore +2 -0
  2. app.py +78 -0
  3. languages.py +42 -0
  4. model.py +34 -0
  5. requirements.txt +4 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ __pycache__
2
+ models
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from model import run
3
+ from languages import language_names
4
+
5
+
6
+
7
+ example_text = "Now let's make my mum's favourite. So three mars bars into the pan. Then we add the tuna and just stir for a bit, just let the chocolate and fish infuse. A sprinkle of olive oil and some tomato ketchup. Now smell that. Oh boy this is going to be incredible."
8
+ css = """
9
+ body {
10
+ background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);
11
+ }
12
+ .gradio-container {
13
+ font-family: 'Inter', sans-serif;
14
+ }
15
+ textarea {
16
+ border-radius: 12px !important;
17
+ font-size: 16px !important;
18
+ }
19
+ button {
20
+ border-radius: 999px !important;
21
+ font-weight: 600 !important;
22
+ }
23
+ """
24
+
25
+ with gr.Blocks(
26
+ theme=gr.themes.Soft(
27
+ primary_hue="indigo",
28
+ secondary_hue="cyan",
29
+ neutral_hue="slate"
30
+ ),
31
+ css=css
32
+ ) as app:
33
+
34
+ gr.Markdown(
35
+ """
36
+ # 🌍 Universal Translator
37
+ Translate texts quickly between multiple languages.
38
+ Simple and functional.
39
+ """
40
+ )
41
+
42
+ with gr.Row():
43
+ with gr.Column(scale=1):
44
+ source_language = gr.Dropdown(
45
+ choices=language_names,
46
+ value="English",
47
+ label="Source Language"
48
+ )
49
+ input_text = gr.Textbox(
50
+ label="Original text",
51
+ placeholder="Please enter the text...",
52
+ lines=8,
53
+ value=example_text
54
+ )
55
+
56
+ with gr.Column(scale=1):
57
+ target_language = gr.Dropdown(
58
+ choices=language_names,
59
+ value="Portuguese",
60
+ label="Target Language"
61
+ )
62
+ output_text = gr.Textbox(
63
+ label="Translation",
64
+ lines=8,
65
+ interactive=False
66
+ )
67
+
68
+ translate_btn = gr.Button("Translate ✨")
69
+
70
+ translate_btn.click(
71
+ run,
72
+ inputs=[input_text, target_language],
73
+ outputs=output_text
74
+ )
75
+
76
+
77
+ if __name__ == "__main__":
78
+ app.queue(max_size=10).launch(mcp_server=True)
languages.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ languages = {
2
+ "Chinese": "zh",
3
+ "English": "en",
4
+ "French": "fr",
5
+ "Portuguese": "pt",
6
+ "Spanish": "es",
7
+ "Japanese": "ja",
8
+ "Turkish": "tr",
9
+ "Russian": "ru",
10
+ "Arabic": "ar",
11
+ "Korean": "ko",
12
+ "Thai": "th",
13
+ "Italian": "it",
14
+ "German": "de",
15
+ "Vietnamese": "vi",
16
+ "Malay": "ms",
17
+ "Indonesian": "id",
18
+ "Filipino": "tl",
19
+ "Hindi": "hi",
20
+ "Traditional Chinese": "zh-Hant",
21
+ "Polish": "pl",
22
+ "Czech": "cs",
23
+ "Dutch": "nl",
24
+ "Khmer": "km",
25
+ "Burmese": "my",
26
+ "Persian": "fa",
27
+ "Gujarati": "gu",
28
+ "Urdu": "ur",
29
+ "Telugu": "te",
30
+ "Marathi": "mr",
31
+ "Hebrew": "he",
32
+ "Bengali": "bn",
33
+ "Tamil": "ta",
34
+ "Ukrainian": "uk",
35
+ "Tibetan": "bo",
36
+ "Kazakh": "kk",
37
+ "Mongolian": "mn",
38
+ "Uyghur": "ug",
39
+ "Cantonese": "yue"
40
+ }
41
+
42
+ language_names = list(languages.keys())
model.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import snapshot_download
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+
4
+
5
+ model_id = "tencent/HY-MT1.5-1.8B"
6
+ local_dir = "./models"
7
+ model_path = snapshot_download(model_id, local_dir=local_dir)
8
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
9
+ model = AutoModelForCausalLM.from_pretrained(model_path)
10
+
11
+ def run(
12
+ text: str = "It’s on the house.",
13
+ target_language: str = "Portuguese",
14
+ ):
15
+ messages = [
16
+ {
17
+ "role": "user",
18
+ "content": f"Translate the following segment into {target_language}, without additional explanation.\n\n{text}"
19
+ },
20
+ ]
21
+ tokenized_chat = tokenizer.apply_chat_template(
22
+ messages,
23
+ tokenize=True,
24
+ add_generation_prompt=False,
25
+ return_tensors="pt"
26
+ )
27
+
28
+ outputs = model.generate(tokenized_chat.to(model.device), max_new_tokens=2048)
29
+ output_text = tokenizer.decode(outputs[0])
30
+ return output_text
31
+
32
+ if __name__ == "__main__":
33
+ translated_text = run("Now let's make my mum's favourite. So three mars bars into the pan. Then we add the tuna and just stir for a bit, just let the chocolate and fish infuse. A sprinkle of olive oil and some tomato ketchup. Now smell that. Oh boy this is going to be incredible.")
34
+ print(translated_text)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ huggingface_hub
3
+ transformers
4
+ torch