Spaces:
Sleeping
Sleeping
import os | |
import sys | |
import subprocess | |
import streamlit as st | |
def clone_repo(): | |
# 从环境变量中获取 GitHub Token | |
github_token = os.getenv('GH_TOKEN') | |
if github_token is None: | |
st.error("GitHub token is not set. Please set the GH_TOKEN secret in your Space settings.") | |
return False | |
# 使用 GitHub Token 进行身份验证并克隆仓库 | |
clone_command = f'git clone https://{github_token}@github.com/mamba-ai/translate_agent.git' | |
if os.path.exists('translate_agent'): | |
# st.warning("Repository already exists.") | |
return True | |
else: | |
# st.info("Cloning repository...") | |
result = subprocess.run(clone_command, shell=True, capture_output=True, text=True) | |
if result.returncode == 0: | |
# st.success("Repository cloned successfully!") | |
repo_dir = 'translate_agent' | |
# 将仓库路径添加到 Python 模块搜索路径中 | |
sys.path.append(os.path.abspath(repo_dir)) | |
return True | |
else: | |
st.error(f"Failed to clone repository: {result.stderr}") | |
return False | |
if clone_repo(): | |
# 克隆成功后导入模块 | |
import translate_agent.agent as ta | |
# 创建一个语言选项列表 | |
languages_src = ["中国語", "日本語"] | |
languages_tgt = ["日本語", "中国語"] | |
lang_country_map = { | |
"中国語": "China", | |
"日本語": "Japan", | |
} | |
# 设置页面标题 | |
st.title("MambaTranslate デモ") | |
# 用户选择源语言和目标语言 | |
source_lang = st.selectbox("ソース言語を選択", list(languages_src)) | |
target_lang = st.selectbox("ターゲット言語を選択", list(languages_tgt)) | |
country = lang_country_map[target_lang] | |
print(f"Source language: {source_lang}; Target language: {target_lang}; Country: {country}") | |
# 输入要翻译的文本 | |
source_text = st.text_area("翻訳するテキストを入力", height=200) | |
print(f"Source text:\n\n{source_text}\n------------\n") | |
# 按钮触发翻译 | |
if st.button("Translate"): | |
if source_text: | |
with st.spinner("Translating..."): | |
translation = ta.translate( | |
source_lang=source_lang, | |
target_lang=target_lang, | |
source_text=source_text, | |
country=country, | |
) | |
st.success("翻訳完了!") | |
st.text_area("翻訳されたテキスト", translation, height=200) | |
else: | |
st.warning("翻訳するテキストを入力してください。") | |
else: | |
# st.error("Repository could not be cloned. Please check the settings and try again.") | |
pass | |