bhupen@gridflowAI
new model
5c3c178
from io import StringIO
import gradio as gr
import pandas as pd
import xgboost as xgb
# Add this import
def preprocess_data(df):
# Assuming the model expects the input without the first 50 columns
processed_data = df.drop(df.columns[0:50], axis=1)
# Assuming you want the last column as the target.
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')
# Check if the file is non-empty and has recognizable data
try:
# Load the csv content into a DataFrame
data = process_csv_text(input_csv)
if data is None:
return "Uploaded file is empty or not recognized as a valid CSV."
# Continue with the rest of the processing
processed_data = preprocess_data(data)
predictions = loaded_model.predict(processed_data[0]) # Access the X part
data['Predictions'] = predictions
data['Actual'] = processed_data[1] # Access the y part
return data.to_html()
except Exception as e:
return str(e)
# Define the Gradio interface
iface = gr.Interface(
fn=predict_interface,
inputs="file",
outputs="html",
live=True
)
iface.launch(server_name="0.0.0.0", server_port=7860)