graphmaker / app.py
rioanggara's picture
changes
dc71877
raw
history blame
1.6 kB
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, x_column, y_column):
# Load dataset from binary
df = pd.read_csv(io.BytesIO(input_csv))
# Prepare data for regression
X = df[[x_column]].values
y = df[y_column].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(x_column)
plt.ylabel(y_column)
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
# Gradio interface
iface = gr.Interface(
fn=linear_regression,
inputs=[
gr.components.File(type="binary"),
gr.components.Textbox(label="X Column Name"),
gr.components.Textbox(label="Y Column Name"),
],
outputs=[
gr.components.Image(type="pil"),
gr.components.Textbox(label="Regression Info")
],
title="Automatic Linear Regression Modeling",
description="Upload a CSV file and specify the columns for performing linear regression."
)
# Launch the app
if __name__ == "__main__":
iface.launch()