Spaces:
Running
title: AI Chatbot
emoji: π€
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.49.1
app_file: app.py
pinned: false
license: mit
short_description: A simple AI chatbot built with Gradio
AI Chatbot with Database Integration
A smart AI chatbot built with Gradio that can handle general conversation and answer specific questions from a database/FAQ system.
Features
- π€ General Conversation: Handles greetings, help requests, thanks, and goodbyes
- π Smart Database Integration: Intelligent fuzzy matching finds answers even with different wording
- π§ AI-Powered Matching: Uses keyword extraction, text similarity, and semantic matching
- π Smart Learning: Automatically saves unanswered questions to
/unanswered_questionstable for review - π¬ Interactive Chat Interface: Clean, modern UI built with Gradio
- π Error Handling: Robust error handling for network issues and API failures
- π± Responsive Design: Works on desktop and mobile devices
How It Works
The chatbot operates in three modes:
- Conversation Mode: Recognizes common conversation patterns like greetings, help requests, and thanks
- Smart Database Query Mode: For specific questions, it uses intelligent matching to find relevant answers
- Learning Mode: When no answer is found, it automatically saves the question to your database for review
Smart Matching Algorithm
The chatbot uses a sophisticated matching system that combines:
- Text Normalization: Removes punctuation, converts to lowercase, handles whitespace
- Enhanced Keyword Extraction: Identifies important words with stemming (removes 's', 'ing', 'ed')
- Sequence Matching: Uses difflib for character-level similarity
- Keyword Overlap: Calculates Jaccard similarity between keyword sets
- Substring Matching: Detects when one text contains another with length weighting
- Word Order Similarity: Matches common word sequences
- Semantic Similarity: Groups related words (money, time, contact, requirements, etc.)
- Phrase Matching: Recognizes common phrase patterns
- Adaptive Thresholds: Adjusts matching criteria based on query length
- Weighted Scoring: Combines all methods with optimized weights
Example: If your database has "What are the admission requirements?" and a user asks "admission requirements", the chatbot will find a match with 68% similarity!
Success Rate: The improved algorithm achieves 92% success rate (23/25 test cases matched) compared to the previous 66.7%!
Local Development
Prerequisites
- Python 3.7 or higher
- pip package manager
Installation
- Clone or download this repository
- Install the required dependencies:
pip install -r requirements.txt
Running Locally
python app.py
The chatbot will be available at http://localhost:7860
Deployment to Hugging Face Spaces
Step 1: Create a Hugging Face Account
- Go to Hugging Face and create an account
- Verify your email address
Step 2: Create a New Space
- Navigate to Create a New Space
- Fill in the details:
- Space Name: Choose a unique name (e.g.,
my-ai-chatbot) - License: Select an appropriate license (e.g., MIT)
- Space Hardware: Choose CPU (free tier)
- Visibility: Set to Public or Private as desired
- SDK: Select "Gradio"
- Space Name: Choose a unique name (e.g.,
Step 3: Upload Files
Upload these files to your Hugging Face Space:
app.py- Main application filerequirements.txt- DependenciesREADME.md- This documentation (optional)
Step 4: Configure Settings
- In your Space settings, make sure the SDK is set to "Gradio"
- The App file should be set to
app.py - If your database requires authentication, add credentials as secrets in the Space settings
Step 5: Deploy
Once all files are uploaded, your Space will automatically build and deploy. The process usually takes 2-5 minutes.
Database Integration
The chatbot connects to your database at https://database-dhe2.onrender.com and tries multiple endpoints:
Answer Retrieval Endpoints:
/faq/search/query/api/faq
Smart Matching Endpoints (for getting all questions):
/questions/faq/all/api/questions/all_questions
Unanswered Questions Endpoints:
/unanswered_questions/api/unanswered_questions/save_question/api/save_question
Request Formats:
GET Request for Answers:
https://database-dhe2.onrender.com/faq?question=your_question_here
POST Request for Answers:
{
"question": "your_question_here"
}
POST Request for Saving Unanswered Questions:
{
"question": "unanswered_question_here",
"created_at": "2024-01-01T12:00:00.000000"
}
Response Formats:
{"answer": "response_text"}{"response": "response_text"}["response_text"]"response_text"
Database Table Structure:
unanswered_questions table:
CREATE TABLE unanswered_questions (
id INT PRIMARY KEY AUTO_INCREMENT,
question TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Smart Learning Feature:
When the chatbot cannot find an answer to a question, it automatically:
- Saves the question to the
/unanswered_questionstable with the structure:id(auto-generated by database)question(the user's question)created_at(timestamp when question was asked)
- Provides a helpful response to the user
- Allows you to review and add answers later
Customization
Modifying Conversation Patterns
Edit the regex patterns in the AIChatbot class to customize conversation recognition:
self.greeting_patterns = [
r'\b(hi|hello|hey|good morning|good afternoon|good evening)\b',
# Add your own patterns here
]
Changing Database URL
Update the database URL in the AIChatbot initialization:
chatbot = AIChatbot(database_url="your_database_url_here")
Customizing Responses
Modify the response functions to change how the chatbot responds to different conversation types:
get_greeting_response()get_help_response()get_thanks_response()get_goodbye_response()
Troubleshooting
Common Issues
- Database Connection Errors: Check if your database URL is accessible and the endpoints are working
- Import Errors: Make sure all dependencies are installed:
pip install -r requirements.txt - Port Issues: If port 7860 is busy, Gradio will automatically use the next available port
Testing Database Connection
You can test your database connection by running:
import requests
response = requests.get("https://database-dhe2.onrender.com/faq?question=test")
print(response.status_code, response.json())
File Structure
βββ app.py # Main application file
βββ requirements.txt # Python dependencies
βββ README.md # This documentation
License
This project is open source and available under the MIT License.
Support
If you encounter any issues:
- Check the Hugging Face Space logs for error messages
- Test your database endpoints manually
- Verify all dependencies are correctly installed
- Check that your database is accessible from the internet
Contributing
Feel free to submit issues, feature requests, or pull requests to improve this chatbot!