Upload 3 files
Browse files- README.md +6 -7
- app.py +122 -0
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
title: Hy
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.14.0
|
| 8 |
python_version: '3.12'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
-
-
|
| 12 |
-
|
| 13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Hy-MT2
|
| 3 |
+
emoji: 🌍
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.14.0
|
| 8 |
python_version: '3.12'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
+
short_description: Hy-MT2 multilingual translation demo
|
| 12 |
+
---
|
|
|
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gc
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import spaces
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 6 |
+
|
| 7 |
+
current_model_id = None
|
| 8 |
+
tokenizer = None
|
| 9 |
+
model = None
|
| 10 |
+
|
| 11 |
+
LANGUAGES = [
|
| 12 |
+
"Arabic", "Bengali", "Burmese", "Cantonese", "Chinese", "Czech",
|
| 13 |
+
"Dutch", "English", "Filipino", "French", "German", "Gujarati",
|
| 14 |
+
"Hebrew", "Hindi", "Indonesian", "Italian", "Japanese", "Kazakh",
|
| 15 |
+
"Khmer", "Korean", "Malay", "Marathi", "Mongolian", "Persian",
|
| 16 |
+
"Polish", "Portuguese", "Russian", "Spanish", "Tamil", "Telugu",
|
| 17 |
+
"Thai", "Tibetan", "Traditional Chinese", "Turkish", "Ukrainian",
|
| 18 |
+
"Urdu", "Uyghur", "Vietnamese"
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
MODELS = [
|
| 22 |
+
"tencent/Hy-MT2-30B-A3B",
|
| 23 |
+
"tencent/Hy-MT2-7B",
|
| 24 |
+
"tencent/Hy-MT2-1.8B"
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
def load_model(model_id):
|
| 28 |
+
global current_model_id, tokenizer, model
|
| 29 |
+
|
| 30 |
+
if current_model_id == model_id:
|
| 31 |
+
return
|
| 32 |
+
|
| 33 |
+
print(f"Switching model from {current_model_id} to {model_id}...")
|
| 34 |
+
|
| 35 |
+
if model is not None:
|
| 36 |
+
del model
|
| 37 |
+
del tokenizer
|
| 38 |
+
gc.collect()
|
| 39 |
+
if torch.cuda.is_available():
|
| 40 |
+
torch.cuda.empty_cache()
|
| 41 |
+
|
| 42 |
+
print(f"Loading tokenizer for {model_id}...")
|
| 43 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 44 |
+
|
| 45 |
+
print(f"Loading model {model_id}...")
|
| 46 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 47 |
+
model_id,
|
| 48 |
+
torch_dtype=torch.bfloat16,
|
| 49 |
+
device_map="auto",
|
| 50 |
+
trust_remote_code=True,
|
| 51 |
+
)
|
| 52 |
+
model.eval()
|
| 53 |
+
current_model_id = model_id
|
| 54 |
+
print("Model loaded successfully.")
|
| 55 |
+
|
| 56 |
+
@spaces.GPU
|
| 57 |
+
def translate(source_text, target_lang, selected_model):
|
| 58 |
+
global current_model_id, tokenizer, model
|
| 59 |
+
|
| 60 |
+
if not source_text.strip():
|
| 61 |
+
return ""
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
load_model(selected_model)
|
| 65 |
+
|
| 66 |
+
prompt = f"Translate the following text into {target_lang}. Note that you should only output the translated result without any additional explanation:\n\n{source_text}"
|
| 67 |
+
messages = [{"role": "user", "content": prompt}]
|
| 68 |
+
|
| 69 |
+
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 70 |
+
|
| 71 |
+
if "30B" in selected_model:
|
| 72 |
+
gen_kwargs = {
|
| 73 |
+
"temperature": 0.7,
|
| 74 |
+
"top_p": 1.0,
|
| 75 |
+
"repetition_penalty": 1.0,
|
| 76 |
+
}
|
| 77 |
+
else:
|
| 78 |
+
gen_kwargs = {
|
| 79 |
+
"temperature": 0.7,
|
| 80 |
+
"top_p": 0.6,
|
| 81 |
+
"top_k": 20,
|
| 82 |
+
"repetition_penalty": 1.05,
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
with torch.no_grad():
|
| 86 |
+
outputs = model.generate(
|
| 87 |
+
**inputs,
|
| 88 |
+
max_new_tokens=4096,
|
| 89 |
+
**gen_kwargs
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
|
| 93 |
+
return response
|
| 94 |
+
|
| 95 |
+
except Exception as e:
|
| 96 |
+
return f"Error during generation: {str(e)}\n\n(Note: Zero GPU environments may timeout or run out of memory when loading large models dynamically.)"
|
| 97 |
+
|
| 98 |
+
with gr.Blocks(title="Hy-MT2 Translator") as demo:
|
| 99 |
+
gr.Markdown("# Hy-MT2 Translator")
|
| 100 |
+
gr.Markdown("https://huggingface.co/collections/tencent/hy-mt2")
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
with gr.Column():
|
| 104 |
+
source_text = gr.Textbox(label="Source Text", lines=8, placeholder="Enter text to translate...")
|
| 105 |
+
|
| 106 |
+
with gr.Row():
|
| 107 |
+
target_lang = gr.Dropdown(choices=LANGUAGES, value="English", label="Target Language")
|
| 108 |
+
model_selector = gr.Dropdown(choices=MODELS, value="tencent/Hy-MT2-1.8B", label="Model")
|
| 109 |
+
|
| 110 |
+
translate_btn = gr.Button("Translate", variant="primary")
|
| 111 |
+
|
| 112 |
+
with gr.Column():
|
| 113 |
+
output_text = gr.Textbox(label="Translated Text", lines=12, interactive=False)
|
| 114 |
+
|
| 115 |
+
translate_btn.click(
|
| 116 |
+
fn=translate,
|
| 117 |
+
inputs=[source_text, target_lang, model_selector],
|
| 118 |
+
outputs=output_text
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
spaces
|
| 3 |
+
torch
|
| 4 |
+
transformers>=5.6.0
|
| 5 |
+
accelerate
|
| 6 |
+
tqdm
|