import numpy as np
import gradio as gr
import seaborn as sns
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error

df=sns.load_dataset('tips')

from sklearn.preprocessing import OneHotEncoder
ohe=OneHotEncoder(drop='first', sparse=False)
df_encoded=ohe.fit_transform(df[['sex','smoker','day','time']])
df_encoded=pd.DataFrame(df_encoded,columns=ohe.get_feature_names_out(['sex','smoker','day','time']))
result_df=pd.concat([df,df_encoded],axis=1)
result_df=result_df.drop(columns=['sex','smoker','day','time'],axis=1)
X=result_df.drop(['tip'],axis=1)
y=result_df['tip']

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(X,y,test_size=0.1, random_state=42)

from sklearn.preprocessing import StandardScaler
ss=StandardScaler()
x_train[['total_bill','size','sex_Male','smoker_Yes','day_Sat','day_Sun','day_Thur','time_Lunch']]=ss.fit_transform(x_train[['total_bill','size','sex_Male','smoker_Yes','day_Sat','day_Sun','day_Thur','time_Lunch']])
x_test[['total_bill','size','sex_Male','smoker_Yes','day_Sat','day_Sun','day_Thur','time_Lunch']]=ss.transform(x_test[['total_bill','size','sex_Male','smoker_Yes','day_Sat','day_Sun','day_Thur','time_Lunch']])
from sklearn.linear_model import LinearRegression
lr=LinearRegression()
lr.fit(x_train,y_train)
y_pred=lr.predict(x_test)
import numpy as np
from sklearn.metrics import mean_squared_error

# Import the necessary libraries and prepare the data and model as you did before

# Define the prediction function
def predict_total_tip(total_bill, size, sex, smoker, day, time):
    # Map user-friendly inputs to 1s and 0s
    sex_Male = 1 if sex == '1' else 0
    smoker_Yes = 1 if smoker == '1' else 0
    day_Sat = 1 if day == '1' else 0
    day_Sun = 1 if day == '2' else 0
    day_Thur = 1 if day == '3' else 0
    time_Lunch = 1 if time == '1' else 0
    
    # Create a feature array
    features = [float(total_bill), int(size), sex_Male, smoker_Yes, day_Sat, day_Sun, day_Thur, time_Lunch]
    
    # Standardize the features using the same StandardScaler used in the code
    features = ss.transform([features])
    
    # Predict the total_bill using the Linear Regression model
    tip = lr.predict(features)[0]
    return tip

# Define the Gradio interface
iface = gr.Interface(
    fn=predict_total_tip,
    inputs=[
        gr.Number(label="total_bill($)"),
        gr.Number(label="Size(number of members)"),
        gr.Radio(label="Sex", choices=["Female (0)", "Male (1)"]),
        gr.Radio(label="Smoker", choices=["No (0)", "Yes (1)"]),
        gr.Radio(label="Day", choices=["Sat (1)", "Sun (2)", "Thur (3)"]),
        gr.Radio(label="Time", choices=["Dinner (0)", "Lunch (1)"])
    ],
    outputs="text",
    title="Tip Prediction",
    description="Predict the tip using a Linear Regression model.",
    #theme=gr.themes.Glass(primary_hue="green", secondary_hue="red"),
)


# Launch the Gradio interface
#iface.launch(share=True)
iface.launch(share=True)