跳到主要內容

發表文章

在既有 Cloud Storage Bucket 上加入 CORS 設定

  前言  當 Javascript 在存取資源的時候, 瀏覽器會禁止非同源的請求, 除非 Server 上有 CORS  設定讓瀏覽器知道哪些請求是可以允許被發送的 如何在 既有 Cloud Storage Bucket 上加入 CORS 設定 (以下使用 Terraform  來做設定) 假設 Cloud Storage 上有個非 Terraform 建立的 Bucket    首先, 建立資源描述 resource "google_storage_bucket" "andytest123" {   name          = "andytest123"   location      = "US" } Step 2. 把既有的資源設定從雲上讀下來 terraform import google_storage_bucket.image-store andy-prj/andytest123 Step 3. 補上 CORS 設定 resource "google_storage_bucket" "andytest123" {   name          = "andytest123"   location      = "US"   cors {     origin          = ["http://localhost:3000",     "https://web-stage.uc.r.appspot.com",     "https://web.uc.r.appspot.com"     ]     method          = ["GET"]     response_header = ["*"]     max_age_sec...

快速建立 NLU 模型

Step 1. 準備環境 conda create --name=rasa-env python=3.8 conda activate rasa-env pip install rasa[full] Step 2. 初始化工作目錄 import rasa from rasa.cli.scaffold import create_initial_project create_initial_project("MY_NLP_PROJECT") import os os.chdir("MY_NLP_PROJECT") Step 3. 開始訓練 rasa train nlu Step 4. 測試 #import rasa from rasa.core.agent import Agent interpreter = Agent.load("./models/nlu-20211213-035921-charitable-score.tar.gz") import asyncio import nest_asyncio nest_asyncio.apply() asyncio.run(interpreter.parse_message("kill"))

Run notebook in Airflow

  The simplest way to achieve this goal is using KubernetesPodOperator to execute papermill command. For example run_notebook = kubernetes_pod_operator.KubernetesPodOperator( task_id=f"run-notebook", name=f"run-notebook", namespace='default', is_delete_operator_pod=True, image_pull_policy="IfNotPresent", startup_timeout_seconds=3600, cmds=['/bin/bash'], arguments=["-c", """ echo y | conda create --name=runenv python=3.8 source /opt/conda/etc/profile.d/conda.sh conda activate runenv conda install ipykernel python -m ipykernel install --user --name=runenv papermill \ "{{dag_run.conf['nbIn']}}" \ "{{dag_run.conf['nbOut']}}" \ ...

Run Notebook from another Notebook

Step 1. Install this package on your environment pip install papermill Step 2. Prepare a kernel that runs your notebook conda create --name=runenv python=3.8 source /opt/conda/etc/profile.d/conda.sh conda activate runenv conda install ipykernel python -m ipykernel install --user --name=runenv Step 3. Run notebook papermill -k runenv \ gs://andy-notebook/in/template.ipynb \ gs://andy-notebook/out/template.ipynb \ -p project_id stage-env \ -p dataset_path gs://andy-data In this example, we use runenv to run notebook gs://andy-notebook/in/template.ipynb and pass parameters "project_id" and "dataset_path" to the notebook. The execution result will be put on GCS (gs://andy-notebook/out/template.ipynb). That's it.

在 Jupyter Notebook 使用 conda 建立的環境

  Step 1. 在  base environment 安裝 nb_conda_kernels (base)$ conda install nb_conda_kernels 如果不行的話就接著做下一步 Step 2. 在新建立的環境下安裝 ipykernel (new-env)$ conda install ipykernel python -m ipykernel install --user --name="My_Project_Name" 其他有用指令 List kernels $ jupyter kernelspec list Remove kernel $ jupyter kernelspec remove [KERNAL_NAME]

[解決方法] mac 作業系統上無法使用 docker

  錯誤訊息 Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? 原因 因為 docker 的設計是走 client-server 的架構,  如果少裝了 server 的部分就會出現以上的錯誤訊息 解決方法 因為 docker daemon 需要使用 linux kernel 上的某些功能, 所以若想要在 mac 的 OS X 上使用 docker 必須額外起一台 linux VM 給 docker daemon 用  Step 1. 安裝 virtual box $ brew install virtualbox --cask   Step 2. 安裝 docker machine $ brew install docker-machine --cask   Step 3. 設定 使用 docker-machine 建立 VM 跑容器 $docker-machine create --driver virtualbox default $docker-machine restart   輸出環境變數 $docker-machine env default 如果執行以上的指令出現錯誤訊息 Error checking TLS connection: ...  可以執行以下指令重新產生憑證 $docker-machine regenerate-certs 最後套用環境變數, 讓 docker 知道要怎麼去跟這台 VM 溝通  $eval $(docker-machine env default)   測試 若做完以上的步驟沒噴錯誤訊息的話, 可以跑個 hello-world 看看 docker daemon 有沒有起來 $docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha...

淺談 Cloud Dataflow & Apache Beam 處理資料流 | Data Pipeline using Apache Beam and Cloud Dataflow

  前言 在這個資訊爆炸的時代, 資料的產生速度飛快, 面對這些海量的資訊, 若以傳統的方法做資料分析或是分析前的資料處理(Extract Load Transform, ETL), 勢必會面臨到硬體資源不足(記憶體不夠)或是資料處理速度太慢的問題 記憶體不夠時, 將虛擬機做垂直擴展 scaling up 就能夠暫時解決, 但若要加快資料處理的速度, 就會需要一個分散式的資料處理引擎 Spark 或是 Cloud Dataflow, 透過動態的水平擴展 Scaling out 的方式來增加資料處理的 worker 去消化大量資訊 Apache Beam 2016 年時 Google 開源出一套分散式資料處理框架, 定義在 Spark, Flink 以及 Cloud Dataflow 等等分散式資料處理引擎的上層, 讓開發者可以輕鬆地設計資料處理的 pipeline 並運行在分散式的環境裡 它有以下的優點 開源 背後社群龐大, 容易找到資源 移植性 同一個資料處理邏輯可以跑在各大分散式資料處理引擎上 (Flink, Spark, Dataflow...) 擴充性 除了使用 built-in 的 I/O Connector 之外, 開發者也可以自由地實作客製化版本的 Connector 模組 Cloud Dataflow Google 完全託管式的(Fully Managed) 分散式資料處理引擎, No-Ops 的服務特色強調不需要額外的維運人員來維護底層的基礎設施 Dataflow 的上層主要是運行 Apache Beam 的程式碼, 並支援在 Runtime 時動態的彈性擴充底層的運算資源, 因此使用 Dataflow 的開發人員可以更專注地開發資料處理的邏輯以及 pipeline 的設計 開始寫 Apache Beam 程式 Step 1. 建立實驗環境 為了簡化說明, 接下來的示範會跑在 Jupyter Notebook 的環境裡 因此首先會需要在 GCP 上用 Dataflow 的 Notebook 功能建立一個可以跑 Jupyter Notebook 的環境 建立完成後選擇 Open Jupyterlab 進入筆記本 接下來 Step 2. & Step 3. 的程式碼可以直接複製貼到筆記本上執行 Step 2. import 必須的套件 ...