File size: 7,437 Bytes
135276f
57d7889
 
a024afa
 
135276f
 
 
 
 
 
 
 
a024afa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57d7889
 
a024afa
 
 
 
 
 
 
 
 
 
 
57d7889
a024afa
 
 
 
57d7889
a024afa
 
 
 
 
 
 
57d7889
a024afa
 
 
 
 
 
57d7889
a024afa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57d7889
a024afa
 
 
57d7889
a024afa
 
 
 
 
 
 
 
 
 
 
 
 
57d7889
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import os
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from trl import AutoModelForCausalLMWithValueHead
from huggingface_hub import login

# Set your Hugging Face token as an environment variable
# You can also use os.environ["HUGGINGFACE_TOKEN"] = "your_token_here" in your code
# But using environment variables outside the code is more secure

# Authenticate with Hugging Face
login(token=os.environ.get("LA_NAME"))

# Set device and dtype
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch_dtype = torch.bfloat16

# Load models only once at startup
print("Loading models...")
model_id = "meta-llama/Meta-Llama-3.1-8B"  # Replace with your actual model ID
tokenizer = AutoTokenizer.from_pretrained(model_id)
lm_model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch_dtype,
    device_map="auto"
)

# Load the reward model
RM = AutoModelForCausalLMWithValueHead.from_pretrained(
    'ray24724919/plan2align_rm',
    torch_dtype=torch_dtype,
    device_map="auto"
)
RM.eval()
print("Models loaded successfully!")

# Self-contained translation and evaluation functions
def translate(source_text, target_language="English"):
    """
    Translate text from Chinese to the specified target language.
    
    Args:
        source_text (str): The Chinese text to translate
        target_language (str): The target language for translation
        
    Returns:
        str: The translated text
    """
    # Format the input as per the system prompt
    messages = [
        {"role": "system", "content": "You are a helpful translator and only output the result."},
        {"role": "user", "content": f"### Translate this from Chinese to {target_language}, Chinese:\n{source_text}\n### {target_language}:"}
    ]
    
    # Format messages for the model
    prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    
    # Tokenize the input
    inputs = tokenizer(prompt, return_tensors="pt").to(device)
    
    # Generate translation
    with torch.no_grad():
        outputs = lm_model.generate(
            **inputs,
            max_new_tokens=512,
            temperature=0.7,
            do_sample=True,
            pad_token_id=tokenizer.eos_token_id
        )
    
    # Decode the generated text
    translation = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True).strip()
    return translation

def evaluate_translation(source_text, translation, target_language="English"):
    """
    Evaluate the quality of a translation using the reward model.
    
    Args:
        source_text (str): The original Chinese text
        translation (str): The translated text
        target_language (str): The target language of the translation
        
    Returns:
        float: The reward score
    """
    messages = [
        {"role": "system", "content": "You are a helpful translator and only output the result."},
        {"role": "user", "content": f"### Translate this from Chinese to {target_language}, Chinese:\n{source_text}\n### {target_language}:"},
        {"role": "assistant", "content": translation}
    ]
    
    # Format messages for the reward model
    prompt = tokenizer.apply_chat_template(messages, tokenize=False)
    
    # Tokenize the input
    inputs = tokenizer(prompt, return_tensors="pt").to(device)
    
    # Get reward score
    with torch.no_grad():
        outputs = RM(input_ids=inputs.input_ids)
        reward_score = outputs.value.item()
    
    return reward_score

# Combined function for the Gradio interface
def translate_text(source_text, target_language):
    """
    Translate text and get reward score
    
    Args:
        source_text (str): The Chinese text to translate
        target_language (str): The target language for translation
        
    Returns:
        tuple: (translation, reward_score)
    """
    if not source_text.strip():
        return "Please enter some text to translate.", 0.0
    
    try:
        translation = translate(source_text, target_language)
        reward_score = evaluate_translation(source_text, translation, target_language)
        return translation, float(reward_score)
    except Exception as e:
        return f"Error: {str(e)}", 0.0

# Define available target languages
target_languages = [
    "English", "French", "Spanish", "German", "Italian", 
    "Portuguese", "Russian", "Japanese", "Korean", "Arabic"
]

# Create the Gradio interface
with gr.Blocks(title="Chinese Translation with Reward Scoring") as demo:
    gr.Markdown("# Chinese to Any Language Translation")
    gr.Markdown("This demo translates Chinese text to your chosen language and provides a quality score from our reward model.")
    
    with gr.Row():
        with gr.Column():
            source_text = gr.Textbox(
                label="Chinese Text",
                placeholder="Enter Chinese text here...",
                lines=5
            )
            target_language = gr.Dropdown(
                choices=target_languages,
                value="English",
                label="Target Language"
            )
            translate_button = gr.Button("Translate")
        
        with gr.Column():
            translation_output = gr.Textbox(
                label="Translation",
                lines=5,
                interactive=False
            )
            reward_score = gr.Number(
                label="Translation Quality Score (higher is better)",
                precision=4,
                interactive=False
            )
            
            with gr.Row():
                score_indicator = gr.Label(label="Quality Rating")
    
    # Function to update the quality rating based on score
    def update_quality_rating(score):
        if score >= 0.8:
            return "Excellent"
        elif score >= 0.6:
            return "Good"
        elif score >= 0.4:
            return "Average"
        elif score >= 0.2:
            return "Poor"
        else:
            return "Very Poor"
    
    # Set up the translation flow
    translate_outputs = translate_button.click(
        fn=translate_text,
        inputs=[source_text, target_language],
        outputs=[translation_output, reward_score]
    )
    
    # Update the quality rating whenever the reward score changes
    reward_score.change(
        fn=update_quality_rating,
        inputs=[reward_score],
        outputs=[score_indicator]
    )
    
    # Examples
    gr.Examples(
        examples=[
            ["你好,世界!", "English"],
            ["我喜欢学习新的语言。", "Spanish"],
            ["北京烤鴨很好吃。", "French"],
            ["人工智能正在改变世界。", "German"],
            ["今天天气真好。", "Japanese"]
        ],
        inputs=[source_text, target_language],
        outputs=[translation_output, reward_score],
        fn=translate_text
    )
    
    gr.Markdown("## How It Works")
    gr.Markdown("""
    1. Enter Chinese text in the input box
    2. Select your desired target language
    3. Click 'Translate' to get the translation
    4. The system will display the translation and a quality score
    
    The quality score is generated by a reward model trained to evaluate translation quality.
    Higher scores indicate better translations.
    """)

# Launch the app
if __name__ == "__main__":
    demo.launch()