|
from io import StringIO
|
|
import gradio as gr
|
|
import pandas as pd
|
|
import xgboost as xgb
|
|
|
|
|
|
def preprocess_data(df):
|
|
|
|
|
|
|
|
processed_data = df.drop(df.columns[0:50], axis=1)
|
|
|
|
|
|
X = processed_data.drop(processed_data.columns[-1], axis=1).values
|
|
y = processed_data[processed_data.columns[-1]].values
|
|
|
|
|
|
return (X, y)
|
|
|
|
|
|
|
|
def process_csv_text(temp_file):
|
|
if isinstance(temp_file, str):
|
|
df = pd.read_csv(StringIO(temp_file))
|
|
else:
|
|
df = pd.read_csv(temp_file.name)
|
|
print(df)
|
|
return df
|
|
|
|
|
|
def predict_interface(input_csv):
|
|
print(type(input_csv))
|
|
loaded_model = xgb.XGBRegressor()
|
|
loaded_model.load_model('xgboost_model.json')
|
|
|
|
|
|
try:
|
|
|
|
data = process_csv_text(input_csv)
|
|
if data is None:
|
|
return "Uploaded file is empty or not recognized as a valid CSV."
|
|
|
|
|
|
processed_data = preprocess_data(data)
|
|
predictions = loaded_model.predict(processed_data[0])
|
|
data['Predictions'] = predictions
|
|
data['Actual'] = processed_data[1]
|
|
return data.to_html()
|
|
except Exception as e:
|
|
return str(e)
|
|
|
|
|
|
|
|
iface = gr.Interface(
|
|
fn=predict_interface,
|
|
inputs="file",
|
|
outputs="html",
|
|
live=True
|
|
)
|
|
|
|
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|