開始使用免費開始

處理數值型請求資料

你正在打造一個內容審查系統。系統需要根據數值特徵(lengthuser_reputationreport_count)為每則使用者留言計算信任分數。你將建立一個端點,將這些特徵處理為可供審查模型使用的格式。

注意:機器學習模型與 CommentMetrics 的 Pydantic 模型(欄位包含 length(int)、user_reputation(int)、report_count(int))都已為你建立並載入。

本練習屬於課程

使用 FastAPI 將 AI 佈署到生產環境

檢視課程

練習說明

  • main.py 中,將接收到的留言 metrics 資料轉換為 NumPy 的 2D 陣列,並在傳入模型前先擷取 lengthuser_reputationreport_count

  • features 陣列傳入已預先載入的 model 以進行預測。

  • 使用指令 python3 main.py 執行 main.py 以啟動伺服器。

  • 在終端機右上角開啟另一個終端機分頁。

    Terminal with arrow pointing to the "new terminal" button on top right.

  • 使用以下 curl 指令測試 predict_trust 端點:

curl -X POST "http://localhost:8080/predict_trust" \
     -H "Content-Type: application/json" \
     -d '{
           "length": 150,
           "user_reputation": 100,
           "report_count": 0
         }'

動手互動練習

將理論付諸實踐,立即體驗我們的互動練習

開始練習