ailan-ai commited on
Commit
6d52e55
·
verified ·
1 Parent(s): dd0e3e4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+
5
+ # 设置设备,如果有GPU则使用GPU
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
7
+
8
+ # 加载基础模型和分词器
9
+ tokenizer_base = AutoTokenizer.from_pretrained("tencent/Hunyuan-MT-7B", trust_remote_code=True)
10
+ model_base = AutoModelForCausalLM.from_pretrained("tencent/Hunyuan-MT-7B", trust_remote_code=True).to(device)
11
+
12
+ # 加载Chimera集成模型和分词器
13
+ tokenizer_chimera = AutoTokenizer.from_pretrained("tencent/Hunyuan-MT-Chimera-7B", trust_remote_code=True)
14
+ model_chimera = AutoModelForCausalLM.from_pretrained("tencent/Hunyuan-MT-Chimera-7B", trust_remote_code=True).to(device)
15
+
16
+
17
+ def translate(model_choice, text_to_translate, source_lang, target_lang):
18
+ """
19
+ 根据选择的模型进行翻译
20
+ """
21
+ if model_choice == "Hunyuan-MT-7B (基础版)":
22
+ tokenizer = tokenizer_base
23
+ model = model_base
24
+ # 基础版Prompt模板
25
+ prompt = f"Translate the following text from {source_lang} to {target_lang}:\n{text_to_translate}"
26
+ else: # Chimera-7B
27
+ tokenizer = tokenizer_chimera
28
+ model = model_chimera
29
+ # Chimera版需要一个特殊的、包含候选翻译的模板,这里我们简化一下,
30
+ # 实际应用中会先用基础模型生成多个候选。为简化Demo,我们直接套用基础模板。
31
+ # 官方的Chimera用法更复杂,需要输入多个候选翻译进行精炼。
32
+ prompt = f"Translate the following text from {source_lang} to {target_lang}:\n{text_to_translate}"
33
+
34
+ inputs = tokenizer(prompt, return_tensors="pt").to(device)
35
+
36
+ # 生成翻译结果
37
+ output = model.generate(**inputs, max_new_tokens=256)
38
+
39
+ # 解码并清理结果
40
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
41
+
42
+ # 移除prompt部分,只返回翻译结果
43
+ translated_text = response.replace(prompt, "").strip()
44
+
45
+ return translated_text
46
+
47
+ # --- 创建Gradio界面 ---
48
+ with gr.Blocks() as demo:
49
+ gr.Markdown("# 腾讯混元翻译模型体验Demo")
50
+ gr.Markdown("选择一个模型,输入源语言、目标语言和待翻译的文本。")
51
+
52
+ with gr.Row():
53
+ model_selector = gr.Radio(
54
+ ["Hunyuan-MT-7B (基础版)", "Hunyuan-MT-Chimera-7B (集成优化版)"],
55
+ label="选择模型",
56
+ value="Hunyuan-MT-7B (基础版)"
57
+ )
58
+
59
+ with gr.Row():
60
+ source_language = gr.Textbox(label="源语言", value="Chinese")
61
+ target_language = gr.Textbox(label="目标语言", value="English")
62
+
63
+ input_text = gr.Textbox(label="输入文本", lines=5, placeholder="在这里输入需要翻译的文本...")
64
+ output_text = gr.Textbox(label="翻译结果", lines=5)
65
+
66
+ translate_button = gr.Button("开始翻译")
67
+
68
+ translate_button.click(
69
+ fn=translate,
70
+ inputs=[model_selector, input_text, source_language, target_language],
71
+ outputs=output_text
72
+ )
73
+
74
+ # 启动应用
75
+ demo.launch()