Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,63 @@
|
|
1 |
-
import
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
|
10 |
def chatbot(question):
|
@@ -41,5 +94,6 @@ import pickle
|
|
41 |
|
42 |
# Lưu trữ mô hình
|
43 |
with open("model.pkl", "wb") as f:
|
44 |
-
pickle.dump(
|
|
|
45 |
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
# ! pip install neattext # Installing the neattext library
|
4 |
+
import neattext.functions as ntf
|
5 |
+
from sklearn.feature_extraction.text import TfidfVectorizer,CountVectorizer,TfidfTransformer
|
6 |
+
from sklearn.pipeline import Pipeline
|
7 |
+
|
8 |
+
import os
|
9 |
+
import pandas as pd
|
10 |
+
|
11 |
+
# Đường dẫn tới file Excel
|
12 |
+
excel_file_path = '/content/Data_LHU_2cot .xlsx'
|
13 |
+
|
14 |
+
# Đọc file Excel
|
15 |
+
df = pd.read_excel(excel_file_path)
|
16 |
+
|
17 |
+
# Lấy tên file Excel và thay đổi đuôi sang CSV
|
18 |
+
csv_file_name = os.path.splitext(os.path.basename(excel_file_path))[0] + '.csv'
|
19 |
+
csv_file_path = os.path.join(os.path.dirname(excel_file_path), csv_file_name)
|
20 |
+
|
21 |
+
# Ghi dữ liệu ra file CSV
|
22 |
+
df.to_csv(csv_file_path, index=False, encoding='utf-8-sig') # utf-8-sig để giữ nguyên dấu
|
23 |
+
|
24 |
+
print(f'Chuyển đổi thành công! File CSV được lưu tại: {csv_file_path}')
|
25 |
+
# Kiểm tra và loại bỏ NaN từ cột 'Câu hỏi' và 'Câu trả lời'
|
26 |
+
sn['Câu hỏi'].fillna('', inplace=True)
|
27 |
+
sn['Câu trả lời'].fillna('', inplace=True)
|
28 |
+
|
29 |
+
# Làm sạch dữ liệu
|
30 |
+
sn['Câu hỏi_Làm sạch'] = sn['Câu hỏi'].apply(lambda x: ntf.remove_userhandles(x) if isinstance(x, str) else x)
|
31 |
+
sn['Câu hỏi_Làm sạch'] = sn['Câu hỏi_Làm sạch'].apply(lambda x: ntf.remove_punctuations(x) if isinstance(x, str) else x)
|
32 |
+
|
33 |
+
sn['Câu trả lời_Làm sạch'] = sn['Câu trả lời'].apply(lambda x: ntf.remove_userhandles(x) if isinstance(x, str) else x)
|
34 |
+
sn['Câu trả lời_Làm sạch'] = sn['Câu trả lời_Làm sạch'].apply(lambda x: ntf.remove_punctuations(x) if isinstance(x, str) else x)
|
35 |
|
36 |
+
from sklearn.neural_network import MLPClassifier
|
37 |
+
from sklearn.pipeline import Pipeline
|
38 |
+
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
|
39 |
+
|
40 |
+
# Khởi tạo mô hình Neural Network
|
41 |
+
nn_pipe = Pipeline([
|
42 |
+
('vect', CountVectorizer()),
|
43 |
+
('tfidf', TfidfTransformer()),
|
44 |
+
('nn', MLPClassifier(hidden_layer_sizes=(100,), max_iter=300, activation='relu', solver='adam'))
|
45 |
+
])
|
46 |
+
|
47 |
+
# Huấn luyện mô hình Neural Network
|
48 |
+
nn_pipe.fit(sn['Câu hỏi'], sn['Câu trả lời'])
|
49 |
+
|
50 |
+
import openai
|
51 |
+
openai.api_key = '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'
|
52 |
+
|
53 |
+
# Danh sách tin nhắn cho OpenAI
|
54 |
+
messages = [
|
55 |
+
{"role": "system", "content": "You are a kind helpful assistant."},
|
56 |
+
]
|
57 |
+
|
58 |
+
confidence_threshold = 0.5
|
59 |
+
|
60 |
+
import gradio as gr
|
61 |
|
62 |
|
63 |
def chatbot(question):
|
|
|
94 |
|
95 |
# Lưu trữ mô hình
|
96 |
with open("model.pkl", "wb") as f:
|
97 |
+
pickle.dump(chatbot, f)
|
98 |
+
|
99 |
|