Skip to content

EhteZafar/AI-Code-Explainer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🤖 AI Code Explainer

Perfect CS Course Project ⭐⭐ Difficulty | AI-Powered Code Analysis

An intelligent web application that explains any code snippet in simple, understandable language using OpenAI's GPT models. Perfect for students, educators, and developers who want to understand code quickly.

✨ Features

  • 🔍 Instant Code Analysis - Paste any code and get immediate explanations
  • 🌐 Multi-Language Support - Works with Python, JavaScript, Java, C++, Go, Rust, and more
  • 📊 Adjustable Detail Levels - Choose between simple, medium, or detailed explanations
  • 🎯 Auto Language Detection - Automatically identifies the programming language
  • 📈 Complexity Assessment - Evaluates code complexity (Simple/Moderate/Complex)
  • 🎨 Beautiful UI - Clean, modern Streamlit interface
  • Fast & Efficient - Optimized API with proper error handling

🏗️ Architecture

AI-Code-Explainer/
├── backend/                 # FastAPI Backend
│   ├── app/
│   │   ├── __init__.py
│   │   ├── config.py       # Configuration management
│   │   ├── models.py       # Pydantic models
│   │   ├── routes/         # API endpoints
│   │   │   ├── __init__.py
│   │   │   └── code_routes.py
│   │   └── services/       # Business logic
│   │       ├── __init__.py
│   │       └── openai_service.py
│   ├── main.py             # FastAPI app entry
│   ├── requirements.txt
│   └── .env.example
├── frontend/               # Streamlit Frontend
│   ├── app.py             # Streamlit UI
│   └── requirements.txt
├── .gitignore
└── README.md

🚀 Quick Start

Prerequisites

  • Python 3.8 or higher
  • OpenAI API key (Get one here)
  • pip (Python package manager)

Installation

  1. Clone the repository
git clone https://github.com/EhteZafar/AI-Code-Explainer.git
cd AI-Code-Explainer
  1. Set up Backend
cd backend

# Create virtual environment (recommended)
python -m venv venv

# Activate virtual environment
# Windows:
venv\Scripts\activate
# Linux/Mac:
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Configure environment
copy .env.example .env
# Edit .env and add your OpenAI API key
  1. Set up Frontend
cd ../frontend

# Create virtual environment (recommended)
python -m venv venv

# Activate virtual environment
# Windows:
venv\Scripts\activate
# Linux/Mac:
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

Configuration

Edit backend/.env with your settings:

OPENAI_API_KEY=sk-your-actual-api-key-here
OPENAI_MODEL=gpt-4o-mini
OPENAI_MAX_TOKENS=1000
OPENAI_TEMPERATURE=0.7

Running the Application

Terminal 1 - Start Backend:

cd backend
python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000

Terminal 2 - Start Frontend:

cd frontend
streamlit run app.py

The application will open automatically in your browser at http://localhost:8501

📖 Usage

  1. Paste Your Code - Copy any code snippet into the text area
  2. Select Language (Optional) - Choose the programming language or use auto-detect
  3. Choose Detail Level - Select how detailed you want the explanation
    • Simple: Beginner-friendly explanations
    • Medium: Balanced technical explanation
    • Detailed: Deep technical dive
  4. Click "Explain Code" - Get instant AI-powered explanation
  5. Review Results - See explanation, detected language, and complexity

🔧 API Endpoints

POST /api/explain

Explain code using AI

Request Body:

{
  "code": "def hello():\n    print('Hello, World!')",
  "language": "Python",
  "detail_level": "medium"
}

Response:

{
  "explanation": "This is a Python function...",
  "language": "Python",
  "complexity": "Simple",
  "success": true
}

GET /api/supported-languages

Get list of supported programming languages

🛠️ Tech Stack

  • Backend: FastAPI (Modern, fast Python web framework)
  • AI: OpenAI GPT-4o-mini (Cost-effective, powerful)
  • Frontend: Streamlit (Interactive Python web apps)
  • Validation: Pydantic (Data validation)
  • Async: AsyncIO (Efficient async operations)

📝 Code Quality Features

  • Type Hints - Full type annotations for better code clarity
  • Input Validation - Pydantic models for request/response validation
  • Error Handling - Comprehensive exception handling
  • Logging - Detailed logging for debugging
  • CORS Support - Proper CORS configuration
  • Best Practices - Follows FastAPI and Python best practices

🎓 Why This is a Great CS Project

  1. Relevant to CS - Uses AI/ML, web development, and software engineering
  2. Modern Tech Stack - FastAPI and OpenAI are industry-standard
  3. Practical Application - Solves a real problem for developers
  4. Scalable Architecture - Clean separation of concerns
  5. Easy to Demonstrate - Interactive UI for presentations
  6. Extensible - Easy to add features like code optimization, bug detection, etc.

🚀 Future Enhancements

  • Code syntax highlighting
  • Support for code snippets with multiple files
  • Save/export explanations
  • User authentication
  • Explanation history
  • Code improvement suggestions
  • Multiple AI model options
  • Batch processing
  • API rate limiting
  • Docker deployment

📊 Cost Considerations

Using GPT-4o-mini:

  • Very cost-effective (~$0.00015 per request)
  • Perfect for student projects
  • Excellent quality explanations

Estimated costs for 1000 explanations: ~$0.15

🐛 Troubleshooting

Backend won't start:

  • Verify Python version: python --version (should be 3.8+)
  • Check if port 8000 is available
  • Ensure all dependencies are installed: pip install -r requirements.txt

OpenAI API errors:

Frontend can't connect to backend:

  • Ensure backend is running on port 8000
  • Check API_URL in frontend/app.py
  • Verify CORS settings in backend/app/config.py

📄 License

This project is open source and available under the MIT License.

👨‍💻 Author

Ehte Zafar

🤝 Contributing

Contributions, issues, and feature requests are welcome! Feel free to check the issues page.

⭐ Show Your Support

Give a ⭐️ if this project helped you!


Built with ❤️ for Computer Science Students

hello

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages