Mediocreatmybest commited on
Commit
7534093
0 Parent(s):

Duplicate from Mediocreatmybest/PipelineTranslator

Browse files
Files changed (4) hide show
  1. .gitattributes +35 -0
  2. README.md +14 -0
  3. app.py +81 -0
  4. requirements.txt +8 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: PipelineTranslator
3
+ emoji: 🏃
4
+ colorFrom: indigo
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 3.36.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ duplicated_from: Mediocreatmybest/PipelineTranslator
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+ import ast
5
+
6
+ translation_task_names = {
7
+ 'English to French': 'translation_en_to_fr',
8
+ # 'French to English': 'translation_fr_to_en',
9
+ # 'English to Spanish': 'translation_en_to_es',
10
+ # 'Spanish to English': 'translation_es_to_en',
11
+ 'English to German': 'translation_en_to_de',
12
+ # 'German to English': 'translation_de_to_en',
13
+ # 'English to Italian': 'translation_en_to_it',
14
+ # 'Italian to English': 'translation_it_to_en',
15
+ 'English to Dutch': 'translation_en_to_nl',
16
+ 'Dutch to English': 'translation_nl_to_en',
17
+ # 'English to Portuguese': 'translation_en_to_pt',
18
+ # 'Portuguese to English': 'translation_pt_to_en',
19
+ 'English to Russian': 'translation_en_to_ru',
20
+ 'Russian to English': 'translation_ru_to_en',
21
+ 'English to Chinese': 'translation_en_to_zh',
22
+ 'Chinese to English': 'translation_zh_to_en',
23
+ # 'English to Japanese': 'translation_en_to_ja',
24
+ # 'Japanese to English': 'translation_ja_to_en',
25
+ 'English to Romanian': 'translation_en_to_ro',
26
+ 'Swedish to English': 'translation_SV_to_EN',
27
+ }
28
+
29
+ model_names = {
30
+ 'T5-Base': 't5-base',
31
+ 'T5-Small': 't5-small',
32
+ 'T5-Large': 't5-large',
33
+ 'Opus-En-ZH': 'liam168/trans-opus-mt-en-zh',
34
+ 'Opus-ZH-En': 'Helsinki-NLP/opus-mt-zh-en',
35
+ 'DDDSSS/translation_en-zh': 'DDDSSS/translation_en-zh',
36
+ 'T5-Base-nl-en': 'yhavinga/t5-base-36L-ccmatrix-multi',
37
+ 'T5-Small-nl-en': 'yhavinga/t5-small-24L-ccmatrix-multi',
38
+ 'Opus-Sv-En': 'Helsinki-NLP/opus-mt-sv-en',
39
+ 'Opus-En-Ru': 'Helsinki-NLP/opus-mt-en-ru',
40
+ 'Opus-Ru-En': 'Helsinki-NLP/opus-mt-ru-en',
41
+ }
42
+
43
+ # Create a dictionary to store loaded models
44
+ loaded_models = {}
45
+
46
+ # Simple translation function
47
+ def translate_text(model_choice, task_choice, text_input, load_in_8bit, device):
48
+ model_key = (model_choice, task_choice, load_in_8bit) # Create a tuple to represent the unique combination of task and 8bit loading
49
+
50
+ # Check if the model is already loaded
51
+ if model_key in loaded_models:
52
+ translator = loaded_models[model_key]
53
+ else:
54
+ model_kwargs = {"load_in_8bit": load_in_8bit} if load_in_8bit else {}
55
+ dtype = torch.float16 if load_in_8bit else torch.float32 # Set dtype based on the value of load_in_8bit
56
+ translator = pipeline(task=translation_task_names[task_choice],
57
+ model=model_names[model_choice], # Use selected model
58
+ device=device, # Use selected device
59
+ model_kwargs=model_kwargs,
60
+ torch_dtype=dtype, # Set the floating point
61
+ use_fast=True
62
+ )
63
+ # Store the loaded model
64
+ loaded_models[model_key] = translator
65
+
66
+ translation = translator(text_input)[0]['translation_text']
67
+ return str(translation).strip()
68
+
69
+ def launch(model_choice, task_choice, text_input, load_in_8bit, device):
70
+ return translate_text(model_choice, task_choice, text_input, load_in_8bit, device)
71
+
72
+ model_dropdown = gr.Dropdown(choices=list(model_names.keys()), label='Select Model')
73
+ task_dropdown = gr.Dropdown(choices=list(translation_task_names.keys()), label='Select Translation Task')
74
+ text_input = gr.Textbox(label="Input Text") # Single line text input
75
+ load_in_8bit = gr.Checkbox(label="Load model in 8bit")
76
+ # https://www.gradio.app/docs/radio
77
+ device = gr.Radio(['cpu', 'cuda'], label='Select device', value='cpu')
78
+
79
+ iface = gr.Interface(launch, inputs=[model_dropdown, task_dropdown, text_input, load_in_8bit, device],
80
+ outputs=gr.Textbox(type="text", label="Translation"))
81
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ accelerate
4
+ Pillow
5
+ bitsandbytes
6
+ scipy
7
+ sentencepiece
8
+ sacremoses