| import pickle | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| def load_model(model_path): | |
| with open(model_path, 'rb') as f: | |
| model_data = pickle.load(f) | |
| return model_data | |
| def predict_answer(question, model_data): | |
| vectorizer = model_data['vectorizer'] | |
| df = model_data['data'] | |
| question_vector = vectorizer.transform([question]) | |
| similarity_scores = np.dot(df, question_vector.T).toarray().flatten() | |
| best_match_index = np.argmax(similarity_scores) | |
| return df.iloc[best_match_index]['คำตอบ'] | |
| model_data = load_model('tfidf_model.pkl') | |
| response = predict_answer("คำถามของผู้ใช้", model_data) | |
| print(response) | |