Download README.md from imkrish/chatbot: direct link, hf CLI and curl.
- Browser
- Download file 7.67 kB
-
https://huggingface.co/spaces/imkrish/chatbot/resolve/main/README.md
- Command line
-
hf download hf://spaces/imkrish/chatbot/README.md
-
curl -L -o README.md https://huggingface.co/spaces/imkrish/chatbot/resolve/main/README.md
title: Chatbot
emoji: π€
colorFrom: indigo
colorTo: blue
sdk: docker
sdk_version: '1.0'
app_file: app.py
pinned: false
CampVerse Chatbot Microservice
Features
- Gemini AI Integration: Enhanced NLP understanding using Google's Gemini API
- Intent Classification: Smart detection of user intents (event search, greetings, help, etc.)
- Semantic Event Search: Find events using natural language queries
- FAQ Support: Answer common questions about the platform
- Real-time Chat: WebSocket support for live conversations
Quick Start for New Users
If you're new and want to run this chatbot microservice, you can choose either Docker or Python. Both methods are described below:
π Run via Python (Local Development)
- Clone the Repository
git clone https://github.com/CcpC-cuj/CampVerse.git
cd CampVerse/ML/chatbot
- Install Python & Dependencies Make sure you have Python 3.10+ installed. Then install dependencies:
pip install -r requirements.txt
Set Environment Variables
export BACKEND_URL=http://localhost:5001 # Backend API URL export GEMINI_API_KEY=your_gemini_api_key # For enhanced NLP (optional)Start the Backend Service
uvicorn app:app_socket --host 0.0.0.0 --port 8000Test with CLI Chat Client Open a new terminal and run:
python cli_chat.py
Type your questions and interact with the bot.
π³ Run via Docker (Containerized)
- Build the Docker Image
docker build -t chatbot-service .
- Run the Docker Container
docker run -p 8000:8000 -e BACKEND_URL=http://localhost:5001 chatbot-service ```3. **Test the Service**
- REST API:
curl -X POST http://localhost:8000/chatbot -H "Content-Type: application/json" -d '{"question":"How do I register?"}' - Socket.IO:
python cli_chat.py
π Integrate with Your Web App
Use REST API or Socket.IO as shown below to connect your frontend.
A production-ready chatbot microservice using FastAPI, Socket.IO, and sentence transformers for FAQ-based question answering.
Features
- π FastAPI REST API: HTTP endpoint for Q&A
- π Socket.IO Real-time: WebSocket support for live chat
- π€ AI-Powered: Uses sentence transformers and FAISS for semantic search
- π― Intent Classification: Detects greetings, event queries, help requests, and more
- π Dynamic Event Discovery: Fetches and searches live events from backend API
- π³ Docker Ready: Containerized for easy deployment
- π Logging: Comprehensive logging for debugging
- β Input Validation: Validates question length and content
- π CORS Enabled: Ready for frontend integration
Architecture
βββββββββββββββββββ
β Frontend β
β (React/Web) β
ββββββββββ¬βββββββββ
β
β HTTP POST /chatbot
β OR Socket.IO
β
ββββββββββΌβββββββββ
β app.py β
β FastAPI + β
β Socket.IO β
ββββββββββ¬βββββββββ
β
β Semantic Search
β
ββββββββββΌβββββββββ
β faq.json β
β (FAQ Database) β
βββββββββββββββββββ
API Endpoints
REST API
POST /chatbot
Request:
{
"question": "How do I register?"
}
Response:
{
"question": "How do I register?",
"answer": "Click on the Register button..."
}
Socket.IO Events
Client β Server
user_question: Send a questionsocket.emit('user_question', { question: 'How do I login?' });
Server β Client
bot_answer: Receive answersocket.on('bot_answer', (data) => { console.log(data.answer); });
Setup & Installation
Local Development
Install dependencies:
pip install -r requirements.txtRun the server:
uvicorn app:app_socket --host 0.0.0.0 --port 8000Test with CLI client:
python cli_chat.py
Docker Deployment
Build the image:
docker build -t chatbot-service .Run the container:
docker run -p 8000:8000 chatbot-serviceTest the service:
- REST API:
curl -X POST http://localhost:8000/chatbot -H "Content-Type: application/json" -d '{"question":"How do I register?"}' - Socket.IO:
python cli_chat.py
- REST API:
Project Structure
chatbot/
βββ app.py # Main backend (FastAPI + Socket.IO)
βββ cli_chat.py # CLI client for testing
βββ Dockerfile # Docker configuration
βββ faq.json # FAQ database
βββ requirements.txt # Python dependencies
βββ README.md # This file
Integration with Frontend
React Example (Socket.IO)
import io from 'socket.io-client';
const socket = io('http://localhost:8000');
socket.on('connect', () => {
console.log('Connected to chatbot');
});
socket.on('bot_answer', (data) => {
console.log('Answer:', data.answer);
});
// Send question
socket.emit('user_question', { question: 'How do I login?' });
React Example (REST API)
const response = await fetch('http://localhost:8000/chatbot', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ question: 'How do I login?' })
});
const data = await response.json();
console.log(data.answer);
Configuration
Environment Variables
You can configure the service using environment variables:
PORT: Server port (default: 8000)LOG_LEVEL: Logging level (default: INFO)
FAQ Management
Edit faq.json to add/update questions and answers:
[
{
"question": "How do I register?",
"answer": "Click on the Register button on the homepage..."
}
]
After updating, rebuild the Docker image or restart the server.
Monitoring & Logs
The service logs all:
- Client connections/disconnections
- Incoming questions
- Matched FAQ pairs
- Errors and warnings
View logs in Docker:
docker logs -f <container-id>
Security
- CORS is enabled for all origins (configure for production)
- Input validation (max 512 characters)
- Error handling to prevent crashes
- No rate limiting (add if needed for production)
Testing
Manual Testing
- Start the service
- Run CLI client:
python cli_chat.py - Type questions and verify responses
Testing REST API
curl -X POST http://localhost:8000/chatbot \
-H "Content-Type: application/json" \
-d '{"question":"How do I register?"}'
Troubleshooting
Service won't start:
- Check if port 8000 is available
- Verify all dependencies are installed
- Check logs for errors
CLI client can't connect:
- Ensure service is running on port 8000
- Check firewall settings
- Verify
python-socketiois installed
No answer returned:
- Check if
faq.jsonexists and is valid - Verify the question exists in FAQ database
- Check server logs for errors
Future Enhancements
- Add rate limiting for production
- Add authentication for Socket.IO
- Support multiple languages
- Add conversation history
- Implement fallback responses
- Add metrics and analytics
License
Part of the CampVerse project.