Spaces:
Running
Running
File size: 6,453 Bytes
f84f5ba 170c546 f84f5ba 8a18a49 f84f5ba |
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 |
import os
import gradio as gr
from openai import OpenAI
# 初始化 OpenAI 客户端
client = OpenAI(
api_key=os.environ['API_KEY'], # 请确保已设置环境变量,或直接填入你的 API Key
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
# 支持的语言选项
LANGUAGES = {
"Auto Detect": "auto",
"English": "English",
"Chinese": "Chinese",
"Traditional Chinese": "Traditional Chinese",
"Russian": "Russian",
"Japanese": "Japanese",
"Korean": "Korean",
"Spanish": "Spanish",
"French": "French",
"Portuguese": "Portuguese",
"German": "German",
"Italian": "Italian",
"Thai": "Thai",
"Vietnamese": "Vietnamese",
"Indonesian": "Indonesian",
"Malay": "Malay",
"Arabic": "Arabic",
"Hindi": "Hindi",
"Hebrew": "Hebrew",
"Burmese": "Burmese",
"Tamil": "Tamil",
"Urdu": "Urdu",
"Bengali": "Bengali",
"Polish": "Polish",
"Dutch": "Dutch",
"Romanian": "Romanian",
"Turkish": "Turkish",
"Khmer": "Khmer",
"Lao": "Lao",
"Cantonese": "Cantonese",
"Czech": "Czech",
"Greek": "Greek",
"Swedish": "Swedish",
"Hungarian": "Hungarian",
"Danish": "Danish",
"Finnish": "Finnish",
"Ukrainian": "Ukrainian",
"Bulgarian": "Bulgarian",
"Serbian": "Serbian",
"Telugu": "Telugu",
"Afrikaans": "Afrikaans",
"Armenian": "Armenian",
"Assamese": "Assamese",
"Asturian": "Asturian",
"Basque": "Basque",
"Belarusian": "Belarusian",
"Bosnian": "Bosnian",
"Catalan": "Catalan",
"Cebuano": "Cebuano",
"Croatian": "Croatian",
"Egyptian Arabic": "Egyptian Arabic",
"Estonian": "Estonian",
"Galician": "Galician",
"Georgian": "Georgian",
"Gujarati": "Gujarati",
"Icelandic": "Icelandic",
"Javanese": "Javanese",
"Kannada": "Kannada",
"Kazakh": "Kazakh",
"Latvian": "Latvian",
"Lithuanian": "Lithuanian",
"Luxembourgish": "Luxembourgish",
"Macedonian": "Macedonian",
"Maithili": "Maithili",
"Maltese": "Maltese",
"Marathi": "Marathi",
"Mesopotamian Arabic": "Mesopotamian Arabic",
"Moroccan Arabic": "Moroccan Arabic",
"Najdi Arabic": "Najdi Arabic",
"Nepali": "Nepali",
"North Azerbaijani": "North Azerbaijani",
"North Levantine Arabic": "North Levantine Arabic",
"Northern Uzbek": "Northern Uzbek",
"Norwegian Bokmål": "Norwegian Bokmål",
"Norwegian Nynorsk": "Norwegian Nynorsk",
"Occitan": "Occitan",
"Odia": "Odia",
"Pangasinan": "Pangasinan",
"Sicilian": "Sicilian",
"Sindhi": "Sindhi",
"Sinhala": "Sinhala",
"Slovak": "Slovak",
"Slovenian": "Slovenian",
"South Levantine Arabic": "South Levantine Arabic",
"Swahili": "Swahili",
"Tagalog": "Tagalog",
"Ta’izzi-Adeni Arabic": "Ta’izzi-Adeni Arabic",
"Tosk Albanian": "Tosk Albanian",
"Tunisian Arabic": "Tunisian Arabic",
"Venetian": "Venetian",
"Waray": "Waray",
"Welsh": "Welsh",
"Western Persian": "Western Persian"
}
def translate_text(text, source_lang, target_lang):
"""
调用阿里云百炼的翻译模型进行翻译
"""
if not text.strip():
return "请输入要翻译的文本"
# 构造消息内容
messages = [{"role": "user", "content": text}]
# 构造翻译选项
translation_options = {
"source_lang": LANGUAGES.get(source_lang, "auto"),
"target_lang": LANGUAGES.get(target_lang, "English")
}
try:
# 调用模型
completion = client.chat.completions.create(
model="qwen-mt-turbo",
messages=messages,
stream=True,
max_tokens=2048,
extra_body={"translation_options": translation_options})
response = ''
for chunk in completion:
response = chunk.choices[0].delta.content
yield response
except Exception as e:
yield f"翻译出错: {str(e)}"
# 创建 Gradio 界面
with gr.Blocks(title="Qwen3-MT Translator") as demo:
gr.Markdown("# 🌍 Qwen3-MT Translator")
gr.Markdown(
'A real-time translation tool based on the Qwen3-MT model<br><a href="https://www.alibabacloud.com/help/en/model-studio/translation-abilities" target="_blank">Learn more about Qwen3-MT and API documentation</a>',
elem_id="desc"
)
gr.Image(
value="https://modelscope.oss-cn-beijing.aliyuncs.com/resource/Qwen3-MT.png",
label="Qwen3-MT Supported Languages",
show_label=False,
show_download_button=False,
interactive=False,
height=60
)
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:], # Exclude "Auto Detect"
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"],
["Hello, how are you today?", "English", "Chinese"],
["私は学生です。", "Japanese", "Chinese"],
["Bonjour, comment allez-vous?", "French", "English"]
],
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
)
# 启动应用
if __name__ == "__main__":
demo.launch() |