Spaces:
Sleeping
Sleeping
File size: 2,714 Bytes
62f49b5 |
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 |
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
|