File size: 3,754 Bytes
5558e45
da2c9bd
 
14637b5
78e167d
14637b5
 
78e167d
14637b5
 
da2c9bd
 
 
 
 
14637b5
da2c9bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
beba0cd
4df79e9
 
3da5b25
 
 
 
 
2ede8ce
4df79e9
da2c9bd
 
fd0291a
732a250
 
 
fd0291a
 
 
da2c9bd
 
 
3c9ea56
ab2e1ab
 
ef0c21a
ab2e1ab
14637b5
fc0ac32
 
 
14637b5
fc0ac32
ab2e1ab
4df79e9
 
 
 
 
 
fc0ac32
3c9ea56
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import gradio as gr
import pandas as pd
import numpy as np

#pip install langdetect
from langdetect import detect

#pip install vaderSentiment
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

from sklearn.model_selection import train_test_split
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error

data = pd.read_csv("modeled_data.csv")
analyzer = SentimentIntensityAnalyzer()

def sample_model(df, regressor, scale=None):
    X = df.drop("rate",axis=1)
    y = df["rate"]
    
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state=1)
    scaled_X_train, scaled_X_test = X_train, X_test
    if scale != None:
        scaler = scale
        scaled_X_train = pd.DataFrame(scaler.fit_transform(X_train), columns = X_train.columns)
        scaled_X_test = pd.DataFrame(scaler.transform(X_test),columns = X_test.columns)
    
    model = regressor
    model.fit(scaled_X_train, y_train)
    y_pred = model.predict(scaled_X_test)
    
    rmse = np.sqrt(mean_squared_error(y_test, y_pred))
    
    return model, scaled_X_train, scaled_X_test, y_train, y_test 


def user_interaction(comment, model):
    
    negative_score = analyzer.polarity_scores(comment)["neg"]
    neutral_score = analyzer.polarity_scores(comment)["neu"]
    positive_score = analyzer.polarity_scores(comment)["pos"]
    compound_score = analyzer.polarity_scores(comment)["compound"]
    rate_pred = model.predict([[negative_score, neutral_score, positive_score, compound_score]])
    
    return round(negative_score,2), round(neutral_score,2), round(positive_score,2), round(compound_score,2), round(rate_pred[0],2)
    """return (f"\nYour Comment: {comment}\n" + 
           "*"*10 + "Analysis of the Comment" + "*"*10 + "\n" +
           "-"*10 + f"Negativity Score: {negative_score:.2f}" + "-"*10 + "\n" +
           "-"*10 + f"Neutrality Score: {neutral_score:.2f}" + "-"*10 + "\n" +
           "-"*10 + f"Positivity Score: {positive_score:.2f}" + "-"*10 + "\n" +
           "-"*10 + f"Compound Score: {compound_score:.2f}" + "-"*10 + "\n" +
           "*"*43 + "\n"), ("\nThe estimated rating this comment can give" + "\n" + 
           "*"*20 + str(round(rate_pred[0], 2)) + "*"*20 + "\n")"""


def take_input(comment):
    #if (detect(comment) != "en") or (len(comment) < 20):
     #   return "Sorry, your comment does not meet the requirements.\n", "Please check your comment"
    #else:
    cons_tuned_svr, _, _, _, _ = sample_model(data, SVR(C=3, kernel="rbf", tol=0.001))
    return user_interaction(comment, cons_tuned_svr)




with gr.Blocks() as demo:
    gr.Markdown("# AIN311 Project P05 - MOOC Recommendation")
    gr.Markdown("## Generating a Rating from User Comment")
    
    with gr.Column():
        gr.Markdown("""
                    ##### Thanks for your interest and taking your time.
                    ##### Tell us about your personal experience enrolling in this course. Was it the right match for you?
                    ##### (Note: Comment should be written in English and be longer than 20 characters)
                    """)
        input_comment = gr.Textbox(placeholder="Write your comment here...")
        button = gr.Button("What is the Rating I Gave? Click me to Learn")
        gr.Markdown("#### Sentiment Scores of Your Comment")
        negscore = gr.Number(label="Negativity Score")
        neuscore = gr.Number(label="Neutrality Score")
        posscore = gr.Number(label="Positivity Score")
        compscore = gr.Number(label="Compound Score")
        rating = gr.Number(label="Generated Rating from Your Comment")        
    button.click(fn=take_input, inputs=input_comment, outputs=[negscore, neuscore, posscore, compscore, rating])

demo.launch()