File size: 1,443 Bytes
b65581e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
from transformers import pipeline

pipeline = pipeline( model="ThanhHoang/Chat")


def chatbot(question):
    # Làm sạch câu hỏi từ người dùng
    clean_question = ntf.remove_userhandles(question)
    clean_question = ntf.remove_punctuations(clean_question)

    # Sử dụng mô hình Neural Network để dự đoán câu trả lời
    predicted_answer_prob_nn = nn_pipe.predict_proba([clean_question])[0]
    predicted_answer_nn = nn_pipe.predict([clean_question])[0]

    # Check the confidence level for Neural Network
    if max(predicted_answer_prob_nn) >= confidence_threshold:
        return predicted_answer_nn
    else:
        # Sử dụng OpenAI GPT-3.5-turbo nếu Neural Network không tự tin
        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"})
        chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
        reply = chat.choices[0].message.content
        messages.append({"role": "assistant", "content": reply})
        return reply

iface = gr.Interface(
    fn=chatbot,
    inputs="text",
    outputs="text",
    title="ChatBot_LHU",
    description="Nhập câu hỏi của bạn ở đây"
)
iface.launch()

# Lưu trữ mô hình
import pickle

# Lưu trữ mô hình
with open("model.pkl", "wb") as f:
    pickle.dump(nn_pipe, f)