This is a machine learning-based chatbot built using Django (backend) and React (frontend), allowing users to upload datasets (TXT, CSV, JSON), train a Naive Bayes machine learning model, and query the trained model in real-time. The chatbot uses WebSockets for real-time communication, allowing for dynamic user interactions, while the knowledge base is created using user-uploaded data.
This project provides:
- Dataset ingestion from text files, CSVs, and JSON.
- Dynamic machine learning model training to create an intelligent chatbot.
- Real-time chat using Django Channels and React.
- A simple frontend interface allowing for file uploads and chat.
- π File Upload: Upload datasets to train the chatbot.
- π Real-Time Chat: Chat with the bot in real time, with responses based on the trained model.
- β‘ Machine Learning Model: Dynamic training using TF-IDF and Naive Bayes.
- π€ Speech Recognition (Optional): Speak to the bot using the browserβs Web Speech API.
- π Notifications: Get browser notifications when new messages arrive.
- π» WebSockets: Real-time two-way communication for instant responses.
chatbot_project/
βββ backend/ # Django Backend
β βββ chatbot_project/ # Django project config
β β βββ asgi.py # ASGI config for WebSockets
β β βββ settings.py # Django settings, WebSocket config
β β βββ urls.py # Main URL routing for HTTP views and file upload
β βββ chatbot/ # Main chatbot app
β βββ consumers.py # WebSocket consumer for real-time chat
β βββ routing.py # WebSocket URL routing
β βββ views.py # Django views for file upload and chatbot queries
β βββ chatbot_logic.py # Chatbot logic: file processing, model training, queries
β βββ intents.json # Static intents data (optional, depending on the use case)
βββ frontend/ # React Frontend
β βββ public/ # Public files (HTML, assets)
β β βββ index.html # Main HTML file
β βββ src/ # React source files
β β βββ components/ # React components
β β β βββ Chat.js # Main chat component (file upload, WebSocket)
β β βββ App.js # Main React App component
β β βββ index.js # Entry point for React
β β βββ Chat.css # Styling for chat interface
βββ Procfile # Heroku deployment configuration
βββ requirements.txt # Python dependencies (Django, channels, joblib, pandas, etc.)
βββ README.md # Project documentation
# Clone the repository
git clone https://github.com/kyleBrian/machine-learning-chatbot.git
cd machine-learning-chatbot/backend/
# Create a virtual environment and install dependencies
python3 -m venv env
source env/bin/activate
pip install -r requirements.txt
# Apply migrations and run the Django server
python manage.py migrate
python manage.py runserver
# Navigate to the frontend folder
cd ../frontend/
# Install React dependencies
npm install
# Start the React frontend
npm start
To train the chatbot, upload datasets via the provided interface on the frontend. The supported file formats are TXT, CSV, and JSON. The dataset will be processed and used to train the machine learning model.
const handleFileUpload = async () => {
if (file) {
const formData = new FormData();
formData.append('file', file);
try {
const response = await axios.post('/upload/', formData);
alert(response.data.message); // Show upload result
} catch (error) {
console.error('File upload failed:', error);
}
}
};
Once the dataset is uploaded and processed, you can chat with the bot in real-time via WebSockets. The responses are generated based on the trained model.
const handleSendMessage = () => {
if (inputMessage.trim() !== '') {
sendMessage(JSON.stringify({ message: inputMessage })); // Send message to backend
setInputMessage(''); // Clear the input field
}
};
-
Install Heroku CLI:
heroku login
-
Create Heroku App:
heroku create your-app-name
-
Set up the
Procfile
for Heroku (to usedaphne
for WebSocket support):web: daphne chatbot_project.asgi:application --port $PORT --bind 0.0.0.0
-
Push the Backend to Heroku:
git push heroku master
-
Build the React App:
npm run build
-
Deploy to Netlify:
- You can drag and drop the
build/
folder onto Netlify for easy deployment.
- You can drag and drop the
-
Backend (Django): When a file is uploaded, Django processes the file, passes the data to the Naive Bayes model (with TF-IDF for vectorization), and saves the trained model.
def add_data(self, file_data, file_type): if file_type == 'txt': data = [line.strip() for line in file_data if line.strip()] self.train_model(data) elif file_type == 'csv': df = pd.read_csv(file_data) self.train_model(df['question'], df['answer']) elif file_type == 'json': data = json.load(file_data) self.train_model(questions, answers)
- Django: Backend framework.
- React: Frontend framework.
- Django Channels: WebSockets for real-time communication.
- Scikit-learn: Machine learning library (TF-IDF + Naive Bayes).
- Heroku: Deployment platform for the backend.
- Netlify: Deployment platform for the frontend.
- Redis: WebSocket backend (used by Django Channels).
- GitHub: kyleBrian
- Email: [email protected]
- Phone: +254 758034649
This project is licensed under the MIT License - see the LICENSE file for details.