Hzzzzx0 commited on
Commit
39986f4
·
verified ·
1 Parent(s): dbf62b2

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +154 -0
app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Gradio demo for bilingual oral translation using Qwen3-0.6B + LoRA.
2
+
3
+ This app provides a simple interface for Chinese ↔ English oral translation
4
+ using a LoRA fine-tuned Qwen3-0.6B model.
5
+ """
6
+
7
+ import gradio as gr
8
+ import torch
9
+ from transformers import AutoModelForCausalLM, AutoTokenizer
10
+ from peft import PeftModel
11
+
12
+
13
+ def build_prompt(direction: str, text: str) -> str:
14
+ """Build the instruction prompt for a given translation direction."""
15
+ if direction == "zh2en":
16
+ inst = "请把下面中文翻译成口语自然的英文。只输出译文。"
17
+ else:
18
+ inst = "请把下面英文翻译成口语自然的中文。只输出译文。"
19
+ return f"### Instruction:\n{inst}\n\n### Input:\n{text}\n\n### Response:\n"
20
+
21
+
22
+ def load_model():
23
+ """Load the base model and LoRA adapter."""
24
+ base_model_name = "Qwen/Qwen3-0.6B"
25
+ adapter_path = "Hzzzzx0/qwen3-0.6b-oral-lora" # You'll need to upload your model here
26
+
27
+ print(f"Loading base model: {base_model_name}")
28
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name, trust_remote_code=True)
29
+ model = AutoModelForCausalLM.from_pretrained(
30
+ base_model_name,
31
+ torch_dtype=torch.bfloat16,
32
+ device_map="auto",
33
+ trust_remote_code=True,
34
+ )
35
+
36
+ print(f"Loading LoRA adapter: {adapter_path}")
37
+ model = PeftModel.from_pretrained(model, adapter_path)
38
+ model.eval()
39
+
40
+ return model, tokenizer
41
+
42
+
43
+ # Load model at startup
44
+ print("Initializing model...")
45
+ model, tokenizer = load_model()
46
+ print("Model loaded successfully!")
47
+
48
+
49
+ def translate(direction: str, text: str) -> str:
50
+ """Translate text using the LoRA fine-tuned model."""
51
+ if not text.strip():
52
+ return "请输入要翻译的文本 / Please enter text to translate"
53
+
54
+ prompt = build_prompt(direction, text)
55
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
56
+
57
+ with torch.no_grad():
58
+ output = model.generate(
59
+ **inputs,
60
+ max_new_tokens=128,
61
+ do_sample=False,
62
+ repetition_penalty=1.2,
63
+ eos_token_id=tokenizer.eos_token_id,
64
+ pad_token_id=tokenizer.pad_token_id,
65
+ )
66
+
67
+ result = tokenizer.decode(output[0], skip_special_tokens=True)
68
+
69
+ # Extract only the response part
70
+ if "### Response:" in result:
71
+ return result.split("### Response:")[-1].strip()
72
+ return result
73
+
74
+
75
+ # Define example inputs
76
+ examples = [
77
+ ["zh2en", "你好呀"],
78
+ ["zh2en", "今天天气真不错"],
79
+ ["zh2en", "我们去吃饭吧"],
80
+ ["en2zh", "See you later"],
81
+ ["en2zh", "How are you doing?"],
82
+ ["en2zh", "Let's grab some coffee"],
83
+ ]
84
+
85
+
86
+ # Create Gradio interface
87
+ with gr.Blocks(title="口语化机器翻译 | Oral Translation", theme=gr.themes.Soft()) as demo:
88
+ gr.Markdown(
89
+ """
90
+ # 🌐 口语化自动机器翻译
91
+ ## Oral Machine Translation (Chinese ↔ English)
92
+
93
+ 基于 **Qwen3-0.6B + LoRA** 微调的中英双向口语翻译系统
94
+
95
+ Built with Qwen3-0.6B fine-tuned using LoRA for natural, conversational translation.
96
+ """
97
+ )
98
+
99
+ with gr.Row():
100
+ with gr.Column():
101
+ direction = gr.Radio(
102
+ choices=[
103
+ ("中文 → 英文 (Chinese to English)", "zh2en"),
104
+ ("英文 → 中文 (English to Chinese)", "en2zh"),
105
+ ],
106
+ value="zh2en",
107
+ label="翻译方向 | Translation Direction",
108
+ )
109
+
110
+ input_text = gr.Textbox(
111
+ lines=5,
112
+ placeholder="输入要翻译的文本...\nEnter text to translate...",
113
+ label="输入 | Input",
114
+ )
115
+
116
+ translate_btn = gr.Button("🔄 翻译 | Translate", variant="primary")
117
+
118
+ with gr.Column():
119
+ output_text = gr.Textbox(
120
+ lines=5,
121
+ label="翻译结果 | Translation",
122
+ )
123
+
124
+ gr.Examples(
125
+ examples=examples,
126
+ inputs=[direction, input_text],
127
+ outputs=output_text,
128
+ fn=translate,
129
+ cache_examples=False,
130
+ )
131
+
132
+ translate_btn.click(
133
+ fn=translate,
134
+ inputs=[direction, input_text],
135
+ outputs=output_text,
136
+ )
137
+
138
+ gr.Markdown(
139
+ """
140
+ ---
141
+ ### 📊 模型信息 | Model Info
142
+ - **基础模型 | Base Model**: Qwen3-0.6B
143
+ - **微调方法 | Fine-tuning**: LoRA (rank=16, alpha=32)
144
+ - **训练数据 | Training Data**: OpenSubtitles (5K samples)
145
+ - **BLEU Score**: 11.89 (vs 1.24 baseline, +858% improvement)
146
+
147
+ ### 🔗 相关链接 | Links
148
+ - [GitHub Repository](https://github.com/yourusername/mt-qwen-oral)
149
+ - [Model Card](https://huggingface.co/Hzzzzx0/qwen3-0.6b-oral-lora)
150
+ """
151
+ )
152
+
153
+ if __name__ == "__main__":
154
+ demo.launch()