A tool for analyzing and explaining Python code using AI
Project description
Python Code Explainer with Llama
A powerful tool that analyzes Python code and generates comprehensive documentation and explanations using the Llama language model. Available both as a Streamlit web application and a command-line interface.
โจ Features
- ๐ Automatic extraction of functions, classes, and methods from Python code
- ๐ Detailed code analysis including arguments, return values, and docstrings
- ๐ค AI-powered non-technical explanations using Llama model
- ๐ฅ๏ธ Multiple interfaces: Web UI and Command Line
- ๐ฅ Export analysis as well-formatted Word documents
- ๐ GitHub Action for automated documentation generation
- ๐งฉ Modular architecture for easy extension
๐ Quick Start
Prerequisites
- Python 3.8 or higher
- Ollama server running locally (with Llama model installed)
Installation
-
Clone the repository:
git clone https://github.com/yourusername/python-code-explainer.git cd python-code-explainer
-
Install the package in development mode:
pip install -e .
Or install the required packages directly:
pip install -r requirements.txt
-
Set up Ollama:
# Install Ollama (if not already installed) # Visit https://ollama.ai for installation instructions # Start Ollama server in one terminal ollama serve # In another terminal, pull the Llama model ollama pull llama2
๐ Usage
Web Interface
-
Start the Streamlit application:
streamlit run app.py
Or:
python -m src.code_explainer.cli web
-
Open your web browser to
http://localhost:8501 -
Upload a Python file and explore the analysis
Command Line Interface
Analyze a Python file:
python -m src.code_explainer.cli analyze path/to/your/file.py
Generate documentation:
python -m src.code_explainer.cli document path/to/your/file.py --output docs/
๐ ๏ธ How It Works
-
Code Analysis: The tool parses Python files to extract:
- Function and class definitions
- Method signatures with arguments and return types
- Docstrings and inline comments
- Import statements and module-level documentation
-
AI Integration: The extracted information is processed by the Llama model to generate:
- Clear, non-technical explanations
- Usage examples
- Documentation in multiple formats
-
Output Generation: Results can be:
- Viewed in the web interface
- Exported as Word documents
- Generated automatically via GitHub Actions
๐ฆ Project Structure
.
โโโ src/
โ โโโ code_explainer/
โ โ โโโ __init__.py
โ โ โโโ cli.py # Command line interface
โ โ โโโ code_analyzer.py # Core code analysis logic
โ โ โโโ document_generator.py # Document generation
โ โ โโโ llm_integration.py # Llama model integration
โ โโโ action.py # GitHub Action entry point
โ โโโ analyzer.py # Core analysis functionality
โโโ .github/workflows/ # GitHub Actions workflows
โโโ app.py # Streamlit web app
โโโ main.py # Legacy entry point
โโโ requirements.txt # Python dependencies
โโโ setup.py # Package configuration
GitHub Actions
This project includes a GitHub Actions workflow (.github/workflows/code_analysis.yml) that:
- Runs tests on push and pull requests
- Performs code analysis using the Python Code Explainer
- Generates documentation in Word format
- Automatically commits and pushes generated documentation to the repository
Workflow Triggers
- Push to main branch: Runs tests, performs analysis, and updates documentation
- Pull requests: Runs tests and performs analysis (does not update documentation)
- Manual trigger: Can be manually triggered from the Actions tab
Generated Documentation
When the workflow runs on the main branch, it will:
- Generate a Word document (
docs/code_analysis_YYYYMMDD_HHMMSS.docx) - Include a summary of all analyzed code elements
- Provide detailed explanations of the code structure
- Automatically commit and push the generated documentation
Required Permissions
The workflow requires the following permissions:
contents: write- To commit and push generated documentationpull-requests: write- To update pull request statusesstatuses: write- To update commit statuses
These permissions are automatically provided by the default GITHUB_TOKEN. No additional configuration is needed for public repositories. For private repositories, ensure the workflow has the necessary permissions in your repository settings.
๐ License
MIT License - see LICENSE for details.
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development Setup
- Fork and clone the repository
- Set up a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -e .[dev]
- Make your changes and run tests
- Submit a pull request
๐ง Contact
For any questions or feedback, please open an issue on the GitHub repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file code_explainer-0.1.0.tar.gz.
File metadata
- Download URL: code_explainer-0.1.0.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
de606109f4bf2b4dbc3982d469a3abff88fc5e100a916b3ec609d93f43f7ce15
|
|
| MD5 |
0cf17f0c32004037d743e9ac0a7366b3
|
|
| BLAKE2b-256 |
e830e64acf8d003b5aee0a2e4a0c404d1f02a2b593c5eeff2f9da51915c19a0a
|
File details
Details for the file code_explainer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: code_explainer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
825bfb524c70c65cbed87c056eb457dea3d9d070a28db7ae906dfbc535b00215
|
|
| MD5 |
df265d30baae6aafaebaa8b5f4dd8e91
|
|
| BLAKE2b-256 |
55d9d5b1c36826003f7f07193198519b91f8a9de1f1e28ceba6d33674ecc2e63
|