開始使用免費開始

處理文字型的請求資料

內容審核系統的另一個需求是納入使用者留言的情緒傾向。系統需要辨識特定的問題片語,協助審核人員檢視可能不當的內容。

你將建立一個端點,分析使用者傳入的文字,並萃取標準化的審核標記。

本練習屬於課程

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

檢視課程

練習說明

  • analyze_comment() 函式中,將輸入文字轉成小寫,讓比對不分大小寫。
  • 使用 problem_keywords 清單,從處理後的文字中萃取問題關鍵字並放入 found_issues
  • 回傳含有以下鍵的 JSON 回應:issues(關鍵字清單)、issue_count(整數)、以及 original_text(字串)。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

@app.post("/analyze_comment")
def analyze_comment(text: str):
    problem_keywords = ["spam", "hate", "offensive", "abuse"]
    
    # Convert the input text to lowercase
    text_lower = ____
    # Extract matching flags using list comprehension
    found_issues = [____ for ____ in problem_keywords if keyword in text_lower]
    # Return the dictionary with required keys
    return {
        "____": found_issues,
        "____": len(found_issues),
        "____": text
    }
編輯並執行程式碼