CoolFace
Apppublic

namanbagoria/CodingAssistant

sourceHugging Faceapache-2.0updated 3mo agoView on Hugging Face
0likes
App README

๐Ÿš€ MultiLang Code Assistant (CodeLlama)

![Python Version](https://www.python.org/) ![Gradio UI](https://gradio.app/) ![Ollama](https://ollama.com/) ![Code Llama](https://meta.ai/) ![License](LICENSE)

An AI-powered, multilingual coding assistant built using Code Llama running locally through Ollama. It helps developers generate, explain, debug, optimize, and translate code across multiple programming languages without relying on cloud-based APIs or sharing sensitive code with external servers.


๐Ÿ“– Table of Contents


๐ŸŒŸ Overview

This project demonstrates how to deploy and utilize locally hosted Large Language Models (LLMs) for software development tasks. By integrating Code Llama with a local Ollama API server and a python-based Gradio user interface, it provides a seamless playground for testing local code intelligence.


๐Ÿค– What is a Code Assistant?

A Code Assistant is an AI-driven tool designed to aid developers in writing, debugging, explaining, optimizing, and translating code across various languages. By leveraging models trained on large corpuses of source code, it acts as an intelligent pair programmer to increase developer productivity, reduce debugging cycles, and facilitate learning new programming paradigms.


๐Ÿง  About Code Llama & Ollama

  • โ€”Code Llama: A family of state-of-the-art code generation models developed by Meta Platforms, built on top of Llama 2 architecture. It is trained on code-specific datasets, making it highly proficient in generating code and understanding programming syntax across Python, C++, Java, JavaScript, SQL, and more.
  • โ€”Ollama: A lightweight framework designed to bundle, run, and manage open-source LLMs locally on your own machine. It provides a simple CLI and runs a local server that exposes REST API endpoints for text generation.

๐Ÿ”’ Advantages of Running LLMs Locally

  1. 1.Data Privacy & Security: Your code stays entirely on your machine. No external servers or third-party APIs process your prompts, making it safe for proprietary or sensitive codebases.
  2. 2.Zero Latency/Network Costs: Completely free to use with no token counts or subscription limits, and functions independently of internet bandwidth.
  3. 3.Offline Availability: Work anywhere, even with no internet connection.
  4. 4.Full Customizability: Fine-tune parameters (like temperature) or specify custom system instructions and persona profiles via a modelfile.

โœจ Key Features

  • โ€”๐Ÿ–ฅ๏ธ Interactive Web UI: A simple and clean Gradio interface for chatting with the assistant.
  • โ€”๐Ÿ’ฌ Contextual History: Keeps track of prior messages during the session to maintain conversational flow.
  • โ€”โšก Local LLM Execution: Uses the customized CodeMan model based on Code Llama.
  • โ€”๐ŸŒ Multilingual Support: Supports code translation, generation, optimization, and debugging across Python, C++, Java, JavaScript, SQL, etc.

๐Ÿ“ Repository Structure

text
MultiLangCodeAssCodellama/
โ”œโ”€โ”€ .gitignore          # Excludes temporary, build, and environment files
โ”œโ”€โ”€ LICENSE             # MIT License
โ”œโ”€โ”€ README.md           # Documentation
โ”œโ”€โ”€ app.py              # Application entry point and Gradio UI
โ”œโ”€โ”€ modelfile           # Ollama custom model configuration
โ”œโ”€โ”€ requirements.txt    # Python package dependencies
โ””โ”€โ”€ theory.txt          # Theoretical details of Code Llama

๐Ÿ“„ File Descriptions

  • โ€”`app.py`: The core application logic. It initiates a Gradio web interface, processes the conversation prompt history, sends a POST request to Ollama's local generation endpoint (http://localhost:11434/api/generate), parses the response, and displays it in the browser.
  • โ€”`modelfile`: Defines a custom Ollama model configuration named CodeMan. It uses codellama as the base model, sets the temperature parameter to 1, and configures a system prompt defining the persona of a code teaching assistant created by Naman.
  • โ€”`requirements.txt`: Declares dependencies needed to run the application (specifically gradio and langchain).
  • โ€”`theory.txt`: Contains reference documentation summarizing the architecture, sizes, and capabilities of the Code Llama model family.

โš™๏ธ Application Workflow

The diagram below outlines how user prompts travel through the application stack:

mermaid
sequenceDiagram
    actor User
    participant App as app.py (Gradio UI)
    participant Ollama as Ollama API (Local Port 11434)
    participant CodeMan as Custom CodeMan Model (Code Llama)

    User->>App: Enters prompt in text area
    Note over App: App appends prompt to session history<br/>and joins them using newlines
    App->>Ollama: POST HTTP request to /api/generate<br/>(Body: model=CodeMan, prompt=history)
    Ollama->>CodeMan: Feeds input prompt to model
    CodeMan-->>Ollama: Generates response output
    Ollama-->>App: Returns JSON payload (HTTP 200)
    Note over App: App parses 'response' from JSON
    App-->>User: Displays response in Gradio UI output box
  1. 1.Input: The user enters a programming-related prompt into the Gradio UI.
  2. 2.Context Compilation: The script compiles the input prompt, joining it with the current session's history using newline characters.
  3. 3.API Dispatch: The compiled prompt is sent as a JSON payload in an HTTP POST request to Ollama's local REST API server.
  4. 4.Local Execution: The customized Code Llama model (CodeMan) processes the prompt and returns a completed response.
  5. 5.Output: The application receives the JSON response, extracts the generated code or explanation text, and displays it directly to the user.

๐Ÿ› ๏ธ Technologies Used

  • โ€”Python: Core programming language.
  • โ€”Gradio: Framework used to create the interactive web interface.
  • โ€”Requests: HTTP library to interact with Ollama's API.
  • โ€”JSON: To structure API payloads and parse model outputs.
  • โ€”Ollama: Framework running the local LLM server.
  • โ€”Code Llama: Base LLM for coding intelligence.
  • โ€”LangChain: Included in the requirements list.

๐Ÿš€ Installation & Setup

1. Prerequisites

  • โ€”Python 3.8 or higher.
  • โ€”Ollama installed on your system.

2. Install and Run Ollama

  1. 1.Download Ollama from the official website and install it on your OS.
  2. 2.Ensure Ollama service is active. On Windows/macOS, it typically runs in the system tray.
  3. 3.Download the base Code Llama model using the terminal command:
bash
   ollama pull codellama

3. Create the Custom Model

Build the custom model CodeMan defined in the modelfile:

bash
ollama create CodeMan -f ./modelfile

4. Clone and Setup Environment

  1. 1.Clone this repository to your local machine:
bash
   git clone <repository-url>
   cd MultiLangCodeAssCodellama
  1. 1.Create a virtual environment:
bash
   python -m venv venv
  1. 1.Activate the virtual environment:
  2. 2.Windows (Command Prompt / PowerShell):
cmd
     venv\Scripts\activate
  • โ€”macOS / Linux:
bash
     source venv/bin/activate
  1. 1.Install the required dependencies:
bash
   pip install -r requirements.txt

5. Run the Application

Start the local Python application:

bash
python app.py

After executing, Gradio will spin up a local server. Open the URL printed in the terminal (usually http://127.0.0.1:7860) in your web browser.


๐Ÿ’ก Example Usage

Use the following example prompts to test the coding assistant's capabilities:

Task TypeExample Prompt
Generate Python Code"Write a Python function to find the nth Fibonacci number using dynamic programming."
Explain C++ Algorithm"Explain how the QuickSort algorithm works and analyze its time complexity in C++."
Debug Java Code"Why does this Java code throw a NullPointerException? `String name = null; System.out.println(name.length());`"
Translate Languages"Translate this Python function to JavaScript: `def greet(name): return f'Hello, {name}'`"
Optimize SQL Queries"Optimize this SQL query to improve join performance on large tables: `SELECT FROM users u, orders o WHERE u.id = o.user_id;`"*

๐Ÿ”ฎ Future Improvements

  • โ€”Streaming Responses: Enable token-by-token streaming in the UI to remove the waiting time for the full output block.
  • โ€”Dynamic Parameter Tuning: Add sliders in Gradio to adjust parameters like temperature, top_k, and top_p at runtime.
  • โ€”LangChain Integration: Leverage LangChain to implement retrieval-augmented generation (RAG) over local documents.
  • โ€”Session Management: Add options to save, export, or clear session history from the UI.

๐Ÿค Contributing

Contributions are welcome! If you would like to submit improvements or report issues:

  1. 1.Fork this repository.
  2. 2.Create a branch for your feature (git checkout -b feature/AmazingFeature).
  3. 3.Commit your changes (git commit -m 'Add some AmazingFeature').
  4. 4.Push to the branch (git push origin feature/AmazingFeature).
  5. 5.Open a Pull Request.

๐Ÿ“„ License

This project is licensed under the terms of the MIT License. See LICENSE for details.