Skip to content

Commit df1e801

Browse files
jayhacktkucar
authored andcommitted
[WIP] Modal Demo (#381)
Co-authored-by: jayhack <[email protected]>
1 parent 6224ca6 commit df1e801

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Repository Analyzer API
2+
3+
A simple Modal API endpoint that analyzes GitHub repositories using Codegen. The API returns basic metrics about any public GitHub repository including:
4+
5+
- Total number of files
6+
- Number of functions
7+
- Number of classes
8+
9+
## Running Locally
10+
11+
1. Install dependencies:
12+
13+
```bash
14+
uv add modal
15+
```
16+
17+
2. Start the API server:
18+
19+
```bash
20+
modal serve src/codegen/extensions/modal/api.py
21+
```
22+
23+
3. Test with curl:
24+
25+
```bash
26+
# Replace with your local Modal endpoint URL
27+
curl "{URL}?repo_name=fastapi/fastapi"
28+
```
29+
30+
## Response Format
31+
32+
The API returns JSON in this format:
33+
34+
```json
35+
{
36+
"status": "success",
37+
"error": "",
38+
"num_files": 123,
39+
"num_functions": 456,
40+
"num_classes": 78
41+
}
42+
```
43+
44+
If there's an error, you'll get:
45+
46+
```json
47+
{
48+
"status": "error",
49+
"error": "Error message here",
50+
"num_files": 0,
51+
"num_functions": 0,
52+
"num_classes": 0
53+
}
54+
```
55+
56+
## Development
57+
58+
The API is built using:
59+
60+
- Modal for serverless deployment
61+
- FastAPI for the web endpoint
62+
- Codegen for repository analysis
63+
64+
To deploy changes:
65+
66+
```bash
67+
modal deploy src/codegen/extensions/modal/api.py
68+
```

src/codegen/extensions/modal/api.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Modal API endpoint for repository analysis."""
2+
3+
import modal
4+
from pydantic import BaseModel
5+
6+
from codegen import Codebase
7+
8+
# Create image with dependencies
9+
image = modal.Image.debian_slim(python_version="3.13").apt_install("git").pip_install("fastapi[standard]", "codegen>=0.5.30")
10+
11+
# Create Modal app
12+
app = modal.App("codegen-repo-analyzer")
13+
14+
15+
class RepoMetrics(BaseModel):
16+
"""Response model for repository metrics."""
17+
18+
num_files: int = 0
19+
num_functions: int = 0
20+
num_classes: int = 0
21+
status: str = "success"
22+
error: str = ""
23+
24+
25+
@app.function(image=image)
26+
@modal.web_endpoint(method="GET")
27+
def analyze_repo(repo_name: str) -> RepoMetrics:
28+
"""Analyze a GitHub repository and return metrics.
29+
30+
Args:
31+
repo_name: Repository name in format 'owner/repo'
32+
33+
Returns:
34+
RepoMetrics object containing repository metrics or error information
35+
"""
36+
try:
37+
# Validate input
38+
if "/" not in repo_name:
39+
return RepoMetrics(status="error", error="Repository name must be in format 'owner/repo'")
40+
41+
# Initialize codebase
42+
codebase = Codebase.from_repo(repo_name)
43+
44+
# Calculate metrics
45+
num_files = len(codebase.files(extensions="*")) # Get all files
46+
num_functions = len(codebase.functions)
47+
num_classes = len(codebase.classes)
48+
49+
return RepoMetrics(
50+
num_files=num_files,
51+
num_functions=num_functions,
52+
num_classes=num_classes,
53+
)
54+
55+
except Exception as e:
56+
return RepoMetrics(status="error", error=str(e))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[project]
2+
name = "codegen-repo-analyzer"
3+
version = "0.1.0"
4+
description = "Modal API endpoint for analyzing GitHub repositories using Codegen"
5+
requires-python = ">=3.13"
6+
dependencies = ["modal>=0.73.25", "fastapi[standard]", "codegen>=0.5.30"]

0 commit comments

Comments
 (0)