Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeGuru — Local Code Teaching Assistant

CodeGuru is a locally running AI-powered code teaching assistant built with Ollama, Python, Requests, and Gradio.

The model is designed to help users solve coding-related problems, generate code, explain programming concepts, debug code, and provide programming guidance.

Model Instruction

You are a code teaching assistant named as CodeGuru created by Aman. Answer all the code related questions being asked.

The application sends user prompts to a locally hosted Ollama API and displays the model's response through a simple Gradio web interface.

Features

  • Runs locally using Ollama
  • Uses a custom Ollama model named CodeGuru
  • Designed specifically for coding-related questions and problems
  • Generates code and programming explanations
  • Helps with programming concepts and debugging
  • Simple Gradio web interface
  • Uses the local Ollama API
  • No external AI API key is required for the local setup
  • Python virtual environment can be stored separately from the project

Project Structure

Codellama/
│
├── app.py
├── modelfile
├── requirements.txt
├── README.md
├── .gitignore
│
├── images/
│   ├── codeguru-introduction-response.png
│   └── codeguru-fibonacci-response.png
│
└── venv/
    └── (virtual environment stored on another drive)

Note: The venv is shown in the project structure for documentation purposes. The actual virtual environment is stored separately on another drive to save space on the project/C: drive.

Virtual Environment Location

The virtual environment used for this project is stored at:

G:\Program Files\Python Environments\codellama

It does not need to be physically inside the project folder.

Activate it in PowerShell with:

& "G:\Program Files\Python Environments\codellama\Scripts\Activate.ps1"

After activation, the terminal should show:

(codellama)

The virtual environment is excluded from Git using .gitignore.

Technologies Used

  • Python
  • Ollama
  • Gradio
  • Requests
  • JSON
  • PowerShell
  • Virtual Environment (venv)

How It Works

The application communicates with the locally running Ollama API.

User Coding Problem
        ↓
Gradio Interface
        ↓
Python app.py
        ↓
Requests HTTP POST
        ↓
Ollama Local API
        ↓
CodeGuru Model
        ↓
Generated Coding Answer
        ↓
Gradio Output

1. Install Ollama

Install Ollama on your computer and make sure the Ollama service is running.

Check that Ollama is available:

ollama --version

2. Pull the CodeLlama Base Model

Before creating the custom CodeGuru model, pull the CodeLlama base model using Ollama:

ollama pull codellama

This downloads the CodeLlama model that is used as the base model for CodeGuru.

You can verify that CodeLlama has been downloaded with:

ollama list

You should see codellama in the list of available models.

You can also test the base model directly:

ollama run codellama

3. Create the CodeGuru Model

The custom CodeGuru model is created from the project's modelfile.

Open PowerShell in the directory containing the modelfile and run:

ollama create Codeguru -f modelfile

This creates the custom Ollama model named:

Codeguru

The modelfile uses CodeLlama as the base model and adds the custom CodeGuru instructions.

Verify that the CodeGuru model exists:

ollama list

You can also test the custom model directly:

ollama run Codeguru

4. Set Up the Python Environment

Activate the existing virtual environment:

& "G:\Program Files\Python Environments\codellama\Scripts\Activate.ps1"

Install the required Python packages:

pip install -r requirements.txt

5. Run the Application

Make sure Ollama is running, activate the codellama environment, and run:

python app.py

Gradio will start a local server, for example:

http://127.0.0.1:7860

Open the displayed local URL in your browser.

Example Responses

CodeGuru Introduction Response

CodeGuru introduction response

Fibonacci Code Generation

CodeGuru Fibonacci response

API Request

The application sends a request similar to:

data = {
    "model": "Codeguru",
    "prompt": final_prompt,
    "stream": False
}

The request is sent to the local Ollama generate endpoint:

http://localhost:11434/api/generate

The response is then converted from JSON and the generated text is returned to Gradio.

Important Code

The response is parsed using:

data = response.json()
actual_response = data["response"]

Using response.json() avoids the json.load() error that occurs when trying to load a normal string as a file object.

Requirements

The project requires Python and the packages listed in requirements.txt.

Install them with:

pip install -r requirements.txt

Ollama must also be installed separately because it runs the local AI model.

The CodeLlama base model must be downloaded before creating CodeGuru:

ollama pull codellama

Then create the custom model:

ollama create Codeguru -f modelfile

Local-Only Architecture

                    Your Computer
┌────────────────────────────────────────────────────┐
│                                                    │
│   Browser                                          │
│      │                                             │
│      ▼                                             │
│   Gradio :7860                                     │
│      │                                             │
│      ▼                                             │
│   Python app.py                                    │
│      │                                             │
│      ▼                                             │
│   Ollama :11434                                    │
│      │                                             │
│      ▼                                             │
│   CodeGuru Model                                   │
│                                                    │
└────────────────────────────────────────────────────┘

The model inference is performed locally through Ollama.

Git and .gitignore

The project includes a .gitignore file to prevent unnecessary or private files from being committed to GitHub.

Important ignored files/folders include:

venv/
.venv/
__pycache__/
.env
.vscode/
*.log
*.gguf
*.bin
*.safetensors

The virtual environment is intentionally not uploaded to GitHub because it contains installed packages and can be recreated using requirements.txt.

Troubleshooting

ollama command is not recognized

Make sure Ollama is installed and available in your system PATH.

CodeLlama model not found

Pull the base CodeLlama model:

ollama pull codellama

Then verify:

ollama list

Model not found

Check the installed models:

ollama list

If CodeGuru is missing, recreate it:

ollama create Codeguru -f modelfile

Gradio starts but no response is generated

Check that Ollama is running and test the model directly:

ollama run Codeguru

Then restart the Python application:

python app.py

AttributeError: 'str' object has no attribute 'read'

Do not use:

json.load(response)

when response is a string.

Use:

data = response.json()

Future Improvements

  • Add proper chat history containing both user prompts and model responses
  • Add a Chatbot-style Gradio interface
  • Add code syntax highlighting
  • Add a clear conversation button
  • Add model selection
  • Add temperature and other model parameters
  • Add streaming responses
  • Add code execution/sandbox support
  • Improve the UI and branding

License

This project is licensed under the MIT License.

See the LICENSE file for details.

Author

Aman Singh

Built as a local Generative AI project using Ollama + CodeGuru + Gradio.

About

CodeGuru is a local AI-powered code teaching assistant built with Ollama, Python, and Gradio to solve coding problems, generate code, and explain programming concepts.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages