CodeSensei is an AI-powered coding assistant designed to help you with coding tasks, questions, and queries. Built using Python, Gradio, and the Ollama CodeLlama model, CodeSensei is equipped to provide you with coding solutions, explanations, and insights to help you improve your coding skills.
- AI-Powered Code Assistance: Get help with your coding tasks, whether you're a beginner or an experienced developer.
- Natural Language Processing: Understands natural language prompts, allowing you to ask questions in plain English.
- Multiple Programming Languages: Supports a variety of programming languages and provides feedback and solutions accordingly.
- User-Friendly Interface: Simple and intuitive interface built using Gradio, making it easy to interact with the assistant.
- User Input: The user inputs a coding task or question into the prompt box.
- Pre-Processing: The input is pre-processed, which may involve cleaning, tokenizing, or normalizing the user's input.
- Task Analysis: The assistant determines if the task is code-related. If not, it provides general assistance.
- Keyword Extraction: If code-related, keywords or context are extracted from the input.
- Language Detection: The assistant identifies the programming language used.
- Code Analysis: The assistant analyzes the code structure and logic.
- Feedback: Finally, the assistant provides code feedback, suggestions, or answers to the coding queries.
import requests
import json
import gradio as gr
# Set the URL for the API endpoint
url="http://localhost:11434/api/generate"
# Define headers for the API request
headers = {
'Content-Type': 'application/json'
}
# Initialize an empty list to store the conversation history
history = []
def generate_response(Prompt):
# Append the current prompt to the history
history.append(Prompt)
final_prompt = "\n".join(history)
# Prepare the data to send to the API
data = {
"model": "codesensei",
"Prompt": final_prompt,
"stream": False
}
# Send a POST request to the API
response = requests.post(url, headers=headers, data=json.dumps(data))
# Check if the request was successful
if response.status_code == 200:
response = response.text
data = json.loads(response)
actual_response = data['response']
return actual_response
else:
print("error:", response.text)
# Setup Gradio interface
interface = gr.Interface(
fn=generate_response, # Function to be called for prediction
inputs=gr.Textbox(lines=2, placeholder="Enter your Prompt"), # Input textbox
outputs="text", # Output display
title="CodeSensei", # Title of the interface
description="CodeSensei is an AI assistant designed to help you with coding tasks and questions. Just enter your code or query in the textbox above, and CodeSensei will provide you with helpful responses and insights.", # Description of the interface
theme="HaleyCH/HaleyCH_Theme", # Theme for better visualization
)
# Launch the interface
interface.launch()
- Import Statements: The necessary libraries are imported, including
requests
for making API calls,json
for handling JSON data, andgradio
for creating the user interface. - URL and Headers: The URL and headers are set up for making POST requests to the API that powers CodeSensei.
- Conversation History: A list named
history
is initialized to store the conversation context, which helps maintain continuity in the dialogue. - Generate Response Function: This function handles the user's prompt, sends it to the API, and processes the response to display it back to the user.
- Gradio Interface: The user interface is set up with Gradio, defining the input and output components and setting the title and description.
In this screenshot, the user has greeted CodeSensei with "Hello!" and the assistant responds by offering its help for any programming-related tasks.
Here, the user has asked CodeSensei for guidance on creating a portfolio website, and the assistant provides a step-by-step guide on how to get started.
To run CodeSensei locally, follow these steps:
- Clone the repository.
git clone https://github.com/yourusername/codesensei.git
- Navigate to the project directory.
cd codesensei
- Install the required packages.
pip install -r requirements.txt
- Run the application.
python codesensei.py
If you would like to contribute to this project, feel free to fork the repository and submit a pull request with your changes.