Spaces:
Running
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_questions` table 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: | |
| 1. **Conversation Mode**: Recognizes common conversation patterns like greetings, help requests, and thanks | |
| 2. **Smart Database Query Mode**: For specific questions, it uses intelligent matching to find relevant answers | |
| 3. **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 | |
| 1. Clone or download this repository | |
| 2. Install the required dependencies: | |
| ```bash | |
| pip install -r requirements.txt | |
| ``` | |
| ### Running Locally | |
| ```bash | |
| python app.py | |
| ``` | |
| The chatbot will be available at `http://localhost:7860` | |
| ## Deployment to Hugging Face Spaces | |
| ### Step 1: Create a Hugging Face Account | |
| 1. Go to [Hugging Face](https://huggingface.co) and create an account | |
| 2. Verify your email address | |
| ### Step 2: Create a New Space | |
| 1. Navigate to [Create a New Space](https://huggingface.co/new-space) | |
| 2. 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" | |
| ### Step 3: Upload Files | |
| Upload these files to your Hugging Face Space: | |
| 1. `app.py` - Main application file | |
| 2. `requirements.txt` - Dependencies | |
| 3. `README.md` - This documentation (optional) | |
| ### Step 4: Configure Settings | |
| 1. In your Space settings, make sure the **SDK** is set to "Gradio" | |
| 2. The **App file** should be set to `app.py` | |
| 3. 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:** | |
| ```json | |
| { | |
| "question": "your_question_here" | |
| } | |
| ``` | |
| **POST Request for Saving Unanswered Questions:** | |
| ```json | |
| { | |
| "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:** | |
| ```sql | |
| 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: | |
| 1. Saves the question to the `/unanswered_questions` table with the structure: | |
| - `id` (auto-generated by database) | |
| - `question` (the user's question) | |
| - `created_at` (timestamp when question was asked) | |
| 2. Provides a helpful response to the user | |
| 3. Allows you to review and add answers later | |
| ## Customization | |
| ### Modifying Conversation Patterns | |
| Edit the regex patterns in the `AIChatbot` class to customize conversation recognition: | |
| ```python | |
| 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: | |
| ```python | |
| 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 | |
| 1. **Database Connection Errors**: Check if your database URL is accessible and the endpoints are working | |
| 2. **Import Errors**: Make sure all dependencies are installed: `pip install -r requirements.txt` | |
| 3. **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: | |
| ```python | |
| 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: | |
| 1. Check the Hugging Face Space logs for error messages | |
| 2. Test your database endpoints manually | |
| 3. Verify all dependencies are correctly installed | |
| 4. Check that your database is accessible from the internet | |
| ## Contributing | |
| Feel free to submit issues, feature requests, or pull requests to improve this chatbot! | |