2019年5月28日 星期二

install chinese and japanese fonts for matplotlib and seaborn plots

import matplotlib
from matplotlib.font_manager import FontProperties

### 下載日中字型檔
### install japanese font
!apt-get -y install fonts-ipafont-gothic
font_jp = FontProperties(fname=r'/usr/share/fonts/opentype/ipafont-gothic/ipagp.ttf',size=20)
print(font_jp.get_family())
print(font_jp.get_name())

### install chinese font
!apt-get -y install fonts-moe-standard-kai 
font_tw = FontProperties(fname=r'/usr/share/fonts/truetype/moe/MoeStandardKai.ttf',size=20)
print(font_tw.get_family())
print(font_tw.get_name())

### install chinese,japanese,korean font
#!apt-get install fonts-noto-cjk  ## .ttc
#font_cjk = FontProperties(fname=r'/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc',size=20)
#print(font_cjk.get_family())
#print(font_cjk.get_name())

!apt-get install ttf-unifont
font_uni = FontProperties(fname=r'/usr/share/fonts/truetype/unifont/unifont.ttf',size=20)
print(font_uni.get_family())
print(font_uni.get_name())

### 設定畫圖啟用 sans-serif 系列字型
!grep font.family /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/matplotlibrc
!sed -i "s/#font.family/font.family/" /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/matplotlibrc
!grep font.family /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/matplotlibrc

### 設定屬於 sans-serif 系列字型包含日中字型
!grep font.sans-serif /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/matplotlibrc
!sed -i "s/#font.sans-serif.*DejaVu Sans/font.sans-serif     : IPAPGothic, TW-MOE-Std-Kai, Unifont, DejaVu Sans/" /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/matplotlibrc
#!sed -i "s/font.sans-serif.*DejaVu Sans/font.sans-serif     : IPAPGothic, TW-MOE-Std-Kai, Unifont, DejaVu Sans/" /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/matplotlibrc
!grep font.sans-serif /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/matplotlibrc

### 連結日中字型檔到畫圖字型目錄
!ln -s /usr/share/fonts/opentype/ipafont-gothic/ipagp.ttf /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf/
!ln -s /usr/share/fonts/truetype/moe/MoeStandardKai.ttf /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf/
#!ln -s /usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf/
!ln -s /usr/share/fonts/truetype/unifont/unifont.ttf /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf/
!ls /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf/[Miu]*

### 重建畫圖字型快取,納入日中字型檔
matplotlib.font_manager._rebuild()
flist = matplotlib.font_manager.get_fontconfig_fonts()
names = [matplotlib.font_manager.FontProperties(fname=fname).get_name() for fname in flist]
print(names)

### 確認sans-serif系列清單有日中字型
print(matplotlib.rcParams['font.sans-serif'])  ## 確認sans-serif系列清單是否有日中字型
if 'IPAPGothic' not in matplotlib.rcParams['font.sans-serif']:
  matplotlib.rcParams['font.sans-serif'] = ['IPAPGothic', 'TW-MOE-Std-Kai', 'Unifont'] + matplotlib.rcParams['font.sans-serif']
print(matplotlib.rcParams['font.sans-serif'])

### 測試畫圖字型顯示
#!!!!! 若X軸標示字型有誤,表示引擎快取仍未更新,請【重新啟動並運行所有單元格】
import matplotlib.pyplot as plt

testString = u"喜欢 海灘 散步 걷기 好き"   ## 簡中,繁中,日文,韓文,日文
plt.title(testString, fontproperties=font_uni)
plt.xlabel(testString)   # 利用sans-serif第一個字型顯示
plt.ylabel(testString, fontproperties=font_tw)
plt.show()
import seaborn as sns
emotion_counter = [('愉快', 200), ('高興', 180), ('開心', 160), ('歡喜', 140), ('生氣', 130), ('憤怒', 120), ('悲傷', 110), ('難過', 100), ('哀愁', 90), ('傷感', 80)]
sns.set_color_codes("pastel")
sns.barplot(x=[k for k, _ in emotion_counter], y=[v for _, v in emotion_counter])
參考: 解決Python 3 Matplotlib與Seaborn視覺化套件中文顯示問題 link

flask-based web interface deployment for pytorch chatbot

### folder structure and flask setup
> ls 
data/  pytorch_chatbot/  save/  templates/  web.py

> ls templates/
template.html

> conda install Flask

> python web.py
 * Serving Flask app "web" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)


