MrVJavlon4002's picture
Create main.py
27ac1c9 verified
import gradio as gr
import joblib
from gensim.models import Word2Vec
import numpy as np
# Load the models
classifier = joblib.load("random_forest_model.pkl")
word2vec_model = Word2Vec.load("word2vec_model.bin")
label_encoder = joblib.load("label_encoder.pkl")
def predict_comment(comment):
tokenized_comment = comment.split()
comment_vector = get_average_word2vec(tokenized_comment, word2vec_model, 100)
comment_vector = comment_vector.reshape(1, -1)
prediction = classifier.predict(comment_vector)
return "Based on Experience" if label_encoder.inverse_transform(prediction)[0] == 1 else "Not Based on Experience"
def get_average_word2vec(comment, model, num_features):
feature_vec = np.zeros((num_features,), dtype="float32")
n_words = 0
for word in comment:
if word in model.wv.key_to_index:
n_words += 1
feature_vec = np.add(feature_vec, model.wv[word])
if n_words > 0:
feature_vec = np.divide(feature_vec, n_words)
return feature_vec
# Gradio interface
iface = gr.Interface(fn=predict_comment, inputs="text", outputs="text")
iface.launch()