Skip to content

[WIP] Modal Demo #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/codegen/extensions/modal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Repository Analyzer API

A simple Modal API endpoint that analyzes GitHub repositories using Codegen. The API returns basic metrics about any public GitHub repository including:

- Total number of files
- Number of functions
- Number of classes

## Running Locally

1. Install dependencies:

```bash
uv add modal
```

2. Start the API server:

```bash
modal serve src/codegen/extensions/modal/api.py
```

3. Test with curl:

```bash
# Replace with your local Modal endpoint URL
curl "{URL}?repo_name=fastapi/fastapi"
```

## Response Format

The API returns JSON in this format:

```json
{
"status": "success",
"error": "",
"num_files": 123,
"num_functions": 456,
"num_classes": 78
}
```

If there's an error, you'll get:

```json
{
"status": "error",
"error": "Error message here",
"num_files": 0,
"num_functions": 0,
"num_classes": 0
}
```

## Development

The API is built using:

- Modal for serverless deployment
- FastAPI for the web endpoint
- Codegen for repository analysis

To deploy changes:

```bash
modal deploy src/codegen/extensions/modal/api.py
```
56 changes: 56 additions & 0 deletions src/codegen/extensions/modal/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Modal API endpoint for repository analysis."""

import modal
from pydantic import BaseModel

from codegen import Codebase

# Create image with dependencies
image = modal.Image.debian_slim(python_version="3.13").apt_install("git").pip_install("fastapi[standard]", "codegen>=0.5.30")

# Create Modal app
app = modal.App("codegen-repo-analyzer")


class RepoMetrics(BaseModel):
"""Response model for repository metrics."""

num_files: int = 0
num_functions: int = 0
num_classes: int = 0
status: str = "success"
error: str = ""


@app.function(image=image)
@modal.web_endpoint(method="GET")
def analyze_repo(repo_name: str) -> RepoMetrics:
"""Analyze a GitHub repository and return metrics.

Args:
repo_name: Repository name in format 'owner/repo'

Returns:
RepoMetrics object containing repository metrics or error information
"""
try:
# Validate input
if "/" not in repo_name:
return RepoMetrics(status="error", error="Repository name must be in format 'owner/repo'")

# Initialize codebase
codebase = Codebase.from_repo(repo_name)

# Calculate metrics
num_files = len(codebase.files(extensions="*")) # Get all files
num_functions = len(codebase.functions)
num_classes = len(codebase.classes)

return RepoMetrics(
num_files=num_files,
num_functions=num_functions,
num_classes=num_classes,
)

except Exception as e:
return RepoMetrics(status="error", error=str(e))
6 changes: 6 additions & 0 deletions src/codegen/extensions/modal/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
name = "codegen-repo-analyzer"
version = "0.1.0"
description = "Modal API endpoint for analyzing GitHub repositories using Codegen"
requires-python = ">=3.13"
dependencies = ["modal>=0.73.25", "fastapi[standard]", "codegen>=0.5.30"]