Spaces:
Sleeping
Sleeping
Commit
·
62f49b5
1
Parent(s):
9265104
update
Browse files- app.py +77 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import subprocess
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
def clone_repo():
|
| 7 |
+
# 从环境变量中获取 GitHub Token
|
| 8 |
+
github_token = os.getenv('GH_TOKEN')
|
| 9 |
+
|
| 10 |
+
if github_token is None:
|
| 11 |
+
st.error("GitHub token is not set. Please set the GH_TOKEN secret in your Space settings.")
|
| 12 |
+
return False
|
| 13 |
+
|
| 14 |
+
# 使用 GitHub Token 进行身份验证并克隆仓库
|
| 15 |
+
clone_command = f'git clone https://{github_token}@github.com/mamba-ai/translate_agent.git'
|
| 16 |
+
if os.path.exists('translate_agent'):
|
| 17 |
+
# st.warning("Repository already exists.")
|
| 18 |
+
return True
|
| 19 |
+
else:
|
| 20 |
+
# st.info("Cloning repository...")
|
| 21 |
+
result = subprocess.run(clone_command, shell=True, capture_output=True, text=True)
|
| 22 |
+
|
| 23 |
+
if result.returncode == 0:
|
| 24 |
+
# st.success("Repository cloned successfully!")
|
| 25 |
+
repo_dir = 'translate_agent'
|
| 26 |
+
|
| 27 |
+
# 将仓库路径添加到 Python 模块搜索路径中
|
| 28 |
+
sys.path.append(os.path.abspath(repo_dir))
|
| 29 |
+
return True
|
| 30 |
+
else:
|
| 31 |
+
st.error(f"Failed to clone repository: {result.stderr}")
|
| 32 |
+
return False
|
| 33 |
+
|
| 34 |
+
if clone_repo():
|
| 35 |
+
# 克隆成功后导入模块
|
| 36 |
+
import translate_agent.agent as ta
|
| 37 |
+
|
| 38 |
+
# 创建一个语言选项列表
|
| 39 |
+
languages_src = ["中国語", "日本語"]
|
| 40 |
+
languages_tgt = ["日本語", "中国語"]
|
| 41 |
+
|
| 42 |
+
lang_country_map = {
|
| 43 |
+
"中国語": "China",
|
| 44 |
+
"日本語": "Japan",
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# 设置页面标题
|
| 48 |
+
st.title("MambaTranslate デモ")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# 用户选择源语言和目标语言
|
| 52 |
+
source_lang = st.selectbox("ソース言語を選択", list(languages_src))
|
| 53 |
+
target_lang = st.selectbox("ターゲット言語を選択", list(languages_tgt))
|
| 54 |
+
country = lang_country_map[target_lang]
|
| 55 |
+
print(f"Source language: {source_lang}; Target language: {target_lang}; Country: {country}")
|
| 56 |
+
|
| 57 |
+
# 输入要翻译的文本
|
| 58 |
+
source_text = st.text_area("翻訳するテキストを入力", height=200)
|
| 59 |
+
print(f"Source text:\n\n{source_text}\n------------\n")
|
| 60 |
+
|
| 61 |
+
# 按钮触发翻译
|
| 62 |
+
if st.button("Translate"):
|
| 63 |
+
if source_text:
|
| 64 |
+
with st.spinner("Translating..."):
|
| 65 |
+
translation = ta.translate(
|
| 66 |
+
source_lang=source_lang,
|
| 67 |
+
target_lang=target_lang,
|
| 68 |
+
source_text=source_text,
|
| 69 |
+
country=country,
|
| 70 |
+
)
|
| 71 |
+
st.success("翻訳完了!")
|
| 72 |
+
st.text_area("翻訳されたテキスト", translation, height=200)
|
| 73 |
+
else:
|
| 74 |
+
st.warning("翻訳するテキストを入力してください。")
|
| 75 |
+
else:
|
| 76 |
+
# st.error("Repository could not be cloned. Please check the settings and try again.")
|
| 77 |
+
pass
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai==1.35.13
|
| 2 |
+
tiktoken==0.6.0
|
| 3 |
+
icecream==2.1.3
|
| 4 |
+
langchain-text-splitters
|