Vishalpainjane's picture
Update app.py
03548cb verified
import pandas as pd
import numpy as np
import gradio as gr
import warnings
warnings.filterwarnings("ignore")
url='https://drive.google.com/file/d/1VfCaU5vFVWsSYrvKQF2x9iS6I7KoWEVH/view?usp=share_link'
url='https://drive.google.com/uc?id=' + url.split('/')[-2]
df = pd.read_csv(url)
df['cf_rating']=df['cf_rating'].astype(str)
df['cc_rating']=df['cc_rating'].astype(str)
df['cf_username']=df['cf_username'].astype(str)
df['cc_username']=df['cc_username'].astype(str)
df['ss_username']=df['ss_username'].astype(str)
only_cc = df[(df['cf_username'] == "nan")]
only_cf = df[(df['cc_username'] == "nan")]
both_cc_cf = df[(df['cf_username'] != "nan") & (df['cf_rating'] != "nan") & (df['cc_username'] != "nan") & (df['cc_rating'] != "nan")]
both_cc_cf.drop(columns=['ss_username','cf_username','cc_username'],inplace=True)
def linear_regression(X, y):
#calculating mean
mean_x = np.mean(X)
mean_y = np.mean(y)
n = 0
d = 0
for i in range(len(X)):
n += (X[i] - mean_x) * (y[i] - mean_y)
d += (X[i] - mean_x) ** 2
#calculating weights and bias
w = n/d
b = mean_y-(w * mean_x)
return (b[0], w[0])
X=both_cc_cf['cc_rating'].values.reshape(-1,1).astype(float)
Y=both_cc_cf['cf_rating'].values.reshape(-1,1).astype(float)
b_cc, w_cc = linear_regression(X,Y)
b_cf, w_cf = linear_regression(Y,X)
def predict_rating(platform, current_rating):
if platform == "CodeChef":
predicted_rating = current_rating * w_cc + b_cc
else:
predicted_rating = current_rating * w_cf + b_cf
return int(predicted_rating)
interface = gr.Interface(
fn=predict_rating,
inputs=[
gr.Radio(choices=["CodeChef", "Codeforces"], label="Choose your CP platform"),
gr.Number(label="Current Rating")
],
outputs="number",
title="CodeChef and Codeforces Rating Predictor",
)
interface.launch()