[BACK-AI-07] FastAPI Service Development for AI Text Moderation and Profanity Filtering

AI Components (AI Service)

Published

October 14, 2026

NoteTask Brief
  • Repository / Component: ai_profanity_filter.
  • Language: Python 3.10+ (FastAPI, Pydantic, Requests).
  • Task Type: Backend (AI Content Moderation).
  • Status: Ready for Implementation

Task Description

Implement a specialized moderation microservice based on FastAPI. The service intercepts user-input text data (names of created dishes, text audio transcriptions) and checks them for profanity, explicit language, spam, or insults using the lightweight local qwen2.5:3b model.

Step-by-Step Instructions (Python)

  1. Design the Request Pydantic Schema: Define the PromptRequest structure with a mandatory prompt field to pass the text being verified. Fix the model="qwen2.5:3b" and format="json" parameters as immutable properties by default.
  2. Routing the Filter Endpoint: Create a POST /api/generate method that sends the sanitized payload to the local Ollama API (OLLAMA_HOST).
  3. Cleaning Markdown Formatting from Response: Set up the regular expression re.sub(r"```json\s*|```", "", raw_text) to remove any potential formatting tags returned by the language model before converting the string into JSON.
  4. Implement a Soft Fault Tolerance Policy (Soft Fallback):
    • Set an execution limit of 25 seconds (timeout=25) for requests processing in Ollama.
    • In case of json.JSONDecodeError exceptions, network timeouts, or Ollama overload errors (status_code != 200), invoke the fallback_response method.
    • The fallback function must not return an interface-blocking error. Instead, it generates and returns a clean, safe default list of products (Chicken fillet, Potatoes, Cream), allowing the ingestion pipeline to complete the user session successfully.