Chat / app.py
ThanhHoang's picture
Update app.py
58b0bba verified
raw
history blame
1.55 kB
import gradio as gr
from transformers import pipeline
# Load model directly
# from transformers import AutoModel
# model = AutoModel.from_pretrained("ThanhHoang/Chat")
# pipeline = pipeline( model=model)
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)