开始使用免费开始使用

处理文本请求数据

内容审核系统的另一项需求是考虑用户评论的情感倾向。系统需要识别特定的可疑短语,帮助审核员复查可能不当的内容。

您将创建一个端点,用于分析来自用户的文本,并抽取标准化的审核标记。

本练习是课程的一部分

使用 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
    }
编辑并运行代码