<html>
<title>template.html</title>
<body>
<pre>
Test page for pytorch chatbot on seq2seq dataset
<form action='translate' method='post'>
model: <input type='text' name='model' value='{{param["model"]}}' />
epoch: <input type='text' name='epoch' value='{{param["epoch"]}}' />
topn: <input type='text' name='topn' value='{{param["topn"]}}' />
query: <input type='text' name='query' value='{{param["query"]}}'/>
<input type='submit' value='translate' />
</form>
{{param['result']}}
</pre>
</body>
</html>



##########################
# web.py
#    > python web.py
#########################
from flask import Flask, request, render_template

import torch
import random
import pytorch_chatbot.main as pcm
import pytorch_chatbot.evaluate as pce
from pytorch_chatbot.train import indexesFromSentence
from pytorch_chatbot.load import loadPrepareData
from pytorch_chatbot.model import nn, EncoderRNN, LuongAttnDecoderRNN

import subprocess
import json

def predictLoad(corpus, modelFile, n_layers=1, hidden_size=512):
  print('corpus={}\nmodelFile={}'.format(corpus,modelFile))

  torch.set_grad_enabled(False)
  voc, pairs = loadPrepareData(corpus)
  embedding = nn.Embedding(voc.n_words, hidden_size)
  encoder = EncoderRNN(voc.n_words, hidden_size, embedding, n_layers)
  attn_model = 'dot'
  decoder = LuongAttnDecoderRNN(attn_model, embedding, hidden_size, voc.n_words, n_layers)

  checkpoint = torch.load(modelFile)
  encoder.load_state_dict(checkpoint['en'])
  decoder.load_state_dict(checkpoint['de'])

  # train mode set to false, effect only on dropout, batchNorm
  encoder.train(False)
  decoder.train(False)

  #try:
  encoder = encoder.to(device)
  decoder = decoder.to(device)
  #except:
  #  print('cannot get encoder/decoder')
  
  return encoder, decoder, voc

def predict(encoder, decoder, voc, question, top):
  result_list = []

  if(top==1):
    beam_size = 1
    output_words, _ = pce.evaluate(encoder, decoder, voc, question, beam_size)
    answer = ' '.join(output_words)
    answer = answer.replace('<EOS>','')
    result_list.append(answer)
    #print(output_words)
  else:
    beam_size = top
    output_words_list = pce.evaluate(encoder, decoder, voc, question, beam_size)
    count = 0;
    for output_words, score in output_words_list:
      count = count + 1
      if(count <= top):
        output_sentence = ' '.join(output_words)
        output_sentence = output_sentence.replace('<EOS>','')
        result_list.append(output_sentence)
        #print(" {:.3f} < {}".format(score, output_sentence))
  
  return result_list

def filter(voc, question):
  words = question.split()
  result = []
  for w in words:
    if(w in voc.word2index):
      result.append(w) 
  return ' '.join(result)

# -------------------------------
def sentence_test(voc,en,de,top,sentence):
    source = sentence.rstrip()
    seg_source = source
    fil_source = filter(voc, seg_source)
    target = predict(en, de, voc, fil_source, top)
    result = "\nsource: '%s'\nfilter: '%s'\n" % (seg_source,fil_source)
    
    for answer in target:
      result = result + "\t'%s'\n" % (answer)
    
    result = result + '\n'
    return result

def sentence_test_model(seg_corpus_name, iteration, top, sentence):
  n_layers = 1
  hidden_size = 512

  modelFile = home_path + 'save/model/' + seg_corpus_name + '/1-1_512/' + str(iteration) + '_backup_bidir_model.tar'

  en, de, voc = predictLoad(seg_corpus_name, modelFile, n_layers, hidden_size)

  return sentence_test(voc,en,de,top,sentence)

def file_test(voc,en,de,top,test_file_name):
  with open(test_file_name,"r") as f:
    jp_data = f.readlines()

  for i,source in enumerate(jp_data):
    source = source.rstrip()
    seg_source = source
    fil_source = filter(voc, seg_source)
    target = predict(en, de, voc, fil_source, top)
    print("%d:\nsource: '%s'\nfilter: '%s'" % (i+1,seg_source,fil_source))
    
    for answer in target:
      print("\t'%s'" % (answer))

def file_test_model(seg_corpus_name, iteration, top, test_file_name):
  n_layers = 1
  hidden_size = 512

  modelFile = home_path + 'save/model/' + seg_corpus_name + '/1-1_512/' + str(iteration) + '_backup_bidir_model.tar'

  en, de, voc = predictLoad(seg_corpus_name, modelFile, n_layers, hidden_size)

  file_test(voc,en,de,top,test_file_name)
  
