A full-stack web application that accepts Python or JavaScript code snippets and generates a plain-English explanation using an AI model. The application also identifies key code parts, estimates time and space complexity, suggests an optimized version where applicable, and displays a simple diff-style comparison.
- Java 17
- Spring Boot
- Spring Web
- Spring Data JPA
- H2 Database
- Bean Validation
- Gemini API integration
- React
- Vite
- JavaScript
- CSS
- Gemini 2.5 Flash-Lite
Gemini 2.5 Flash-Lite was selected because the use case requires fast, lightweight explanations for short code snippets. The model is suitable for low-latency code analysis tasks and keeps the project easy to run during local review.
- Accepts Python and JavaScript code snippets
- Generates a 2-4 sentence plain-English explanation
- Identifies key functions, loops, conditions, and logic blocks
- Shows estimated time and space complexity
- Suggests optimized code where applicable
- Displays a diff-style comparison between original and optimized code
- Stores recent analysis history using H2 database
- Handles validation errors and AI service errors gracefully
The application follows a simple full-stack architecture:
React Frontend → Spring Boot REST API → Code Analysis Service → Gemini Client Service → Gemini API
The backend is organized into layered packages:
controllerhandles REST endpointsservicecontains business logic and Gemini API integrationdtocontains request and response modelsentitycontains the JPA entity for stored snippetsrepositoryhandles database accessexceptionprovides centralized error handlingconfigcontains Gemini API configuration
POST /api/snippets/analyze
Request:
{
"language": "PYTHON",
"code": "def add(a, b):\n return a + b"
}Response:
{
"id": 1,
"language": "PYTHON",
"originalCode": "def add(a, b):\n return a + b",
"explanation": "This Python function takes two arguments and returns their sum.",
"keyParts": ["def add(a, b):", "return a + b"],
"timeComplexity": "O(1)",
"spaceComplexity": "O(1)",
"optimizedCode": "def add(a, b):\n return a + b",
"optimizationSummary": "The code is already optimal for its intended purpose."
}GET /api/snippets
Returns recent submitted snippets and their explanations.
- Java 17 or later
- Node.js and npm
- Gemini API key
Set the Gemini API key as an environment variable:
setx GEMINI_API_KEY "your_api_key_here"Restart the terminal, then run:
.\mvnw spring-boot:runBackend runs at:
http://localhost:8080
cd frontend
npm install
npm run devFrontend runs at:
http://localhost:5173
The backend uses a strict prompt that instructs the AI model to analyze only the provided code and avoid assuming missing files, hidden functions, or external behavior. The model is asked to return structured JSON so the backend can parse and display consistent results. The system also keeps the original code visible next to the AI-generated explanation and optimized version so the user can compare the output. External AI service failures are handled through centralized exception handling instead of exposing stack traces.
- The application does not execute user-submitted code.
- H2 is used for lightweight local persistence.
- The database resets when the backend restarts because it uses in-memory storage.
- The Gemini API key is loaded from an environment variable and should not be committed to GitHub.