|
import os |
|
import gradio as gr |
|
import spaces |
|
import torch |
|
|
|
|
|
LANGUAGES = { |
|
"Auto Detect": "auto", |
|
"English": "en", |
|
"Chinese": "zh", |
|
"Russian": "ru", |
|
"Japanese": "ka", |
|
"Korean": "ko", |
|
"Spanish": "es", |
|
"French": "fr", |
|
"Portuguese": "pt", |
|
"German": "de", |
|
"Italian": "it", |
|
"Thai": "th", |
|
"Vietnamese": "vi", |
|
"Indonesian": "id", |
|
"Malay": "ms", |
|
"Arabic": "ar", |
|
"Polish": "pl", |
|
"Dutch": "nl", |
|
"Romanian": "ro", |
|
"Turkish": "tr", |
|
"Czech": "cs", |
|
"Danish": "da", |
|
"Finnish": "fi", |
|
"Ukrainian": "uk", |
|
"Norwegian Bokmal":"nb", |
|
"Norwegian":"no", |
|
"Croatian":"hr", |
|
"Swedish":"sv", |
|
"Hungarian":"hu" |
|
} |
|
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
device = "cuda" |
|
MODEL_NAME = "ByteDance-Seed/Seed-X-PPO-7B" |
|
print("Start dowload") |
|
def load_model(): |
|
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME,torch_dtype="bfloat16").to(device) |
|
print(f"Model loaded in {device}") |
|
return model |
|
|
|
model = load_model() |
|
print("Ednd dowload") |
|
|
|
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) |
|
|
|
def prompting(text, source_lang, target_lang): |
|
l = LANGUAGES[target_lang] |
|
prompt=f"Translate the following {source_lang} sentence into {target_lang} and explain it in detail:\n{text} <{l}>" |
|
return prompt |
|
|
|
@spaces.GPU(duration=120) |
|
def translate_text(text, source_lang, target_lang): |
|
if not text.strip(): |
|
return "请输入要翻译的文本" |
|
try: |
|
prompt = prompting(text, source_lang, target_lang) |
|
print(prompt) |
|
input_tokens = ( |
|
tokenizer(prompt, return_tensors="pt") |
|
.input_ids[0] |
|
.cpu() |
|
.numpy() |
|
.tolist() |
|
) |
|
translated_chunk = model.generate( |
|
input_ids=torch.tensor([input_tokens]).to(device), |
|
max_length=512, |
|
num_beams=4, |
|
num_return_sequences=1, |
|
) |
|
full_output = tokenizer.decode(translated_chunk[0], skip_special_tokens=True) |
|
full_output = full_output.replace(prompt.strip(),"") |
|
yield full_output |
|
except Exception as e: |
|
yield f"翻译出错: {str(e)}" |
|
|
|
|
|
description = """ |
|
<div style="text-align: center;"> |
|
<img src="https://github.com/user-attachments/assets/c42e675e-497c-4508-8bb9-093ad4d1f216" alt="Banner" style="max-width: 800px; width: 100%; margin: 0 auto;"> |
|
<h1 style="color: #0077be;">Seed-X: Building Strong Multilingual Translation LLM with 7B Parameters</h1> |
|
</div> |
|
""" |
|
|
|
|
|
with gr.Blocks(title="Seed-X") as demo: |
|
gr.Markdown(description) |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
source_text = gr.Textbox( |
|
label="Input Text", |
|
placeholder="Please enter the text to translate...", |
|
lines=5 |
|
) |
|
with gr.Row(): |
|
source_lang = gr.Dropdown( |
|
choices=list(LANGUAGES.keys()), |
|
value="Auto Detect", |
|
label="Source Language" |
|
) |
|
target_lang = gr.Dropdown( |
|
choices=list(LANGUAGES.keys())[1:], |
|
value="English", |
|
label="Target Language" |
|
) |
|
|
|
translate_btn = gr.Button("Translate", variant="primary") |
|
|
|
with gr.Column(): |
|
target_text = gr.Textbox( |
|
label="Translation Result", |
|
interactive=False, |
|
lines=5 |
|
) |
|
|
|
|
|
|
|
gr.Examples( |
|
examples=[ |
|
["我说一句你说一车啊", "Chinese", "English"], |
|
["INTJ总是装E", "Chinese", "English"], |
|
["Their relationship is a total situationship.", "English", "Chinese"], |
|
], |
|
inputs=[source_text, source_lang, target_lang], |
|
outputs=target_text, |
|
fn=translate_text, |
|
cache_examples=True |
|
) |
|
|
|
|
|
translate_btn.click( |
|
fn=translate_text, |
|
inputs=[source_text, source_lang, target_lang], |
|
outputs=target_text |
|
) |
|
|
|
|
|
source_text.submit( |
|
fn=translate_text, |
|
inputs=[source_text, source_lang, target_lang], |
|
outputs=target_text |
|
) |
|
|
|
gr.Markdown( |
|
"🌐[Github](https://github.com/ByteDance-Seed/Seed-X-7B) 📄[Report](https://arxiv.org/pdf/2507.13618) 🤗[Model](https://huggingface.co/collections/ByteDance-Seed/seed-x-6878753f2858bc17afa78543)" |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |