File size: 2,119 Bytes
1014ac5
8705e71
5bfd5e8
 
1014ac5
5bfd5e8
dc71877
5bfd5e8
8705e71
 
 
 
 
 
 
 
 
 
5bfd5e8
 
 
 
 
 
 
 
 
 
 
 
8705e71
 
5bfd5e8
 
dc71877
5bfd5e8
 
 
dc71877
5bfd5e8
 
 
 
dc71877
5bfd5e8
c662121
 
 
 
 
 
 
8705e71
 
 
 
 
 
 
 
5bfd5e8
1014ac5
5bfd5e8
8705e71
5bfd5e8
dc71877
4548114
5bfd5e8
c662121
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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()