All projects

Technical Report · Web & Data Applications

Word Cloud Generator FastAPI

FastAPI service that accepts text, generates a word-cloud image and returns the result as base64-encoded data for easy use by web/API clients.

AUTHOR  Gaurav Verma CATEGORY  Web & Data Applications SOURCE  https://github.com/gaurav-321/Word-Cloud-Generator-FastAPI DATE  Mar 2022 STATUS  published
View Source Python FastAPI WordCloud Uvicorn

Abstract

A small FastAPI service that wraps the WordCloud library behind a single REST endpoint. A client POSTs raw text; the service renders a word-cloud image and returns it as a base64 string inside a JSON body, so no shared filesystem or file download is needed on the client side.

1. What This Is

I built this to expose word-cloud generation as an HTTP service rather than a local-only script. The repository splits the API entry point from the generation logic into separate modules, keeping the FastAPI layer thin and the core render function independently testable.

The base64 transport is the main design choice: the client receives the full image payload in the JSON response, which makes the endpoint callable from a browser, a mobile app, or another microservice without any file-sharing mechanism.

2. How It Works

The request path is linear and short. Uvicorn serves the FastAPI app; the single endpoint hands the text to the generation module, which calls WordCloud and returns the encoded bytes.

# Stage Input Tool Output
01 Receive text HTTP POST body FastAPI Parsed string
02 Generate word cloud Raw text WordCloud In-memory image
03 Encode image Image bytes Base64 Encoded string
04 Return JSON Encoded string FastAPI HTTP 200 + JSON body

3. Constraints

  • No authentication or rate limiting

    The endpoint is open. Any caller can POST arbitrary text and trigger image rendering, which is CPU-bound. In production this would need at minimum an API key and a request throttle.

  • Base64 payload bloat

    Encoding the image inflates the response by roughly 33 % over raw bytes. For high-resolution clouds the JSON body can grow into the hundreds of kilobytes, which is wasteful compared to streaming an image response.

  • No input validation or size cap

    There is no upper bound on the text length a client can send. A very large input will make the WordCloud render step slow and memory-hungry with no guardrail.

  • Single image format

    The endpoint returns one fixed format. There is no query parameter to request PNG vs. SVG or to control font size, colour palette, or mask shape.

4. Next

  1. a.Add a simple API-key header check and a per-IP rate limit so the render endpoint cannot be abused.
  2. b.Expose query parameters for image format, width, height, and colour palette so the same endpoint serves multiple client needs.
  3. c.Write unit tests around the generation module (mock the WordCloud call) and an integration test that hits the endpoint with a sample payload.

— end of report —