def print_voc(voc):
  print('tw+jp voc size=%d' % (len(voc.word2index)))
  print(voc.index2word)

def list_models(seg_corpus_name=''):
  if seg_corpus_name=='':
    modelPath = home_path + 'save/model/'
  else:
    modelPath = home_path + 'save/model/' + seg_corpus_name + '/1-1_512'

  out_bytes = subprocess.check_output(['ls','-l',modelPath],
                                    stderr=subprocess.STDOUT)
  out_text = out_bytes.decode('utf-8')
  return out_text

def load_source(seg_corpus_name):
  path = home_path + 'data/' + seg_corpus_name + '.txt'
  
  with open(path) as inp:
    data = inp.readlines()

  print(len(data), len(data[0::2]), len(data[1::2]))

  data = { 'source': data[0::2], 'target': data[1::2] }
  return data

# --------------------------
app = Flask(__name__)

param0 = { 'model': 'translation2019_train_83k',
          'epoch':  6000,
          'topn' : 10,
          'query' : 'what time is it?',
          'result' : 'result area'
        }

@app.route('/')
def forms():
  return render_template('translate.html', param=param0)

@app.route('/translate/<model>/<int:epoch>/<int:topn>', methods=['GET', 'POST'])
def translate_long(model,epoch,topn):
  if request.method == 'POST':
    query = request.values['query']
  elif request.method == 'GET':
    query = request.args.get('query')

  return translate(model,epoch,topn,query)


@app.route('/translate', methods=['GET', 'POST'])
def translate_short():
  if request.method == 'POST':
    query = request.values['query']
    model = request.values['model']
    epoch = request.values['epoch']
    topn = request.values['topn']
  elif request.method == 'GET':
    query = request.args.get('query')
    model = request.args.get('model')
    epoch = request.args.get('epoch')
    topn = request.args.get('topn')

  return translate(model,epoch,topn,query)

def translate(model,epoch,topn,query):
  epoch = int(epoch)
  topn = int(topn)

  try:
    target = sentence_test_model(model,epoch,topn,query)
  except:
    target = 'internal error, retry a again'

  result = 'query="{}"\nresult="{}"\n'.format(query,target)
  
  param2 = { 'model': model,
          'epoch':  epoch,
          'topn' : topn,
          'query' : query,
          'result' : result
        }
  return render_template('translate.html', param=param2)

@app.route('/list/<model>')
def list_model(model):
  mlist = list_models(model)
  return '<pre>{}</pre>'.format(mlist)

@app.route('/list/')
def list():
  mlist = list_models()
  return '<pre>{}</pre>'.format(mlist)

#######################################

USE_CUDA = torch.cuda.is_available()
device = torch.device("cuda" if USE_CUDA else "cpu")
home_path = './'

if __name__ == '__main__':
  app.run(host='0.0.0.0',port=8080)


註: 本程式使用 GitHub JavaScript code prettifier 工具標示顏色。其方法如下:
   1.參考 [Blogger] 如何在 Blogger 顯示程式碼 - Google Code Prettify
     於【Blogger 版面配置 HTML/JavaScript小工具】安裝如下套件
       <script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
   2.文章編輯再以HTML模式為程式包上如下標籤。
       <code class="prettyprint lang-html linenums"> ... </code>
       <code class="prettyprint lang-python linenums"> ... </code>

2019年5月17日 星期五

memo for quota setup on Ubuntu/Linux

Ubuntu 設定帳號的硬碟配額方法

############# 一次性安裝及設定指令 =======================
# 安裝 quota 配額套件

$ sudo apt install quota

# 修改檔案系統表 fstab,針對套用配額的掛載點,加上usrquota, grpquota

$ sudo vi /etc/fstab
UUID=xxxx /home ext4 defaults,usrquota,grpquota 0 2

$ sudo mount -o remount /home

$ grep /home /etc/mtab
/dev/sdb1 /home ext4 rw,relatime,quota,usrquota,grpquota,data-ordered 0 0


# 產生權限設定檔
$ sudo quotacheck -cug /home
$ sudo quotacheck -ugvmca
$ ls /home

# 啟用配額管制
$ sudo quotaon -a
$ sudo quotaon -ap

# 修改配額超用免責期,預設為資料區塊數及索引區塊數皆享有7日超過免責期
$ sudo edquota -t
Grace period before enforcing soft limits for users:
Time units may be: days, hours, minutes, or seconds
  Filesystem    Block grace period   Inode grace period
  /dev/sdxy       7days                7days

