MAli7319's picture
Update app.py
da2c9bd
raw
history blame
2.56 kB
import gradio as gr
import pandas as pd
import numpy as np
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")
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]])
print(f"\nYour Comment: {comment}\n")
print("*"*10 + "Analysis of the Comment" + "*"*10)
print("-"*10 + f"Negativity Score: {negative_score:.2f}" + "-"*10)
print("-"*10 + f"Neutrality Score: {neutral_score:.2f}" + "-"*10)
print("-"*10 + f"Positivity Score: {positive_score:.2f}" + "-"*10)
print("-"*10 + f"Compound Score: {compound_score:.2f}" + "-"*10)
print("*"*43)
print("\nThe estimated rating this comment can give")
print("*"*20 + str(round(rate_pred[0], 2)) + "*"*20)
def take_input(model):
comment = input("Thanks for your interest and taking your time.\n"+
"Tell us about your personal experience enrolling in this course. Was it the right match for you?\n"+
"(Note: Comment should be written in English and be longer than 20 characters)\n")
if (detect(comment) != "en") or (len(comment) < 20):
print("Sorry, your comment does not meet the requirements.\n")
take_input(model)
else:
user_interaction(comment, model)
cons_tuned_svr, _, _, _, _ = sample_model(data, SVR(C=3, kernel="rbf", tol=0.001))
iface = gr.Interface(fn=take_input(cons_tuned_svr), inputs="text", outputs="text")
iface.launch()