rioanggara commited on
Commit
8705e71
·
1 Parent(s): a6fb37c
Files changed (1) hide show
  1. app.py +28 -12
app.py CHANGED
@@ -1,14 +1,21 @@
1
  import gradio as gr
 
2
  import numpy as np
3
  from sklearn.linear_model import LinearRegression
4
  import matplotlib.pyplot as plt
5
  import io
6
  from PIL import Image
7
 
8
- def linear_regression(x_values, y_values):
9
- # Convert string inputs to numpy arrays
10
- X = np.array([float(x) for x in x_values.split(',')]).reshape(-1, 1)
11
- y = np.array([float(y) for y in y_values.split(',')])
 
 
 
 
 
 
12
 
13
  # Perform linear regression
14
  model = LinearRegression()
@@ -21,8 +28,8 @@ def linear_regression(x_values, y_values):
21
  plt.figure(figsize=(10, 6))
22
  plt.scatter(X, y, color='blue')
23
  plt.plot(X, y_pred, color='red')
24
- plt.xlabel("X Values")
25
- plt.ylabel("Y Values")
26
  plt.title('Linear Regression')
27
 
28
  # Save plot to a buffer and convert to PIL Image
@@ -36,20 +43,29 @@ def linear_regression(x_values, y_values):
36
 
37
  return image, coef_info
38
 
 
 
 
 
 
 
 
 
 
 
 
39
  # Gradio interface
40
  iface = gr.Interface(
41
  fn=linear_regression,
42
- inputs=[
43
- gr.components.Textbox(placeholder="Enter X values separated by commas (e.g., 1,2,3)", label="X Values"),
44
- gr.components.Textbox(placeholder="Enter Y values separated by commas (e.g., 2,4,6)", label="Y Values")
45
- ],
46
  outputs=[
47
  gr.components.Image(type="pil"),
48
  gr.components.Textbox(label="Regression Info")
49
  ],
50
  title="Automatic Linear Regression Modeling",
51
- description="Enter X and Y values as comma-separated lists to perform linear regression."
52
- )
 
53
 
54
  # Launch the app
55
  if __name__ == "__main__":
 
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
  from PIL import Image
8
 
9
+ def linear_regression(input_csv):
10
+ # Load dataset from binary
11
+ df = pd.read_csv(io.BytesIO(input_csv))
12
+
13
+ # Assume the first column is X and the second column is Y
14
+ if len(df.columns) < 2:
15
+ return None, "CSV file must contain at least two columns."
16
+
17
+ X = df.iloc[:, 0].values.reshape(-1, 1)
18
+ y = df.iloc[:, 1].values
19
 
20
  # Perform linear regression
21
  model = LinearRegression()
 
28
  plt.figure(figsize=(10, 6))
29
  plt.scatter(X, y, color='blue')
30
  plt.plot(X, y_pred, color='red')
31
+ plt.xlabel(df.columns[0])
32
+ plt.ylabel(df.columns[1])
33
  plt.title('Linear Regression')
34
 
35
  # Save plot to a buffer and convert to PIL Image
 
43
 
44
  return image, coef_info
45
 
46
+ # Tutorial Markdown
47
+ tutorial_markdown = """
48
+ ## Tutorial
49
+
50
+ Follow these steps to use the application:
51
+
52
+ 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).
53
+ 2. Upload the CSV file using the 'File Upload' field.
54
+ 3. The application will automatically process the file, perform linear regression, and display the results and a plot.
55
+ """
56
+
57
  # Gradio interface
58
  iface = gr.Interface(
59
  fn=linear_regression,
60
+ inputs=[gr.components.File(type="binary")],
 
 
 
61
  outputs=[
62
  gr.components.Image(type="pil"),
63
  gr.components.Textbox(label="Regression Info")
64
  ],
65
  title="Automatic Linear Regression Modeling",
66
+ description="Upload a CSV file with two columns. The first column will be used as X (independent variable) and the second as Y (dependent variable).",
67
+ layout="vertical"
68
+ ).add_instructions(tutorial_markdown)
69
 
70
  # Launch the app
71
  if __name__ == "__main__":