Files

swot-agent

Python 3.10+ FastAPI Code style: black

A web application that performs automated SWOT analysis (Strengths, Weaknesses, Opportunities, Threats) analysis using the Gemini 2.0 Flash model and the Pydantic AI agent framework. The application is built with FastAPI, HTMX, and Tailwind CSS.

SWOT Analysis Demo

The agent includes three tools:

  • Content Extraction: Extracts content from a web page given a URL
  • Community Insights: Calls Reddit API to get community insights from relevant subreddits
  • Competitive Analysis: Calls Gemini API to get competitive analysis

Getting Started

Prerequisites

Installation

  1. Clone the repository and change to the swot-agent directory.

  2. Install the required dependencies:

    pip install -r requirements.txt
    
  3. Set up your environment variables:

    # Google Cloud settings
    export GOOGLE_CLOUD_PROJECT=your_project_id
    export GOOGLE_APPLICATION_CREDENTIALS=path_to_service_account.json
    
    # (Optional) Reddit API credentials
    export REDDIT_CLIENT_ID=your_reddit_client_id
    export REDDIT_CLIENT_SECRET=your_reddit_client_secret
    
    # (Optional) Application settings
    export APP_SECRET_KEY=your_secret_key
    
  4. Run the application:

    python main.py
    

    You can also use the FastAPI CLI:

    fastapi dev --port 8080
    
  5. Open your web browser and navigate to http://localhost:8080.

Usage

  1. Enter a valid URL in the input field
  2. Click "Analyze" to initiate the AI SWOT analysis
  3. The AI agent will:
    • Extract content from the provided URL
    • Process the content using Gemini 2.0
    • Generate structured SWOT insights
    • Present results in an organized format
  4. View the SWOT analysis results

Deployment

To deploy the application to Google Cloud Run, run the following command:

gcloud run deploy swot-agent --source . --region us-central1 --allow-unauthenticated

You may need to add the aiplatform.user IAM role to your service account.

Configure secrets for the APP_SECRET_KEY, REDDIT_CLIENT_ID, and REDDIT_CLIENT_SECRET. You can run the application without setting these, but the Reddit tool will not be available.

Troubleshooting

If you receive an error about the Gemini quota being exceeded, you can request a quota increase or try another model.

Testing

The project includes test suites for both the AI agent and the FastAPI application in the tests directory.

Running Tests

  1. Install test dependencies:

    pip install pytest pytest-asyncio httpx
    
  2. Run all tests:

    pytest -v
    

Project Structure

swot-agent/
├── main.py            # FastAPI application and server setup
├── agent.py           # SWOT analysis agent implementation
├── tests/             # Test suites
│   ├── __init__.py    # To make tests a Python package
│   ├── test_agent.py  # AI agent test suite
│   └── test_main.py   # FastAPI endpoint test suite
├── templates/         # HTML templates
│   ├── index.html     # Main application page
│   ├── status.html    # Analysis status updates
│   └── result.html    # SWOT analysis results
├── requirements.txt   # Python dependencies
├── LICENSE            # License information
└── README.md          # Project documentation

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.