Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI-Powered Code Explainer

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.

Tech Stack

Backend

  • Java 17
  • Spring Boot
  • Spring Web
  • Spring Data JPA
  • H2 Database
  • Bean Validation
  • Gemini API integration

Frontend

  • React
  • Vite
  • JavaScript
  • CSS

AI Model

  • 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.

Features

  • 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

Architecture

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:

  • controller handles REST endpoints
  • service contains business logic and Gemini API integration
  • dto contains request and response models
  • entity contains the JPA entity for stored snippets
  • repository handles database access
  • exception provides centralized error handling
  • config contains Gemini API configuration

API Endpoints

Analyze Code

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 Recent Snippets

GET /api/snippets

Returns recent submitted snippets and their explanations.

Running the Application Locally

Prerequisites

  • Java 17 or later
  • Node.js and npm
  • Gemini API key

Backend Setup

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:run

Backend runs at:

http://localhost:8080

Frontend Setup

cd frontend
npm install
npm run dev

Frontend runs at:

http://localhost:5173

Hallucination and Accuracy Handling

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.

Notes

  • 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.

About

AI-powered code explainer using Spring Boot, React, and Gemini

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages