Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# transformers パイプラインのインポート
|
| 5 |
+
fugu_translator_enja = pipeline('translation', model='staka/fugumt-en-ja')
|
| 6 |
+
fugu_translator_jaen = pipeline('translation', model='staka/fugumt-ja-en')
|
| 7 |
+
|
| 8 |
+
# デフォルトのモデル
|
| 9 |
+
current_model = 'enja'
|
| 10 |
+
|
| 11 |
+
# 関数: 翻訳を行う
|
| 12 |
+
def translate(model, text):
|
| 13 |
+
global current_model
|
| 14 |
+
|
| 15 |
+
# モデルが変更された場合、変更を反映
|
| 16 |
+
if model != current_model:
|
| 17 |
+
current_model = model
|
| 18 |
+
|
| 19 |
+
if current_model == 'enja':
|
| 20 |
+
return fugu_translator_enja(text)[0]['translation_text']
|
| 21 |
+
else:
|
| 22 |
+
return fugu_translator_jaen(text)[0]['translation_text']
|
| 23 |
+
|
| 24 |
+
# Streamlit アプリケーション
|
| 25 |
+
st.title("Fugu Translator")
|
| 26 |
+
|
| 27 |
+
# デフォルトの入力値
|
| 28 |
+
default_model = 'enja'
|
| 29 |
+
default_text = ''
|
| 30 |
+
|
| 31 |
+
# ユーザー入力の取得
|
| 32 |
+
model = st.selectbox("モデル", ['enja', 'jaen'], index=0, key='model')
|
| 33 |
+
text = st.text_area("入力テキスト", default_text)
|
| 34 |
+
|
| 35 |
+
# 翻訳ボタンが押されたときの処理
|
| 36 |
+
if st.button("翻訳する"):
|
| 37 |
+
result = translate(model, text)
|
| 38 |
+
st.write(f"翻訳結果: {result}")
|