/
PyThaiNLP

PyThaiNLP

บทความนี้จะแนะนำการใช้งาน PyThaiNLP บนระบบ TARA HPC เบื้องต้น ร่วมกับการทำงานกับ jupyter notebook ซึ่งต้อง ssh tunneling ไปยัง TARA HPC โดยจะนำเสนอเป็นลำดับขั้นตอนต่อไป

โดยผู้ใช้งาน ThaiSC ที่มี user account สามารถสำเนาไฟล์ 'intro_pythainlp_232.ipynb’ เพื่อไปทดลองตามตัวอย่างนี้ได้จาก /tarafs/data/project/common/AI/examples/

 

ติดตั้ง PyThaiNLP บน TARA แบบ virtualenv

https://github.com/PyThaiNLP/pythainlp

เนื่องจาก PyThaiNLP เวอร์ชั่นที่ stable ปัจจุบันทำงานร่วมกับ Python 3.7 เราจึงเลือก load module Python เวอร์ชัน 3.7 บน TARA แล้ว activate venv ขึ้นมาจัดการ environment นี้ ดังแสดงในตัวอย่าง

[username@tara-frontend-1 ~]$ mkdir nlp [username@tara-frontend-1 ~]$ cd nlp/ [username@tara-frontend-1 nlp]$ ml av Python ------------------------------------- /tarafs/utils/modules/modules/all -------------------------------------- LAMMPS/3Mar2020-foss-2019b-Python-3.7.4-kokkos SciPy-bundle/2019.10-foss-2019b-Python-3.7.4 PLUMED/2.5.3-foss-2019b-Python-3.7.4 Trinity/2.9.1-foss-2019b-Python-3.7.4 Python/2.7.16-GCCcore-8.3.0 Unicycler/0.4.8-foss-2019b-Python-3.7.4 Python/2.7.18-GCCcore-10.2.0 archspec/0.1.0-GCCcore-8.3.0-Python-3.7.4 Python/2.7.18-GCCcore-10.3.0-bare h5py/2.10.0-foss-2019b-Python-3.7.4 Python/3.7.4-GCCcore-8.3.0 matplotlib/3.1.1-foss-2019b-Python-3.7.4 Python/3.8.6-GCCcore-10.2.0 molmod/1.4.5-foss-2019b-Python-3.7.4 Python/3.9.5-GCCcore-10.3.0-bare pkgconfig/1.5.1-GCCcore-8.3.0-Python-3.7.4 Python/3.9.5-GCCcore-10.3.0 (L,D) yaff/1.6.0-foss-2019b-Python-3.7.4 Where: L: Module is loaded D: Default Module Use "module spider" to find all possible modules. Use "module keyword key1 key2 ..." to search for all possible modules matching any of the "keys". [username@tara-frontend-1 nlp]$ ml Python/3.7.4-GCCcore-8.3.0 [username@tara-frontend-1 nlp]$ ml Currently Loaded Modules: 1) OpenSSL/1.1 8) SQLite/3.29.0-GCCcore-8.3.0 2) binutils/2.32-GCCcore-8.3.0 9) XZ/5.2.4-GCCcore-8.3.0 3) bzip2/1.0.8-GCCcore-8.3.0 10) GMP/6.1.2-GCCcore-8.3.0 4) ncurses/6.1-GCCcore-8.3.0 (H) 11) GCCcore/8.3.0 5) libreadline/8.0-GCCcore-8.3.0 (H) 12) libffi/3.2.1-GCCcore-8.3.0 (H) 6) zlib/1.2.11-GCCcore-8.3.0 (H) 13) Python/3.7.4-GCCcore-8.3.0 7) Tcl/8.6.9-GCCcore-8.3.0 Where: H: Hidden Module [username@tara-frontend-1 nlp]$ virtualenv venv Using base prefix '/tarafs/utils/modules/software/Python/3.7.4-GCCcore-8.3.0' ... done. [username@tara-frontend-1 nlp]$ source venv/bin/activate (venv) [username@tara-frontend-1 nlp]$

