Spaces:
Paused
Paused
baobuiquang
commited on
Commit
•
f249c8e
1
Parent(s):
00c9a21
Delete README.md
Browse files
README.md
DELETED
@@ -1,181 +0,0 @@
|
|
1 |
-
# Natural Language Q&A Chatbot
|
2 |
-
|
3 |
-
## Problem
|
4 |
-
|
5 |
-
Input:
|
6 |
-
* `data` - Example: `data/sample.xlsx`
|
7 |
-
* `question` - Example: "Tổng số hồ sơ chứng thực chữ ký vào ngày 12 tháng 1 năm 2024 là bao nhiêu?"
|
8 |
-
|
9 |
-
Expected output:
|
10 |
-
* `answer`: Example: "165"
|
11 |
-
|
12 |
-
## Solution Approach
|
13 |
-
|
14 |
-
### Preprocessing `data`:
|
15 |
-
|
16 |
-
* Raw Data (`.XLSX`)
|
17 |
-
* ↳ Raw Dataframe (`Pandas DF`)
|
18 |
-
* ↳ Preprocessed Dataframe (`Pandas DF`)
|
19 |
-
|
20 |
-
|
21 |
-
### Feature Extracting `data` and `question`:
|
22 |
-
|
23 |
-
* Preprocessed Dataframe Data / Question (`String`)
|
24 |
-
* ↳ Embedding (`PyTorch Tensor`)
|
25 |
-
|
26 |
-
#### Model:
|
27 |
-
* Stable Model: [HF/XLM-ROBERTA-ME5-BASE](https://huggingface.co/baobuiquang/XLM-ROBERTA-ME5-BASE) (License: [MIT License](https://choosealicense.com/licenses/mit/))
|
28 |
-
* Forked from: [HF/multilingual-e5-base](https://huggingface.co/intfloat/multilingual-e5-base) (License: [MIT License](https://choosealicense.com/licenses/mit/))
|
29 |
-
* Initialized from [xlm-roberta-base](https://huggingface.co/xlm-roberta-base) (License: [MIT License](https://choosealicense.com/licenses/mit/))
|
30 |
-
|
31 |
-
|
32 |
-
### Feature Map Down Sampling Method: [Mean Pooling](https://paperswithcode.com/method/average-pooling)
|
33 |
-
|
34 |
-
* Reduce computationally expensive -> Fast chatbot (Speed)
|
35 |
-
* Prevent overfitting -> Better answer (Accuracy)
|
36 |
-
|
37 |
-
### Measurement: [Cosine Similarity](https://en.wikipedia.org/wiki/Cosine_similarity)
|
38 |
-
* Input:
|
39 |
-
* Embedding `a` (`PyTorch Tensor`)
|
40 |
-
* Embedding `b` (`PyTorch Tensor`)
|
41 |
-
* Output:
|
42 |
-
* Cosine Similarity: The cosine of the angle between the 2 non-zero vectors `a` and `b` in space.
|
43 |
-
```
|
44 |
-
cos_sim = np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
|
45 |
-
```
|
46 |
-
|
47 |
-
### Interactive UI
|
48 |
-
|
49 |
-
Chatbot's Web UI is currently built with [gradio](https://github.com/gradio-app/gradio) (License: [Apache-2.0 License](https://choosealicense.com/licenses/apache-2.0/)).
|
50 |
-
|
51 |
-
## Example and Rough Explanation
|
52 |
-
|
53 |
-
Sample data: [sample.xlsx](https://github.com/baobuiquang/nlqna-chatbot/blob/main/data/sample.xlsx)
|
54 |
-
|
55 |
-
### Step 1. Input:
|
56 |
-
* `question` = "Tổng số hồ sơ chứng thực chữ ký vào ngày 12 tháng 1 năm 2024 là bao nhiêu?"
|
57 |
-
* `data` = `data/sample.xlsx`
|
58 |
-
|
59 |
-
| | | | | | |
|
60 |
-
| :-----------------------------------------------------: | :---: | :------------: | :------------: | :------------: | :---: |
|
61 |
-
| | ... | **11/01/2024** | **12/01/2024** | **13/01/2024** | ... |
|
62 |
-
| ... | | | | | |
|
63 |
-
| **Tổng số HS chứng thực hợp đồng, giao dịch** | | 156 | 161 | 177 | |
|
64 |
-
| **Tổng số HS chứng thực chữ ký** | | 159 | 165 | 182 | |
|
65 |
-
| **Tổng số HS chứng thực việc sửa đổi, bổ sung, hủy bỏ** | | 162 | 169 | 187 | |
|
66 |
-
| ... | | | | | |
|
67 |
-
|
68 |
-
### Step 2. Feature Extraction:
|
69 |
-
|
70 |
-
* `question` -> `question_embedding` (`PyTorch Tensor`)
|
71 |
-
* `data` -> `data_embeddings` (Map of `PyTorch Tensors`)
|
72 |
-
|
73 |
-
| | | | | | |
|
74 |
-
| :-----------------: | :---: | :-----------------: | :-----------------: | :-----------------: | :---: |
|
75 |
-
| | ... | ***\<PT Tensor\>*** | ***\<PT Tensor\>*** | ***\<PT Tensor\>*** | ... |
|
76 |
-
| ... | | | | | |
|
77 |
-
| ***\<PT Tensor\>*** | | 156 | 161 | 177 | |
|
78 |
-
| ***\<PT Tensor\>*** | | 159 | 165 | 182 | |
|
79 |
-
| ***\<PT Tensor\>*** | | 162 | 169 | 187 | |
|
80 |
-
| ... | | | | | |
|
81 |
-
|
82 |
-
### Step 3. Measurement Calculation:
|
83 |
-
|
84 |
-
Calculate the Cosine Similarity between `question_embedding` and `data_embeddings`.
|
85 |
-
|
86 |
-
| | | | | | |
|
87 |
-
| :-------------: | :---: | :-------------: | :-------------: | :-------------: | :---: |
|
88 |
-
| | ... | ***{cos_sim}*** | ***{cos_sim}*** | ***{cos_sim}*** | ... |
|
89 |
-
| ... | | | | | |
|
90 |
-
| ***{cos_sim}*** | | 156 | 161 | 177 | |
|
91 |
-
| ***{cos_sim}*** | | 159 | 165 | 182 | |
|
92 |
-
| ***{cos_sim}*** | | 162 | 169 | 187 | |
|
93 |
-
| ... | | | | | |
|
94 |
-
|
95 |
-
### Step 4. Output:
|
96 |
-
|
97 |
-
Find the highest Cosine Similarity in horizontal and vertical axis to determine the cell for final answer.
|
98 |
-
|
99 |
-
| | | | | | |
|
100 |
-
| :----------------------------: | :---: | :---------: | :----------------------------: | :---------: | :---: |
|
101 |
-
| | ... | *{cos_sim}* | ***{highest_cos_sim_x_axis}*** | *{cos_sim}* | ... |
|
102 |
-
| ... | | | | | |
|
103 |
-
| *{cos_sim}* | | 156 | 161 | 177 | |
|
104 |
-
| ***{highest_cos_sim_y_axis}*** | | 159 | ***165*** | 182 | |
|
105 |
-
| *{cos_sim}* | | 162 | 169 | 187 | |
|
106 |
-
| ... | | | | | |
|
107 |
-
|
108 |
-
Output the answer (cell value): "165"
|
109 |
-
|
110 |
-
## Demo
|
111 |
-
|
112 |
-
https://github.com/baobuiquang/nlqna-chatbot/assets/60503568/57621579-6a58-4638-9644-b4e482ac975e
|
113 |
-
|
114 |
-
## Instructions (Recommended workflow)
|
115 |
-
|
116 |
-
### Installation
|
117 |
-
|
118 |
-
Prerequisites:
|
119 |
-
* [Python 3](https://www.python.org/downloads/)
|
120 |
-
* [Git](https://git-scm.com/downloads)
|
121 |
-
|
122 |
-
Clone [this repository](https://github.com/baobuiquang/nlqna-chatbot):
|
123 |
-
```
|
124 |
-
git clone https://github.com/baobuiquang/nlqna-chatbot.git
|
125 |
-
cd nlqna-chatbot
|
126 |
-
```
|
127 |
-
|
128 |
-
Create virtual environment:
|
129 |
-
```
|
130 |
-
python -m venv venv
|
131 |
-
```
|
132 |
-
|
133 |
-
Activate virtual environment:
|
134 |
-
```
|
135 |
-
venv\Scripts\activate
|
136 |
-
```
|
137 |
-
|
138 |
-
Upgrade `pip` command:
|
139 |
-
```
|
140 |
-
python.exe -m pip install --upgrade pip
|
141 |
-
```
|
142 |
-
|
143 |
-
Install [required packages/libraries](https://github.com/baobuiquang/nlqna-chatbot/blob/main/requirements.txt):
|
144 |
-
```
|
145 |
-
pip install -r requirements.txt
|
146 |
-
```
|
147 |
-
|
148 |
-
Deactivate virtual environment:
|
149 |
-
```
|
150 |
-
deactivate
|
151 |
-
```
|
152 |
-
|
153 |
-
### Start chatbot
|
154 |
-
|
155 |
-
Activate virtual environment:
|
156 |
-
```
|
157 |
-
venv\Scripts\activate
|
158 |
-
```
|
159 |
-
|
160 |
-
Run chatbot app:
|
161 |
-
```
|
162 |
-
python app.py
|
163 |
-
```
|
164 |
-
|
165 |
-
Wait until the terminal print something like this:
|
166 |
-
```
|
167 |
-
...\nlqna-chatbot> python app.py
|
168 |
-
Running on local URL: http://127.0.0.1:7860
|
169 |
-
To create a public link, set `share=True` in `launch()`.
|
170 |
-
```
|
171 |
-
|
172 |
-
Now chatbot can be accessed from [http://127.0.0.1:7860](http://127.0.0.1:7860).
|
173 |
-
|
174 |
-
### Stop chatbot
|
175 |
-
|
176 |
-
Press `Ctrl + C` in the terminal to close the chatbot server.
|
177 |
-
|
178 |
-
Deactivate virtual environment:
|
179 |
-
```
|
180 |
-
deactivate
|
181 |
-
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|