CoolFace
Apppublic

Shrook21/Code-Assistant

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
App README

๐Ÿค– Smart Code Assistant

An intelligent code assistant that can generate, explain, and analyze code using advanced AI models. Built with LangChain, LangGraph, and deployed with Gradio.

[image]

๐Ÿš€ Features

  • โ€”Code Generation: Generate code based on natural language descriptions
  • โ€”Code Explanation: Get detailed explanations of existing code
  • โ€”Code Analysis: Analyze code for improvements and understanding
  • โ€”Interactive Web Interface: User-friendly Gradio interface
  • โ€”Intelligent Task Classification: Automatically determines the type of request
  • โ€”Vector Database Integration: Uses ChromaDB for efficient code retrieval

๐Ÿ—๏ธ Architecture

LangGraph Workflow

mermaid
graph TD
    A[User Input] --> B[Classification Node]
    B --> C{Task Type}
    C -->|Generate Code| D[Code Generation Node]
    C -->|Explain Code| E[Code Explanation Node]  
    C -->|Unclear| F[Fallback Node]
    D --> G[Vector Search Tool]
    E --> H[Final Response]
    F --> H
    G --> H
    H --> I[User Interface]

The assistant uses a conditional graph that:

  1. 1.Classifies the user's input (generate, explain, or unclear)
  2. 2.Routes to appropriate specialized nodes
  3. 3.Retrieves relevant code examples from vector database
  4. 4.Generates contextual responses using AI models

๐Ÿ“ Project Structure

Code-Assistant/
โ”œโ”€โ”€ app.py                    # Main Gradio application
โ”œโ”€โ”€ requirements.txt          # Python dependencies
โ”œโ”€โ”€ smart_assistant.png       # Project image
โ”œโ”€โ”€ agents/
โ”‚   โ”œโ”€โ”€ nodes.py             # LangGraph nodes implementation
โ”‚   โ””โ”€โ”€ state.py             # Graph state management
โ”œโ”€โ”€ chroma_code_db/          # Vector database storage
โ”œโ”€โ”€ config/
โ”‚   โ””โ”€โ”€ settings.py          # Configuration settings
โ”œโ”€โ”€ graph/
โ”‚   โ””โ”€โ”€ conditional_graph.py # LangGraph workflow definition
โ”œโ”€โ”€ prompts/
โ”‚   โ””โ”€โ”€ prompts.py           # AI prompts and templates
โ”œโ”€โ”€ tools/
โ”‚   โ””โ”€โ”€ tools.py             # Custom tools and utilities
โ”œโ”€โ”€ utils/
โ”‚   โ””โ”€โ”€ code_splitter.py     # Code parsing utilities
โ””โ”€โ”€ vectorstore/
    โ”œโ”€โ”€ builder.py           # Vector database builder
    โ””โ”€โ”€ retriever.py         # Document retrieval logic

๐Ÿ› ๏ธ Installation & Setup

Prerequisites

  • โ€”Python 3.8+
  • โ€”Ollama (for local AI models)

Local Installation

  1. 1.Clone the repository:
bash
   git clone https://github.com/yourusername/Code-Assistant.git
   cd Code-Assistant
  1. 1.Install dependencies:
bash
   pip install -r requirements.txt
  1. 1.Set up Ollama:
bash
   # Install Ollama (visit ollama.ai for installation instructions)
   # Pull your preferred model
   ollama pull llama2  # or your preferred model
  1. 1.Configure settings: Edit config/settings.py to set your preferred model and parameters The vector database will be built automatically on first run
  1. 1.Run the application:
bash
   python app.py
  1. 1.Open your browser: Navigate to http://localhost:7860 Enter your name to start the assistant

๐ŸŒ Online Demo

Try the live demo on Hugging Face Spaces:

๐Ÿ”— Code Assistant - Live Demo

No installation required - just open the link and start using the assistant!

๐Ÿ“– Usage

Getting Started

  1. 1.Enter your name when prompted
  2. 2.Ask questions about code in natural language:
  3. 3."Generate a function to sort a list in Python"
  4. 4."Explain how bubble sort works"
  5. 5."Create a REST API endpoint for user login"

Example Interactions

๐Ÿ‘ค User: "Create a function that finds the maximum number in a list"

๐Ÿค– Assistant:

๐Ÿ” Task Classification: GENERATE CODE

def find_maximum(numbers):
    """
    Find the maximum number in a list.
    
    Args:
        numbers (list): List of numbers
        
    Returns:
        int/float: Maximum number in the list
    """
    if not numbers:
        raise ValueError("List cannot be empty")
    
    max_num = numbers[0]
    for num in numbers[1:]:
        if num > max_num:
            max_num = num
    
    return max_num

# Example usage:
my_list = [3, 1, 4, 1, 5, 9, 2, 6]
result = find_maximum(my_list)
print(f"Maximum number: {result}")  # Output: Maximum number: 9

๐Ÿงช Development

Adding New Features

  • โ€”Custom Nodes: Add new processing nodes in agents/nodes.py
  • โ€”Tools: Extend functionality in tools/tools.py
  • โ€”Prompts: Customize AI behavior in prompts/prompts.py
  • โ€”Graph Logic: Modify workflow in graph/conditional_graph.py

Testing Locally

bash
# Run with debug mode
python app.py --debug

# Test individual components
python -m vectorstore.builder  # Build vector database
python -m agents.nodes         # Test individual nodes

๐Ÿค Contributing

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

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • โ€”LangChain - For the AI framework
  • โ€”LangGraph - For workflow orchestration
  • โ€”Gradio - For the web interface
  • โ€”Ollama - For local AI model serving
  • โ€”ChromaDB - For vector database functionality
  • โ€”Hugging Face - For model hosting and deployment

โญ Star this repository if you find it helpful!