Spaces:
Sleeping
Sleeping
Commit
·
5bfd5e8
1
Parent(s):
c02e1f0
changes
Browse files- app.py +54 -45
- requirements.txt +4 -5
app.py
CHANGED
@@ -1,50 +1,59 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
import matplotlib.pyplot as plt
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
plt.
|
26 |
-
plt.
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
37 |
iface = gr.Interface(
|
38 |
-
fn=
|
39 |
-
inputs=
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
)
|
48 |
|
49 |
-
# Launch the
|
50 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from sklearn.linear_model import LinearRegression
|
5 |
import matplotlib.pyplot as plt
|
6 |
+
import io
|
7 |
+
|
8 |
+
def linear_regression(input_csv, x_column, y_column):
|
9 |
+
# Load dataset
|
10 |
+
df = pd.read_csv(input_csv)
|
11 |
+
|
12 |
+
# Prepare data for regression
|
13 |
+
X = df[[x_column]].values
|
14 |
+
y = df[y_column].values
|
15 |
+
|
16 |
+
# Perform linear regression
|
17 |
+
model = LinearRegression()
|
18 |
+
model.fit(X, y)
|
19 |
+
|
20 |
+
# Make predictions
|
21 |
+
y_pred = model.predict(X)
|
22 |
+
|
23 |
+
# Plotting
|
24 |
+
plt.figure(figsize=(10, 6))
|
25 |
+
plt.scatter(X, y, color='blue')
|
26 |
+
plt.plot(X, y_pred, color='red')
|
27 |
+
plt.xlabel(x_column)
|
28 |
+
plt.ylabel(y_column)
|
29 |
+
plt.title('Linear Regression')
|
30 |
+
|
31 |
+
# Save plot to a buffer
|
32 |
+
buf = io.BytesIO()
|
33 |
+
plt.savefig(buf, format='png')
|
34 |
+
buf.seek(0)
|
35 |
+
|
36 |
+
# Regression info
|
37 |
+
coef_info = f"Coefficient: {model.coef_[0]}\nIntercept: {model.intercept_}"
|
38 |
+
|
39 |
+
return buf, coef_info
|
40 |
+
|
41 |
+
# Gradio interface
|
42 |
iface = gr.Interface(
|
43 |
+
fn=linear_regression,
|
44 |
+
inputs=[
|
45 |
+
gr.inputs.File(type="csv"),
|
46 |
+
gr.inputs.Textbox(label="X Column Name"),
|
47 |
+
gr.inputs.Textbox(label="Y Column Name"),
|
48 |
+
],
|
49 |
+
outputs=[
|
50 |
+
gr.outputs.Image(type="plot"),
|
51 |
+
gr.outputs.Textbox(label="Regression Info")
|
52 |
+
],
|
53 |
+
title="Automatic Linear Regression Modeling",
|
54 |
+
description="Upload a CSV file and specify the columns for performing linear regression."
|
55 |
)
|
56 |
|
57 |
+
# Launch the app
|
58 |
+
if __name__ == "__main__":
|
59 |
+
iface.launch()
|
requirements.txt
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
gradio
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
torch
|
|
|
1 |
gradio
|
2 |
+
pandas
|
3 |
+
numpy
|
4 |
+
scikit-learn
|
5 |
+
matplotlib
|
|