Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
pipeline = pipeline( model="ThanhHoang/Chat")
|
5 |
+
|
6 |
+
|
7 |
+
def chatbot(question):
|
8 |
+
# Làm sạch câu hỏi từ người dùng
|
9 |
+
clean_question = ntf.remove_userhandles(question)
|
10 |
+
clean_question = ntf.remove_punctuations(clean_question)
|
11 |
+
|
12 |
+
# Sử dụng mô hình Neural Network để dự đoán câu trả lời
|
13 |
+
predicted_answer_prob_nn = nn_pipe.predict_proba([clean_question])[0]
|
14 |
+
predicted_answer_nn = nn_pipe.predict([clean_question])[0]
|
15 |
+
|
16 |
+
# Check the confidence level for Neural Network
|
17 |
+
if max(predicted_answer_prob_nn) >= confidence_threshold:
|
18 |
+
return predicted_answer_nn
|
19 |
+
else:
|
20 |
+
# Sử dụng OpenAI GPT-3.5-turbo nếu Neural Network không tự tin
|
21 |
+
messages.append({"role": "user", "content": question + "trường Đại học Lạc Hồng ở phường Bửu Long, thành phố Biên Hòa, tỉnh Đồng Nai"})
|
22 |
+
chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
|
23 |
+
reply = chat.choices[0].message.content
|
24 |
+
messages.append({"role": "assistant", "content": reply})
|
25 |
+
return reply
|
26 |
+
|
27 |
+
iface = gr.Interface(
|
28 |
+
fn=chatbot,
|
29 |
+
inputs="text",
|
30 |
+
outputs="text",
|
31 |
+
title="ChatBot_LHU",
|
32 |
+
description="Nhập câu hỏi của bạn ở đây"
|
33 |
+
)
|
34 |
+
iface.launch()
|
35 |
+
|
36 |
+
# Lưu trữ mô hình
|
37 |
+
import pickle
|
38 |
+
|
39 |
+
# Lưu trữ mô hình
|
40 |
+
with open("model.pkl", "wb") as f:
|
41 |
+
pickle.dump(nn_pipe, f)
|
42 |
+
|