Course ES
← back to chapter

Live rewrite

Paste any draft paragraph — an email, a Slack message, a commit note —

Live rewrite

Paste any draft paragraph — an email, a Slack message, a commit note — and watch a tighter version stream in. Same facts, fewer words, better flow. The streaming output makes the "second draft" feel immediate.

Run

bash bootstrap-secrets.sh              # reads ../../../../.env, writes secrets/
docker compose up --build              # default: PROVIDER=openai

Open http://localhost:3000.

To run against Gemini instead:

PROVIDER=gemini docker compose up --build

What's where

  • backend/ai_openai.py — streams a rewrite of the input paragraph
  • backend/ai_gemini.py — same prompt, other SDK
  • backend/main.py — FastAPI: /api/ai (final) + /api/ai/stream (SSE)
  • frontend/app/page.tsx — textarea + streamed result panel

Stop

docker compose down

Run locally

Download the project as a ZIP and run it with Docker. Brings up a FastAPI backend + Next.js frontend on localhost:3000.

Download live-rewrite.zip

unzip live-rewrite.zip
cd live-rewrite
bash bootstrap-secrets.sh   # one-time: pulls API keys into ./secrets
docker compose up --build   # default provider: openai
# or:  PROVIDER=gemini docker compose up --build

Type some input, pick a provider, and run the same code shown in Source against the live API. Sign-in required.


  

The same modules the Run button hits. The whole project (frontend, Dockerfile, compose) is in the ZIP under README.

backend/ai_openai.py

"""Week 3 - Showcase 3 (OpenAI): live-rewrite.

Takes a draft paragraph and streams back a clearer, tighter version
that keeps every fact intact. Useful for editing messages, commit
notes, doc blurbs — anywhere the user wants a faster second draft.

`run_stream` yields tokens (so the local UI sees a live edit happening);
`run` accumulates for the website's non-streaming contract.
"""
from typing import Iterator

from openai import OpenAI

_client = OpenAI()


def _prompt(draft: str) -> str:
    return (
        "Rewrite the following paragraph for clarity, brevity, and natural "
        "flow. Keep every fact and intent intact. Return only the rewritten "
        "paragraph; no commentary, no headings, no preamble."
        f"\n\n---\n{draft}\n---"
    )


def run_stream(draft: str) -> Iterator[str]:
    # Cap output length so the rewrite can't run away on us. 2048
    # tokens (~6k chars) is well over any reasonable rewrite of an
    # 8k-char input paragraph.
    stream = _client.responses.create(
        model="gpt-5.4-nano", input=_prompt(draft), stream=True,
        max_output_tokens=2048,
    )
    for event in stream:
        if event.type == "response.output_text.delta":
            yield event.delta


def run(draft: str) -> str:
    return "".join(run_stream(draft))

backend/ai_gemini.py

"""Week 3 - Showcase 3 (Gemini): live-rewrite.

Same prompt and contract as ai_openai.py; different SDK shape.
"""
import os
from typing import Iterator

from google import genai
from google.genai import types

_client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])


def _prompt(draft: str) -> str:
    return (
        "Rewrite the following paragraph for clarity, brevity, and natural "
        "flow. Keep every fact and intent intact. Return only the rewritten "
        "paragraph; no commentary, no headings, no preamble."
        f"\n\n---\n{draft}\n---"
    )


def run_stream(draft: str) -> Iterator[str]:
    # Cap output length so the rewrite can't run away on us. 2048
    # tokens (~6k chars) is well over any reasonable rewrite of an
    # 8k-char input paragraph.
    stream = _client.models.generate_content_stream(
        model="gemini-3.1-flash-lite", contents=_prompt(draft),
        config=types.GenerateContentConfig(max_output_tokens=2048),
    )
    for chunk in stream:
        if chunk.text:
            yield chunk.text


def run(draft: str) -> str:
    return "".join(run_stream(draft))

Project files

  • .gitignore
  • README.md
  • backend/Dockerfile
  • backend/ai_gemini.py
  • backend/ai_openai.py
  • backend/main.py
  • backend/requirements.txt
  • bootstrap-secrets.sh
  • docker-compose.yml
  • frontend/Dockerfile
  • frontend/app/layout.tsx
  • frontend/app/page.tsx
  • frontend/next.config.ts
  • frontend/package.json
  • frontend/tsconfig.json