import gradio as gr import pandas as pd import numpy as np from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt import io from PIL import Image def linear_regression(input_csv): # Load dataset from binary df = pd.read_csv(io.BytesIO(input_csv)) # Assume the first column is X and the second column is Y if len(df.columns) < 2: return None, "CSV file must contain at least two columns." X = df.iloc[:, 0].values.reshape(-1, 1) y = df.iloc[:, 1].values # Perform linear regression model = LinearRegression() model.fit(X, y) # Make predictions y_pred = model.predict(X) # Plotting plt.figure(figsize=(10, 6)) plt.scatter(X, y, color='blue') plt.plot(X, y_pred, color='red') plt.xlabel(df.columns[0]) plt.ylabel(df.columns[1]) plt.title('Linear Regression') # Save plot to a buffer and convert to PIL Image buf = io.BytesIO() plt.savefig(buf, format='png') buf.seek(0) image = Image.open(buf) # Regression info coef_info = f"Coefficient: {model.coef_[0]}\nIntercept: {model.intercept_}" return image, coef_info # Tutorial and Description Markdown description_markdown = """ ## Automatic Linear Regression Modeling Upload a CSV file with two columns. The first column will be used as X (independent variable) and the second as Y (dependent variable). ### Tutorial Follow these steps to use the application: 1. Prepare a CSV file with two columns. The first column should be your independent variable (X), and the second column your dependent variable (Y). 2. Upload the CSV file using the 'File Upload' field. 3. The application will automatically process the file, perform linear regression, and display the results and a plot. """ # Gradio interface iface = gr.Interface( fn=linear_regression, inputs=[gr.components.File(type="binary")], outputs=[ gr.components.Image(type="pil"), gr.components.Textbox(label="Regression Info") ], description=description_markdown ) # Launch the app if __name__ == "__main__": iface.launch()