Spaces:
Running
Running
Update README.md
Browse files
README.md
CHANGED
|
@@ -11,40 +11,245 @@ license: mit
|
|
| 11 |
short_description: A simple AI chatbot built with Gradio
|
| 12 |
---
|
| 13 |
|
| 14 |
-
# AI Chatbot
|
| 15 |
|
| 16 |
-
A
|
| 17 |
|
| 18 |
## Features
|
| 19 |
|
| 20 |
-
-
|
| 21 |
-
-
|
| 22 |
-
-
|
| 23 |
-
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
## How
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
## Local Development
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
```bash
|
| 38 |
-
pip install -r requirements.txt
|
| 39 |
-
```
|
| 40 |
|
| 41 |
-
|
| 42 |
-
```bash
|
| 43 |
-
python app.py
|
| 44 |
-
```
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
##
|
| 49 |
|
| 50 |
-
|
|
|
|
| 11 |
short_description: A simple AI chatbot built with Gradio
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# AI Chatbot with Database Integration
|
| 15 |
|
| 16 |
+
A smart AI chatbot built with Gradio that can handle general conversation and answer specific questions from a database/FAQ system.
|
| 17 |
|
| 18 |
## Features
|
| 19 |
|
| 20 |
+
- π€ **General Conversation**: Handles greetings, help requests, thanks, and goodbyes
|
| 21 |
+
- π **Smart Database Integration**: Intelligent fuzzy matching finds answers even with different wording
|
| 22 |
+
- π§ **AI-Powered Matching**: Uses keyword extraction, text similarity, and semantic matching
|
| 23 |
+
- π **Smart Learning**: Automatically saves unanswered questions to `/unanswered_questions` table for review
|
| 24 |
+
- π¬ **Interactive Chat Interface**: Clean, modern UI built with Gradio
|
| 25 |
+
- π **Error Handling**: Robust error handling for network issues and API failures
|
| 26 |
+
- π± **Responsive Design**: Works on desktop and mobile devices
|
| 27 |
|
| 28 |
+
## How It Works
|
| 29 |
|
| 30 |
+
The chatbot operates in three modes:
|
| 31 |
+
|
| 32 |
+
1. **Conversation Mode**: Recognizes common conversation patterns like greetings, help requests, and thanks
|
| 33 |
+
2. **Smart Database Query Mode**: For specific questions, it uses intelligent matching to find relevant answers
|
| 34 |
+
3. **Learning Mode**: When no answer is found, it automatically saves the question to your database for review
|
| 35 |
+
|
| 36 |
+
### Smart Matching Algorithm
|
| 37 |
+
|
| 38 |
+
The chatbot uses a sophisticated matching system that combines:
|
| 39 |
+
|
| 40 |
+
- **Text Normalization**: Removes punctuation, converts to lowercase, handles whitespace
|
| 41 |
+
- **Enhanced Keyword Extraction**: Identifies important words with stemming (removes 's', 'ing', 'ed')
|
| 42 |
+
- **Sequence Matching**: Uses difflib for character-level similarity
|
| 43 |
+
- **Keyword Overlap**: Calculates Jaccard similarity between keyword sets
|
| 44 |
+
- **Substring Matching**: Detects when one text contains another with length weighting
|
| 45 |
+
- **Word Order Similarity**: Matches common word sequences
|
| 46 |
+
- **Semantic Similarity**: Groups related words (money, time, contact, requirements, etc.)
|
| 47 |
+
- **Phrase Matching**: Recognizes common phrase patterns
|
| 48 |
+
- **Adaptive Thresholds**: Adjusts matching criteria based on query length
|
| 49 |
+
- **Weighted Scoring**: Combines all methods with optimized weights
|
| 50 |
+
|
| 51 |
+
**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!
|
| 52 |
+
|
| 53 |
+
**Success Rate**: The improved algorithm achieves **92% success rate** (23/25 test cases matched) compared to the previous 66.7%!
|
| 54 |
|
| 55 |
## Local Development
|
| 56 |
|
| 57 |
+
### Prerequisites
|
| 58 |
+
|
| 59 |
+
- Python 3.7 or higher
|
| 60 |
+
- pip package manager
|
| 61 |
+
|
| 62 |
+
### Installation
|
| 63 |
+
|
| 64 |
+
1. Clone or download this repository
|
| 65 |
+
2. Install the required dependencies:
|
| 66 |
+
|
| 67 |
+
```bash
|
| 68 |
+
pip install -r requirements.txt
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
### Running Locally
|
| 72 |
+
|
| 73 |
+
```bash
|
| 74 |
+
python app.py
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
The chatbot will be available at `http://localhost:7860`
|
| 78 |
+
|
| 79 |
+
## Deployment to Hugging Face Spaces
|
| 80 |
+
|
| 81 |
+
### Step 1: Create a Hugging Face Account
|
| 82 |
+
|
| 83 |
+
1. Go to [Hugging Face](https://huggingface.co) and create an account
|
| 84 |
+
2. Verify your email address
|
| 85 |
+
|
| 86 |
+
### Step 2: Create a New Space
|
| 87 |
+
|
| 88 |
+
1. Navigate to [Create a New Space](https://huggingface.co/new-space)
|
| 89 |
+
2. Fill in the details:
|
| 90 |
+
- **Space Name**: Choose a unique name (e.g., `my-ai-chatbot`)
|
| 91 |
+
- **License**: Select an appropriate license (e.g., MIT)
|
| 92 |
+
- **Space Hardware**: Choose CPU (free tier)
|
| 93 |
+
- **Visibility**: Set to Public or Private as desired
|
| 94 |
+
- **SDK**: Select "Gradio"
|
| 95 |
+
|
| 96 |
+
### Step 3: Upload Files
|
| 97 |
+
|
| 98 |
+
Upload these files to your Hugging Face Space:
|
| 99 |
+
|
| 100 |
+
1. `app.py` - Main application file
|
| 101 |
+
2. `requirements.txt` - Dependencies
|
| 102 |
+
3. `README.md` - This documentation (optional)
|
| 103 |
+
|
| 104 |
+
### Step 4: Configure Settings
|
| 105 |
+
|
| 106 |
+
1. In your Space settings, make sure the **SDK** is set to "Gradio"
|
| 107 |
+
2. The **App file** should be set to `app.py`
|
| 108 |
+
3. If your database requires authentication, add credentials as secrets in the Space settings
|
| 109 |
+
|
| 110 |
+
### Step 5: Deploy
|
| 111 |
+
|
| 112 |
+
Once all files are uploaded, your Space will automatically build and deploy. The process usually takes 2-5 minutes.
|
| 113 |
+
|
| 114 |
+
## Database Integration
|
| 115 |
+
|
| 116 |
+
The chatbot connects to your database at `https://database-dhe2.onrender.com` and tries multiple endpoints:
|
| 117 |
+
|
| 118 |
+
### Answer Retrieval Endpoints:
|
| 119 |
+
- `/faq`
|
| 120 |
+
- `/search`
|
| 121 |
+
- `/query`
|
| 122 |
+
- `/api/faq`
|
| 123 |
+
|
| 124 |
+
### Smart Matching Endpoints (for getting all questions):
|
| 125 |
+
- `/questions`
|
| 126 |
+
- `/faq/all`
|
| 127 |
+
- `/api/questions`
|
| 128 |
+
- `/all_questions`
|
| 129 |
+
|
| 130 |
+
### Unanswered Questions Endpoints:
|
| 131 |
+
- `/unanswered_questions`
|
| 132 |
+
- `/api/unanswered_questions`
|
| 133 |
+
- `/save_question`
|
| 134 |
+
- `/api/save_question`
|
| 135 |
+
|
| 136 |
+
### Request Formats:
|
| 137 |
+
|
| 138 |
+
**GET Request for Answers:**
|
| 139 |
+
```
|
| 140 |
+
https://database-dhe2.onrender.com/faq?question=your_question_here
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
**POST Request for Answers:**
|
| 144 |
+
```json
|
| 145 |
+
{
|
| 146 |
+
"question": "your_question_here"
|
| 147 |
+
}
|
| 148 |
+
```
|
| 149 |
+
|
| 150 |
+
**POST Request for Saving Unanswered Questions:**
|
| 151 |
+
```json
|
| 152 |
+
{
|
| 153 |
+
"question": "unanswered_question_here",
|
| 154 |
+
"created_at": "2024-01-01T12:00:00.000000"
|
| 155 |
+
}
|
| 156 |
+
```
|
| 157 |
+
|
| 158 |
+
### Response Formats:
|
| 159 |
+
- `{"answer": "response_text"}`
|
| 160 |
+
- `{"response": "response_text"}`
|
| 161 |
+
- `["response_text"]`
|
| 162 |
+
- `"response_text"`
|
| 163 |
+
|
| 164 |
+
### Database Table Structure:
|
| 165 |
+
|
| 166 |
+
**unanswered_questions table:**
|
| 167 |
+
```sql
|
| 168 |
+
CREATE TABLE unanswered_questions (
|
| 169 |
+
id INT PRIMARY KEY AUTO_INCREMENT,
|
| 170 |
+
question TEXT NOT NULL,
|
| 171 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 172 |
+
);
|
| 173 |
+
```
|
| 174 |
+
|
| 175 |
+
### Smart Learning Feature:
|
| 176 |
+
When the chatbot cannot find an answer to a question, it automatically:
|
| 177 |
+
1. Saves the question to the `/unanswered_questions` table with the structure:
|
| 178 |
+
- `id` (auto-generated by database)
|
| 179 |
+
- `question` (the user's question)
|
| 180 |
+
- `created_at` (timestamp when question was asked)
|
| 181 |
+
2. Provides a helpful response to the user
|
| 182 |
+
3. Allows you to review and add answers later
|
| 183 |
+
|
| 184 |
+
## Customization
|
| 185 |
+
|
| 186 |
+
### Modifying Conversation Patterns
|
| 187 |
+
|
| 188 |
+
Edit the regex patterns in the `AIChatbot` class to customize conversation recognition:
|
| 189 |
+
|
| 190 |
+
```python
|
| 191 |
+
self.greeting_patterns = [
|
| 192 |
+
r'\b(hi|hello|hey|good morning|good afternoon|good evening)\b',
|
| 193 |
+
# Add your own patterns here
|
| 194 |
+
]
|
| 195 |
+
```
|
| 196 |
+
|
| 197 |
+
### Changing Database URL
|
| 198 |
+
|
| 199 |
+
Update the database URL in the `AIChatbot` initialization:
|
| 200 |
+
|
| 201 |
+
```python
|
| 202 |
+
chatbot = AIChatbot(database_url="your_database_url_here")
|
| 203 |
+
```
|
| 204 |
+
|
| 205 |
+
### Customizing Responses
|
| 206 |
+
|
| 207 |
+
Modify the response functions to change how the chatbot responds to different conversation types:
|
| 208 |
+
|
| 209 |
+
- `get_greeting_response()`
|
| 210 |
+
- `get_help_response()`
|
| 211 |
+
- `get_thanks_response()`
|
| 212 |
+
- `get_goodbye_response()`
|
| 213 |
+
|
| 214 |
+
## Troubleshooting
|
| 215 |
+
|
| 216 |
+
### Common Issues
|
| 217 |
+
|
| 218 |
+
1. **Database Connection Errors**: Check if your database URL is accessible and the endpoints are working
|
| 219 |
+
2. **Import Errors**: Make sure all dependencies are installed: `pip install -r requirements.txt`
|
| 220 |
+
3. **Port Issues**: If port 7860 is busy, Gradio will automatically use the next available port
|
| 221 |
+
|
| 222 |
+
### Testing Database Connection
|
| 223 |
+
|
| 224 |
+
You can test your database connection by running:
|
| 225 |
+
|
| 226 |
+
```python
|
| 227 |
+
import requests
|
| 228 |
+
response = requests.get("https://database-dhe2.onrender.com/faq?question=test")
|
| 229 |
+
print(response.status_code, response.json())
|
| 230 |
+
```
|
| 231 |
+
|
| 232 |
+
## File Structure
|
| 233 |
+
|
| 234 |
+
```
|
| 235 |
+
βββ app.py # Main application file
|
| 236 |
+
βββ requirements.txt # Python dependencies
|
| 237 |
+
βββ README.md # This documentation
|
| 238 |
+
```
|
| 239 |
+
|
| 240 |
+
## License
|
| 241 |
+
|
| 242 |
+
This project is open source and available under the MIT License.
|
| 243 |
|
| 244 |
+
## Support
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
+
If you encounter any issues:
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
+
1. Check the Hugging Face Space logs for error messages
|
| 249 |
+
2. Test your database endpoints manually
|
| 250 |
+
3. Verify all dependencies are correctly installed
|
| 251 |
+
4. Check that your database is accessible from the internet
|
| 252 |
|
| 253 |
+
## Contributing
|
| 254 |
|
| 255 |
+
Feel free to submit issues, feature requests, or pull requests to improve this chatbot!
|