Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
from gensim.models import Word2Vec
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load the models
|
| 7 |
+
classifier = joblib.load("random_forest_model.pkl")
|
| 8 |
+
word2vec_model = Word2Vec.load("word2vec_model.bin")
|
| 9 |
+
label_encoder = joblib.load("label_encoder.pkl")
|
| 10 |
+
|
| 11 |
+
def predict_comment(comment):
|
| 12 |
+
tokenized_comment = comment.split()
|
| 13 |
+
comment_vector = get_average_word2vec(tokenized_comment, word2vec_model, 100)
|
| 14 |
+
comment_vector = comment_vector.reshape(1, -1)
|
| 15 |
+
prediction = classifier.predict(comment_vector)
|
| 16 |
+
return "Based on Experience" if label_encoder.inverse_transform(prediction)[0] == 1 else "Not Based on Experience"
|
| 17 |
+
|
| 18 |
+
def get_average_word2vec(comment, model, num_features):
|
| 19 |
+
feature_vec = np.zeros((num_features,), dtype="float32")
|
| 20 |
+
n_words = 0
|
| 21 |
+
for word in comment:
|
| 22 |
+
if word in model.wv.key_to_index:
|
| 23 |
+
n_words += 1
|
| 24 |
+
feature_vec = np.add(feature_vec, model.wv[word])
|
| 25 |
+
if n_words > 0:
|
| 26 |
+
feature_vec = np.divide(feature_vec, n_words)
|
| 27 |
+
return feature_vec
|
| 28 |
+
|
| 29 |
+
# Gradio interface
|
| 30 |
+
iface = gr.Interface(fn=predict_comment, inputs="text", outputs="text")
|
| 31 |
+
iface.launch()
|