Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,64 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
gr.Markdown("# AIN311 Project P05 - MOOC Recommendation")
|
| 6 |
-
print("Hello")
|
| 7 |
-
iface = gr.Interface(fn=user, inputs="text", outputs="text")
|
| 8 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.svm import SVR
|
| 6 |
+
from sklearn.metrics import mean_squared_error
|
| 7 |
+
|
| 8 |
+
data = pd.read_csv("modeled_data.csv")
|
| 9 |
+
|
| 10 |
+
def sample_model(df, regressor, scale=None):
|
| 11 |
+
X = df.drop("rate",axis=1)
|
| 12 |
+
y = df["rate"]
|
| 13 |
+
|
| 14 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state=1)
|
| 15 |
+
scaled_X_train, scaled_X_test = X_train, X_test
|
| 16 |
+
if scale != None:
|
| 17 |
+
scaler = scale
|
| 18 |
+
scaled_X_train = pd.DataFrame(scaler.fit_transform(X_train), columns = X_train.columns)
|
| 19 |
+
scaled_X_test = pd.DataFrame(scaler.transform(X_test),columns = X_test.columns)
|
| 20 |
+
|
| 21 |
+
model = regressor
|
| 22 |
+
model.fit(scaled_X_train, y_train)
|
| 23 |
+
y_pred = model.predict(scaled_X_test)
|
| 24 |
+
|
| 25 |
+
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
|
| 26 |
+
|
| 27 |
+
return model, scaled_X_train, scaled_X_test, y_train, y_test
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def user_interaction(comment, model):
|
| 31 |
+
|
| 32 |
+
negative_score = analyzer.polarity_scores(comment)["neg"]
|
| 33 |
+
neutral_score = analyzer.polarity_scores(comment)["neu"]
|
| 34 |
+
positive_score = analyzer.polarity_scores(comment)["pos"]
|
| 35 |
+
compound_score = analyzer.polarity_scores(comment)["compound"]
|
| 36 |
+
rate_pred = model.predict([[negative_score, neutral_score, positive_score, compound_score]])
|
| 37 |
|
| 38 |
+
print(f"\nYour Comment: {comment}\n")
|
| 39 |
+
print("*"*10 + "Analysis of the Comment" + "*"*10)
|
| 40 |
+
print("-"*10 + f"Negativity Score: {negative_score:.2f}" + "-"*10)
|
| 41 |
+
print("-"*10 + f"Neutrality Score: {neutral_score:.2f}" + "-"*10)
|
| 42 |
+
print("-"*10 + f"Positivity Score: {positive_score:.2f}" + "-"*10)
|
| 43 |
+
print("-"*10 + f"Compound Score: {compound_score:.2f}" + "-"*10)
|
| 44 |
+
print("*"*43)
|
| 45 |
+
print("\nThe estimated rating this comment can give")
|
| 46 |
+
print("*"*20 + str(round(rate_pred[0], 2)) + "*"*20)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def take_input(model):
|
| 50 |
+
comment = input("Thanks for your interest and taking your time.\n"+
|
| 51 |
+
"Tell us about your personal experience enrolling in this course. Was it the right match for you?\n"+
|
| 52 |
+
"(Note: Comment should be written in English and be longer than 20 characters)\n")
|
| 53 |
+
if (detect(comment) != "en") or (len(comment) < 20):
|
| 54 |
+
print("Sorry, your comment does not meet the requirements.\n")
|
| 55 |
+
take_input(model)
|
| 56 |
+
else:
|
| 57 |
+
user_interaction(comment, model)
|
| 58 |
+
|
| 59 |
+
cons_tuned_svr, _, _, _, _ = sample_model(data, SVR(C=3, kernel="rbf", tol=0.001))
|
| 60 |
+
|
| 61 |
+
|
| 62 |
|
| 63 |
+
iface = gr.Interface(fn=take_input(cons_tuned_svr), inputs="text", outputs="text")
|
|
|
|
|
|
|
|
|
|
| 64 |
iface.launch()
|