How muhammad-akhtar/ai_visibility works
Purpose and Entry Points
AI Visibility Intelligence is a Flask application that evaluates whether a business domain appears for commercially relevant AI/search questions, then generates content-gap briefs. Application construction starts at app/__init__.py; server entry points are run.py (Waitress/Gunicorn selection) and wsgi.py. It serves both JSON APIs under /api/v1 and a server-rendered Jinja dashboard.
HTTP and Presentation Boundary
JSON routes manage profiles, pipeline execution, query browsing, and rechecks in app/api/profiles.py and app/api/queries.py. HTML dashboard routes are separate in app/web/views.py, using the same persisted models and calling the API for long-running actions. Serialization and API-shaped responses are centralized in app/api/serializers.py; error behavior distinguishes API JSON from UI HTML through app/errors.py.
Pipeline Orchestration
app/services/pipeline.py is the control-plane boundary and the only pipeline layer intended to write to the database. It creates/tracks a pipeline run, invokes agents sequentially, persists discovered/scored queries, and tolerates recommendation failure after query persistence. Runs are synchronous request work (roughly 10–30 seconds); rechecks score one existing query and may refresh recommendations.
Discovery Agent: LLM Question Generation
app/agents/discovery.py uses OpenAI GPT-4o to produce validated structured buyer questions and associated seed keywords. Seed keywords bridge conversational LLM questions to keyword-provider-compatible terms. Shared agent construction/error behavior resides in app/agents/base.py; typed request and LLM-output validation is represented under app/schemas/__init__.py.
Scoring Agent: Search Evidence and Opportunity
app/agents/scoring.py evaluates seed keywords without an LLM. It obtains volume, difficulty, intent, organic results, and AI Overview evidence through app/services/dataforseo.py; SERP visibility checks use parallel workers and isolate per-query failures as unknown. Opportunity score computation is a distinct deterministic policy in app/utils/scoring.py, combining demand, ranking difficulty, visibility gap, and intent.
Recommendation Agent: Gap-to-Brief Generation
app/agents/recommendation.py sends only confirmed not_visible queries, ordered by opportunity, to GPT-4o and produces concrete content recommendations. Invalid individual recommendations are discarded, and a complete Agent 3 failure does not invalidate already-scored queries. This isolates generative output from the evidence-based scoring stage.
Persistence, Runtime, and Infrastructure
SQLAlchemy entities define the durable workflow state: profiles, pipeline runs, discovered queries, and recommendations in app/models/profile.py, app/models/pipeline_run.py, and app/models/query.py. Extensions are initialized in app/extensions.py; schema lifecycle is managed by Alembic under migrations/versions/001_initial_schema.py. PostgreSQL is the required datastore. External services are OpenAI GPT-4o and DataForSEO. Docker Compose (docker-compose.yml) starts PostgreSQL, applies migrations, and runs Gunicorn; rate limiting is Flask-Limiter with in-memory storage, so it is process-local.
Main entry points: pipeline.py Pipeline control plane · __init__.py Flask app factory · discovery.py Question discovery · recommendation.py Content recommendations
Components 20
Runtime
- Gunicorn / Waitress
run.py - Flask app factory
app/__init__.py - Flask extensions · Initializes SQLAlchemy and process-local rate limiting.
app/extensions.py - Docker Compose · Starts PostgreSQL, applies migrations, and runs Gunicorn.
docker-compose.yml
HTTP & UI
- JSON API routes
app/api/profiles.py - Query API routes
app/api/queries.py - Jinja dashboard · Calls APIs for long-running work.
app/web/views.py - API serializers
app/api/serializers.py
Pipeline
- Pipeline control plane · Only pipeline layer intended to write workflow results.
app/services/pipeline.py
Agents & Evidence
- Question discovery · Generates validated buyer questions and seed keywords.
app/agents/discovery.py - Evidence scoring · Parallel SERP checks isolate failed queries as unknown.
app/agents/scoring.py - Content recommendations · Processes confirmed not-visible queries only.
app/agents/recommendation.py - Agent base
app/agents/base.py - Typed schemas
app/schemas/__init__.py - DataForSEO · Supplies keyword metrics, organic results, and AI Overview evidence.
app/services/dataforseo.py - Opportunity policy · Combines demand, difficulty, visibility gap, and intent.
app/utils/scoring.py - OpenAI GPT-4o
Persistence
- Workflow models · Profiles, runs, queries, and recommendations.
app/models/query.py - PostgreSQL
- Alembic schema
migrations/versions/001_initial_schema.py
Connections 27
- Gunicorn / Waitress → Flask app factory: creates app
- Flask app factory → Flask extensions: initializes
- Flask app factory → JSON API routes: registers
- Flask app factory → Query API routes: registers
- Flask app factory → Jinja dashboard: registers
- JSON API routes → Pipeline control plane: starts runs
- Query API routes → Pipeline control plane: rechecks
- Jinja dashboard → JSON API routes: uses for actions
- JSON API routes → API serializers: serializes
- Query API routes → API serializers: serializes
- Pipeline control plane → Question discovery: discovers questions
- Pipeline control plane → Evidence scoring: scores queries
- Pipeline control plane → Content recommendations: generates briefs · Failure is tolerated after query persistence.
- Pipeline control plane → Workflow models: persists workflow
- Question discovery → Agent base: uses
- Content recommendations → Agent base: uses
- Question discovery → Typed schemas: validates output
- Content recommendations → Typed schemas: validates output
- Question discovery → OpenAI GPT-4o: generates via
- Content recommendations → OpenAI GPT-4o: generates via
- Evidence scoring → DataForSEO: retrieves evidence
- Evidence scoring → Opportunity policy: computes score
- Workflow models → PostgreSQL: stores in
- Alembic schema → PostgreSQL: creates schema
- Docker Compose → Gunicorn / Waitress: runs
- Docker Compose → PostgreSQL: starts
- Docker Compose → Alembic schema: applies