############ 經常性檢視及設定用戶配額指令 ###################
# 修改user1用戶的資料/索引區塊的軟/硬配額
#   資料區塊用於存放檔案內容,blocks顯示目前資料區塊用量
#   索引區塊用於存放目錄內容,inodes顯示目前索引區塊用量
#   軟(soft)配額可以超過,但超過將進入寬限期
#   硬(hard)配額不可超過
#   寬限期(grace)預設7天,超過後硬碟無法新增檔案,直到刪除用量,降到軟配額以下
$ sudo edquota -u user1
Disk quotas for user user1 (uid xxx):
  Filesystem   blocks  soft hard inodes soft hard 
  /dev/sdxy    yyyy      0     0  zzzz    0     0

# 將user1用戶的配額設定套用到user2,user3
$ sudo edquota -p user1 user2 user3

# 列出user1,user2用戶的配額設定
$ sudo quota user1 user2 ...

# 列出所有用戶的配額設定
$ sudo repquota -avus
*** report for user quotas on device /dev/sdxy
Block grace time: 7days: Inode grace time: 7days

                 Space limits          File limits
User       used  soft  hard grace   used soft hard grace
--------------------------------------------------------
root  --  1088k   0k   0k            188  0  0 
.....

2019年5月8日 星期三

environment setup for running pytorch chatbot

PyTorch框架有很多深度學習範例,例如Chatbot聊天機器人展示。
以下記錄如何在Ubuntu環境,已安裝anaconda套件管理工具下,
建置適合PyTorch Chatbot執行的環境。

=== 設定顯示 conda環境,只要設定一次即可,以後登入會自動顯示
user@gpu:~/jupyter$ /usr/local/anaconda3/bin/conda init bash
user@gpu:~/jupyter$ source ~/.bashrc

=== 以後登入會自動顯示如下提示符號
(base) user@gpu:~/jupyter$
    conda create --name chatbot python=3.6 # 建立chatbot環境,執行一次即可
    conda activate chatbot # 進入chatbot環境

(chatbot) user@gpu:~/jupyter$
    -- 以下只要設定一次即可
    conda list # 列出目前環境安裝套件
    --
    conda install jupyter pytorch tensorflow-gpu torchvision tqdm [-c pytorch] # 安裝套件
    --
    jupyter notebook --generate-config # 產生jupyter notebook設定檔
    vi ~/.jupyter/jupyter_notebook_config.py # 修改設定檔
     c.NotebookApp.port = xxxx   # 選擇埠號xxxx
     c.NotebookApp.ip = '*'      # 允許外部連入
    jupyter notebook password       # 設定密碼

    -- 以上只要設定一次即可,以後只要進入chatbot環境,如下啟動jupyter notebook即可
    jupyter notebook                # 啟動jupyter notebook
    [Ctrl-C]
    --
    /usr/bin/lsof -i [:xxxx]  # 查看那個行程佔用那個埠號,或特定xxxx埠號
    /usr/bin/nvidia-smi   # 查看那個行程佔用GPU及其記憶體
    /usr/bin/top   # 查看那個行程佔用CPU及記憶體
    /usr/bin/kill -9 yyy  # 砍掉pid=yyy的行程
    --
    conda deactivate  # 離開chatbot,回到base環境

(base) user@gpu:~/jupyter$

註1: 使用上的注意事項
1. /usr/bin/xfce4-terminal 為命令列終端機,位於選單【應用程式/系統/Xfce終端機】
2. /snap/bin/pycharm-community 為PyCharm IDE,位於選單【應用程式/開發/PyCharm Community Edition】
3. /home/user/.conda/envs/chatbot/pkgs/ 為實際每個人利用conda安裝個人套件後的套件位置
4. /home/user/.conda/envs/chatbot/bin/ 為實際每個人利用conda安裝個人套件後的執行檔位置,例如jupyter指令
5. C:\Users\user\AppData\Local\conda\conda\envs\chatbot 為Windows上chatbot環境位置


註2: 假設 Ubuntu 18.04.1 LTS Kernel 4.15.0-47-generic #50-Ubuntu SMP 已裝好如下套件:
1. /usr/local/cuda <- cuda_10.0.130_410.48_linux.run
2. /usr/lib/x86_64-linux-gnu/libcudnn.so.7 <- libcudnn7_7.5.0.56-1+cuda10.0_amd64.deb
3. /usr/local/anaconda3/bin/conda <- Anaconda3-2019.03-Linux-x86_64.sh
4. /snap/bin/pycharm-community <- pycharm-community-2019.1.1.tar.gz