記事作成年月日等:2023年(令和5年)6月11日(日)
要 旨
今回はstreamlitを使用した翻訳アプリ(DeepL)をデプロイする方法について記述します。
実行環境等
VPS:KAGOYAクラウド KVM CPU 1コア、メモリ1GB、ストレージ25GB
料金:20円/1日、550円/月(2023年(令和5年)6月11日(日)時点)
OS:Ubuntu22.04LTS
操作端末:iMac
実行要領
KAGOYAクラウドは、sshキーを使用してログインするのでrootで実施した。
サーバーにログインして、アップデートする。
1 |
apt update |
translationフォルダの作成
1 |
mkdir translation |
作成したtranslationフォルダへ移動する。
1 |
cd translation |
仮想環境virtualenvをインストールする。
1 |
apt -y install python3.10-venv |
1 |
python3 -m venv .venv |
インストールしたvirtualenvをアクティベートする。
1 |
source .venv/bin/activate |
ファイルを、sftpを使用して以下のように配置する。
translation
--- app.py
--- requirements.txt
--- start.sh
各ファイルの内容は、以下のとおり
app.py
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 79 80 81 |
import streamlit as st import requests import hashlib st.set_page_config( page_title="翻訳アプリ", page_icon="https://sample.com/favicon/favicon.ico", layout="wide", initial_sidebar_state="expanded", ) # パスワードのハッシュ値を計算する def hash_password(password): return hashlib.sha256(password.encode()).hexdigest() # 設定したパスワード password_hash = hash_password("password") # ログインフォームを作成 def login(): entered_password = st.text_input("Password", type="password") if hash_password(entered_password) == password_hash: st.success("Access granted.") return True else: st.error("Access denied.") return False # ログインが成功した場合にのみ、アプリケーションを実行 if login(): st.write("Welcome to my app!") st.title("Translate using DeepL") st.write("using DeepL(https://www.deepl.com)") #翻訳元の言語を指定する source_language = st.text_input("Enter the language code of the source language e.g. English→en:") #翻訳先の言語を指定する target_language = st.text_input("Enter the language code of the target language e.g. Japanese→ja:") #翻訳元の文章を入力する text_input = st.text_area("Enter text to translate") #リセットボタンの設置 if st.button("Reset"): text_input ="" def translate(text_input, source_language, target_language): #Translates text from one language to another using the DeepL API. #Args: #text (str): The text to translate. #source_language (str): The language of the text to translate. #target_language (str): The language to translate the text to. #Returns: #str: The translated text. # Set the API key. api_key = "YOUR_api_key" # Set the API endpoint. api_endpoint = "https://api-free.deepl.com/v2/translate" # Set the API parameters. api_params = { "auth_key": api_key, "text": text_input, "source_lang": source_language, "target_lang": target_language } # Make the API request. response = requests.get(api_endpoint, params=api_params) # Parse the response. response_data = response.json() # Return the translated text. return response_data["translations"][0]["text"] if st.button("Translate"): result = translate(text_input, source_language, target_language) st.success(result) |
requirements.txt
1 |
streamlit==1.17.0 |
start.sh
1 |
streamlit run app.py &>/dev/null& |
requirements.txtによりpip installを実施する。
1 |
pip install -r requirements.txt |
start.shファイルに実行権限を付与する。
1 |
chmod +x start.sh |
アプリを起動させる。
1 |
./start.sh |
http://[IPアドレス]:8501で表示を確認する。
SSLの設定
apache2のインストール
1 |
apt -y install apache2 |
以下に種々の設定ファイルが作成されるが、現時点では修正の必要はない。後ほど修正・追記する。
/etc/apache2/
snapのインストール
Ubuntu22.04LTSにはプリインストールされているので不要。
snapのアップデート
1 |
snap refresh |
既にインストールされているcertbot-autoの削除。
まずdnfをインストールする。
1 |
apt -y install dnf |
削除する。
1 |
dnf remove certbot |
Certbotのインストール
1 |
snap install --classic certbot |
Certbotコマンドの準備。
1 |
ln -s /snap/bin/certbot /usr/bin/certbot |
apache2を起動する。
1 |
systemctl start apache2 |
apache2の自動起動設定を実施する。
1 |
systemctl enable apache2 |
apache2の状態確認
1 |
systemctl status apache2 |
apache2の自動起動状態の確認
1 |
systemctl is-enabled apache2 |
ブラウザでhttp://[IPアドレス]でapache2のデフォルトページが表示されるのを確認する。
python3-certbot-apacheのインストール
1 |
apt -y install python3-certbot-apache |
1 |
certbot --apache |
これで指示に従って入力していく。
プロキシを有効にする。
1 |
a2enmod proxy |
1 |
a2enmod proxy_http |
1 |
a2enmod proxy_balancer |
apache2を再起動する。
1 |
systemctl restart apache2 |
システムファイルの編集
1 |
vi /etc/apache2/sites-available/000-default-le-ssl.conf |
以下を追記する
1 2 3 4 5 6 7 8 9 10 11 12 |
<Virtual Host *:443> #追記する ProxyPreserveHost On ProxyPass / http://localhost:8501/ ProxyPassReverse / http://localhost:8501/ RewriteEngine On RewriteCond %{HTTP:Upgrade} websocket [NC] RewriteCond %{HTTP:Connection} Upgrade [NC] RewriteRule /(.*) ws://localhost:8501/$1 [P,L] </Virtual Host> |
streamlitを一旦停止させる。
1 |
ps aux | grep streamlit |
プロセス番号を確認して、停止させる。
1 |
kill [プロセス番号] |
streamlitを起動させる。
1 |
./start.sh |
apache2を再起動する。
1 |
systemctl restart apache2 |
ブラウザでアクセスする。
https://[ドメイン名]
正常に表示されれば完成。
結 言
今回はstreamlitを使用した翻訳アプリ(DeepL)のデプロイ方法について記述しました。
最後までお読みくださり、ありがとうございました。