burtenshaw commited on
Commit
cb64143
·
0 Parent(s):

first commit

Browse files
Files changed (7) hide show
  1. .gitignore +10 -0
  2. .python-version +1 -0
  3. README.md +0 -0
  4. app.py +123 -0
  5. example.ipynb +0 -0
  6. pyproject.toml +13 -0
  7. uv.lock +0 -0
.gitignore ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python-generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+
9
+ # Virtual environments
10
+ .venv
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.11
README.md ADDED
File without changes
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import plotly.express as px
3
+ import numpy as np
4
+ import pandas as pd
5
+ from sklearn.metrics import confusion_matrix
6
+ from PIL import Image
7
+ from io import BytesIO
8
+
9
+ def generate_plot(
10
+ x_sequence: str,
11
+ y_sequence: str,
12
+ plot_type: str,
13
+ x_label: str,
14
+ y_label: str,
15
+ width: int,
16
+ height: int
17
+ ) -> Image:
18
+ """
19
+ Generate a plot based on the provided x and y sequences and plot type.
20
+
21
+ Parameters:
22
+ - x_sequence (str): A comma-separated string of x values.
23
+ - y_sequence (str): A comma-separated string of y values.
24
+ - plot_type (str): The type of plot to generate ('Bar', 'Scatter', 'Confusion Matrix').
25
+ - x_label (str): Label for the x-axis.
26
+ - y_label (str): Label for the y-axis.
27
+ - width (int): Width of the plot.
28
+ - height (int): Height of the plot.
29
+
30
+ Returns:
31
+ - Image: A PIL Image object of the generated plot.
32
+ """
33
+ # Convert the input sequences to lists of numbers
34
+ try:
35
+ x_data = list(map(float, x_sequence.split(",")))
36
+ y_data = list(map(float, y_sequence.split(",")))
37
+ except ValueError:
38
+ return "Invalid input. Please enter sequences of numbers separated by commas."
39
+
40
+ # Ensure the x and y sequences have the same length
41
+ if len(x_data) != len(y_data):
42
+ return "The x and y sequences must have the same length."
43
+
44
+ # Create a DataFrame for plotting
45
+ df = pd.DataFrame({"x": x_data, "y": y_data})
46
+
47
+ # Set default width and height if not provided
48
+ width = width if width else 800
49
+ height = height if height else 600
50
+
51
+ # Generate the plot based on the selected type
52
+ if plot_type == "Bar":
53
+ fig = px.bar(
54
+ df,
55
+ x="x",
56
+ y="y",
57
+ title="Bar Plot",
58
+ labels={"x": x_label, "y": y_label},
59
+ width=width,
60
+ height=height,
61
+ )
62
+ elif plot_type == "Scatter":
63
+ fig = px.scatter(
64
+ df,
65
+ x="x",
66
+ y="y",
67
+ title="Scatter Plot",
68
+ labels={"x": x_label, "y": y_label},
69
+ width=width,
70
+ height=height,
71
+ )
72
+ elif plot_type == "Confusion Matrix":
73
+ # For demonstration, create a confusion matrix from the sequence
74
+ y_true = np.random.randint(0, 2, len(y_data))
75
+ y_pred = np.array(y_data) > 0.5
76
+ cm = confusion_matrix(y_true, y_pred)
77
+ fig = px.imshow(
78
+ cm, text_auto=True, title="Confusion Matrix", width=width, height=height
79
+ )
80
+ else:
81
+ return "Invalid plot type selected."
82
+
83
+ # Convert the plot to a PNG image
84
+ img_bytes = fig.to_image(
85
+ format="png", width=width, height=height, scale=2, engine="kaleido"
86
+ )
87
+ return Image.open(BytesIO(img_bytes))
88
+
89
+
90
+ # Define the Gradio interface using the new syntax
91
+ app = gr.Interface(
92
+ fn=generate_plot,
93
+ inputs=[
94
+ gr.Textbox(
95
+ lines=2,
96
+ placeholder="Enter x sequence of numbers separated by commas",
97
+ label="X",
98
+ ),
99
+ gr.Textbox(
100
+ lines=2,
101
+ placeholder="Enter y sequence of numbers separated by commas",
102
+ label="Y",
103
+ ),
104
+ gr.Radio(["Bar", "Scatter", "Confusion Matrix"], label="Type", value="Bar"),
105
+ gr.Textbox(
106
+ placeholder="Enter x-axis label (optional)", label="X_Label", value=""
107
+ ),
108
+ gr.Textbox(
109
+ placeholder="Enter y-axis label (optional)", label="Y_Label", value=""
110
+ ),
111
+ gr.Number(
112
+ value=800,
113
+ label="Width",
114
+ ),
115
+ gr.Number(value=600, label="Height"),
116
+ ],
117
+ outputs=gr.Image(type="pil", label="Generated Plot"),
118
+ title="Plotly Plot Generator",
119
+ description="Generate plots using Plotly based on inputted sequences. Choose from Bar, Scatter, or Confusion Matrix plots.",
120
+ )
121
+
122
+ # Launch the app
123
+ app.launch()
example.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
pyproject.toml ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "agent-plotly"
3
+ version = "0.1.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ requires-python = ">=3.11"
7
+ dependencies = [
8
+ "gradio>=5.12.0",
9
+ "ipykernel>=6.29.5",
10
+ "plotly>=5.24.1",
11
+ "scikit-learn>=1.6.1",
12
+ "smolagents>=1.2.2",
13
+ ]
uv.lock ADDED
The diff for this file is too large to render. See raw diff