File size: 1,128 Bytes
27ac1c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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()