Hana-12345
commited on
Upload inference.py with huggingface_hub
Browse files- inference.py +20 -0
inference.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import pickle
|
3 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
4 |
+
|
5 |
+
def load_model(model_path):
|
6 |
+
with open(model_path, 'rb') as f:
|
7 |
+
model_data = pickle.load(f)
|
8 |
+
return model_data
|
9 |
+
|
10 |
+
def predict_answer(question, model_data):
|
11 |
+
vectorizer = model_data['vectorizer']
|
12 |
+
df = model_data['data']
|
13 |
+
question_vector = vectorizer.transform([question])
|
14 |
+
similarity_scores = np.dot(df, question_vector.T).toarray().flatten()
|
15 |
+
best_match_index = np.argmax(similarity_scores)
|
16 |
+
return df.iloc[best_match_index]['คำตอบ']
|
17 |
+
|
18 |
+
model_data = load_model('tfidf_model.pkl')
|
19 |
+
response = predict_answer("คำถามของผู้ใช้", model_data)
|
20 |
+
print(response)
|