DEV Community

Cover image for City Simulation with LLMs — Predicting Traffic & Generating Natural-Language Reports
Preetkamal Singh
Preetkamal Singh

Posted on

City Simulation with LLMs — Predicting Traffic & Generating Natural-Language Reports

TL;DR: I built a city traffic simulation that ingests sensor data, predicts congestion, and uses an LLM to produce human-readable incident reports.

Live demo:https://vercel.com/kamalpannus-projects/trafficpredictorfrontend | Code: https://github.com/Kamalpannu/trafficPredictor-Backend
https://github.com/Kamalpannu/trafficpredictorfrontend


Problem

City operations teams need quick summaries of traffic hotspots from noisy sensor streams. Raw numbers are hard to scan under time pressure — so I built a system that predicts traffic issues and auto-generates plain-English reports.

Architecture

  • Data ingestion: simulated traffic sensors → time series store
  • Prediction service: Python model (simple regression + heuristic)
  • LLM service: LangChain wrapping an LLM to summarize predictions into reports
  • Frontend: React app for dashboards and report viewer
  • Dockerized and deployed via CI/CD

Key implementation


python
from fastapi import FastAPI
from pydantic import BaseModel
from langchain import OpenAI, LLMChain, PromptTemplate

app = FastAPI()
llm = OpenAI(api_key="YOUR_KEY", temperature=0.2)
prompt = PromptTemplate(
    input_variables=["summary"],
    template="You are a city ops assistant. Given this summary: {summary}\nWrite a 3-sentence incident report with suggested actions."
)
chain = LLMChain(llm=llm, prompt=prompt)

class Payload(BaseModel):
    summary: str

@app.post("/report")
async def report(payload: Payload):
    text = chain.run(payload.summary)
    return {"report": text}

Note: replace YOUR_KEY with your environment variable or secrets management.

What I learned

LLMs are great at turning numeric summaries into useful prose, but they require clear prompts and guardrails.

Keep the LLM step after deterministic prediction — don’t let it invent numbers.

Results

Generated concise reports that a non-technical operator can act on.

Demo and sample reports: see https://github.com/Kamalpannu/trafficPredictor-Backend
https://github.com/Kamalpannu/trafficpredictorfrontend

Full source code and deployment steps: https://vercel.com/kamalpannus-projects/trafficpredictorfrontend
Enter fullscreen mode Exit fullscreen mode

Top comments (0)