โดยสรุปจากตัวอย่างข้างต้น เราได้ดำเนินการ 4 ขั้นตอนคือ

  1. ml av Python เพื่อหาเวอร์ชั่นที่เหมาะสมของ Python ในระบบ TARA

  2. ml Python/3.7.4-GCCcore-8.3.0 เพื่อโหลดเวอร์ชั่นที่ต้องการใช้งานขึ้นมา

  3. virtualenv venv เรียกใช้ virtualenv และสร้าง venv ขึ้นมา

  4. source venv/bin/activate source activate environment ขี้นมาใช้งาน

ซึ่งเราจะทำการติดตั้ง PyThaiNLP ใน venv ที่เราเตรียมไว้ โดยเลือกติดตั้งแบบ extra – attacut, ml, wordnet, benchmarks และ thai2fit เพิ่มเติมเพราะเราพอทราบว่าต้องการใช้งาน

5. ใช้คำสั่ง pip install --upgrade pythainlp[attacut,ml,wordnet,benchmarks,thai2fit] ดังแสดงในตัวอย่างด้านล่าง

ที่เราไม่ติดตั้ง pythainlp[full] เพราะมันพังที่ ICU

(venv) [username@tara-frontend-1 nlp]$ pip install --upgrade pythainlp[attacut,ml,wordnet,benchmarks,thai2fit] Collecting pythainlp[attacut,benchmarks,ml,thai2fit,wordnet] Using cached pythainlp-2.3.2-py3-none-any.whl (11.0 MB) ... Successfully installed attacut-1.0.6 docopt-0.6.2 emoji-1.5.0 fire-0.4.0 gensim-4.1.2 nptyping-1.4.4 pythainlp-2.3.2 ssg-0.0.8 typish-1.9.3 (venv) [username@tara-frontend-1 nlp]$

เสร็จสรรพเราก็ได้ pythainlp เวอร์ชั่น 2.3.2 ใน environment nlp ของเรา โดยที่ pythainlp จะมีการเชื่อมต่อกับ data catalog ที่หลากหลายมาให้ ดาวน์โหลดกันด้วย

(venv) [username@tara-frontend-1 nlp]$ thainlp data catalog Dataset/corpus available for download: - g2p 0.1 - lst20-cls 0.2 - ltw2v 0.1 - oscar_icu 1.0 - pos_lst20_perceptron 0.2.3 - pos_lst20_unigram 0.2.3 - scb_1m_en-th_moses 1.0 - scb_1m_en-th_spm 1.0 - scb_1m_th-en_newmm 1.0 - scb_1m_th-en_spm 1.0 - scb_en_th 1.0 - scb_th_en 1.0 - test 0.1 - thai-g2p 0.1 - thai2fit_wv 0.1 - thai2rom-dataset 0.1 - thai2rom-pytorch 0.1 - thai2rom-pytorch-attn 0.1 - thai_w2p 0.1 - thainer 1.5 - thainer-1.4 1.4 - tnc_bigram_word_freqs 2017 - tnc_trigram_word_freqs 2017 - wiki_itos_lstm 0.32 - wiki_lm_lstm 0.32 Use subcommand 'get' to download a dataset. Example: thainlp data get crfcut (venv) [username@tara-frontend-1 nlp]$

ทดลองใช้งาน PyThaiNLP ผ่าน Jupyter Notebook

ซึ่งในส่วนของการใช้งาน PyThaiNLP ในตัวอย่างนี้ขอทำตามบทความจาก https://towardsdatascience.com/beginners-guide-to-pythainlp-4df4d58c1fbe

ดังนั้นก่อนอื่นก็อยากทำตามแบบที่ใช้ jupyter notebook ด้วย

