import gradio as gr from flores import code_mapping from functools import lru_cache import openai # 用于调用外部API import os print(os.getenv('key')) code_mapping = dict(sorted(code_mapping.items(), key=lambda item: item[0])) flores_codes = list(code_mapping.keys()) target_languages = flores_codes # 简化列表 # 假设openai_client已定义,例如: @lru_cache(maxsize=100) def translate(text: str, src_lang: str, tgt_lang: str): if not src_lang: raise gr.Error("The source language is empty! Please choose it in the dropdown list.") if not tgt_lang: raise gr.Error("The target language is empty! Please choose it in the dropdown list.") return _translate(text, src_lang, tgt_lang) def _translate(text: str, src_lang: str, tgt_lang: str): prompt = f"Translate the following text from {src_lang} to {tgt_lang}. Direct output translation result without any explaination:\n\n{text}" key=os.getenv('key') openai_client = openai.OpenAI(base_url="https://ssapi.cppbear.site/v1", api_key=key) response = openai_client.chat.completions.create( model="tbai.xin-dpsk-deepseek-v3", # 如gpt-3.5-turbo或其他兼容模型 messages=[{"role": "user", "content": prompt}], max_tokens=30240, temperature=0.0 ) print(response) return response.choices[0].message.content.strip() description = """