File size: 1,495 Bytes
1014ac5
5bfd5e8
 
 
1014ac5
5bfd5e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1014ac5
5bfd5e8
 
4548114
 
 
5bfd5e8
 
4548114
 
5bfd5e8
 
 
1014ac5
 
5bfd5e8
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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

def linear_regression(input_csv, x_column, y_column):
    # Load dataset
    df = pd.read_csv(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
    buf = io.BytesIO()
    plt.savefig(buf, format='png')
    buf.seek(0)

    # Regression info
    coef_info = f"Coefficient: {model.coef_[0]}\nIntercept: {model.intercept_}"

    return buf, coef_info

# Gradio interface
iface = gr.Interface(
    fn=linear_regression,
    inputs=[
        gr.components.File(type="csv"),
        gr.components.Textbox(label="X Column Name"),
        gr.components.Textbox(label="Y Column Name"),
    ],
    outputs=[
        gr.components.Image(type="plot"),
        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()