1. ทำการติดตั้ง jupyterlab ภายใน environment ที่เตรียมไว้เพิ่มเติม pip install jupyterlab

2. ทำการจองเครื่องใน HPC แบบ sinteract sinteract ซึ่งจะให้เครื่องประเภท compute node มาหนึ่งตัว

3. เมื่อได้เครื่องแล้วก็จะสามารถสตาร์ทโน้ตบุคใน node ทรัพยากรที่ได้รับมา jupyter notebook --no-browser ดังแสดงในตัวอย่างด้านล่าง ซึ่งเรากำลังจะเข้าสู่โหมดการใช้งานรวม 4 หน้าต่างเป็นอย่างน้อย กล่าวคือ

  • Terminal 1 - jupyter notebook --no-browser

  • Terminal 2 - ssh tunneling from local to HPC

  • Terminal 3 - ระหว่างการใช้งาน jupyter notebook เราอาจจะอยากติดตั้ง packages เพิ่ม

  • และหน้าต่างที่สี่คือ browser ของเราที่จะทำการเชื่อมต่อผ่านทางพอร์ตที่เราทำ tunneling เอาไว้ไปหา notebook ที่เราเปิดบน TARA HPC กัน

Terminal 1 - jupyter notebook --no-browser

เราจะเห็นได้ว่า jupyter ใช้พอร์ต 8888 และให้เราเชื่อมต่อกับโน้ตบุคผ่านทาง URLs: http://localhost:8888/?token=58bfd7de821a8722c4e07c0eafad519c868f375e61285982

เปิดหน้า terminal นี้ค้างไว้

Terminal 2 - ssh tunneling from local to HPC

โดยเราต้องใส่ password เพื่อเชื่อมต่อกับ TARA ตามปกติแล้วหน้าจอจะค้างไว้

Browser 1 - เพื่อไปตามพอร์ตที่เชื่อมต่อ tunneling ไว้สำหรับเปิด jupyter notebook

http://localhost:8888/?token=58bfd7de821a8722c4e07c0eafad519c868f375e61285982

โดยใช้ url ที่ได้จากตอน start application ใน Terminal 1

 

Terminal 3 - ติดตั้ง packages หรือ corpus เพิ่ม

เปิด terminal ขึ้นมาอีกหนึ่งหน้าจอ connect ไปที่ Frontend Node ของ TARA แล้วเข้าไปยัง environment ที่เรากำลังใช้งานสำหรับ jupyter notebook ของเรา (venv)

ทั้งนี้ในตัวอย่างที่เลือกใช้นั้นจะต้องมีการดาวน์โหลด corpus เพิ่มเติมทำให้จำเป็นต้องเปิด Terminal ที่สามนี้ขึ้นมาติดตั้งลงใน venv ที่เรากำลังใช้งาน และไม่สามารถติดตั้งผ่าน notebook ได้เนื่องจาก notebook ถูกรันบน compute node ที่ไม่มี Internet

โดยผู้ใช้งาน ThaiSC ที่มี account สามารถสำเนาไฟล์ 'intro_pythainlp_232.ipynb’ เพื่อไปทดลองตามตัวอย่างนี้ได้ที่ /tarafs/data/project/common/AI/examples/intro_pythainlp_232.ipynb


Related articles

 

 

Related content

การใช้งาน Jupyter Notebook กับ Virtualenv
การใช้งาน Jupyter Notebook กับ Virtualenv
More like this
การใช้งาน Jupyter Notebook กับ Singularity
การใช้งาน Jupyter Notebook กับ Singularity
More like this
การติดตั้ง PyTorch package บน TARA
การติดตั้ง PyTorch package บน TARA
More like this
ตัวอย่าง Colab to TARA HPC
ตัวอย่าง Colab to TARA HPC
More like this
AI, Machine learning, and Deep learning
AI, Machine learning, and Deep learning
Read with this
การใช้งาน Python บน TARA
การใช้งาน Python บน TARA
More like this