harishB97 commited on
Commit
e8af752
·
verified ·
1 Parent(s): ec1229f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +165 -119
app.py CHANGED
@@ -1,125 +1,171 @@
1
- import gradio as gr
2
- import plotly.graph_objects as go
3
-
4
- def display_tree():
5
- # This function should create and return a Plotly figure of the tree
6
- # Currently returns a simple string, but should be replaced with actual graph
7
-
8
- # Define the nodes and edges for the graph
9
- nodes = ['Node 1', 'Node 2', 'Node 3', 'Node 4']
10
- edges = [(0, 1), (0, 2), (2, 3)] # Edges are tuples of node indices
11
-
12
- # Define positions for the nodes (you can use a layout algorithm for more complex graphs)
13
- positions = [(0, 0), (1, 2), (1, -2), (2, 0)]
14
-
15
- # Create traces for nodes and edges
16
- edge_x = []
17
- edge_y = []
18
- for edge in edges:
19
- x0, y0 = positions[edge[0]]
20
- x1, y1 = positions[edge[1]]
21
- edge_x.extend([x0, x1, None])
22
- edge_y.extend([y0, y1, None])
23
-
24
- edge_trace = go.Scatter(
25
- x=edge_x, y=edge_y,
26
- line=dict(width=2, color='Black'),
27
- hoverinfo='none',
28
- mode='lines')
29
-
30
- node_x = [pos[0] for pos in positions]
31
- node_y = [pos[1] for pos in positions]
32
-
33
- node_trace = go.Scatter(
34
- x=node_x, y=node_y,
35
- mode='markers+text',
36
- hoverinfo='text',
37
- marker=dict(showscale=False, size=10, color='Goldenrod'),
38
- text=nodes,
39
- textposition="top center"
40
- )
41
-
42
- # Define the layout of the graph
43
- layout = go.Layout(
44
- showlegend=False,
45
- hovermode='closest',
46
- margin=dict(b=0, l=0, r=0, t=0),
47
- xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
48
- yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
49
- )
50
-
51
- # Create the figure
52
- fig = go.Figure(data=[edge_trace, node_trace], layout=layout)
53
- return fig
54
-
55
- def display_image_based_on_dropdown_1(dropdown_value):
56
- # Create a white image
57
- img = Image.new('RGB', (200, 100), color='white')
58
- d = ImageDraw.Draw(img)
59
-
60
- # Specify a font. If you have a .ttf font file you can specify its path
61
- # fnt = ImageFont.truetype('/path/to/font.ttf', 40)
62
- # Otherwise, we'll use a default PIL font
63
- fnt = ImageFont.load_default()
64
-
65
- # Position the text in the center
66
- text = "Placeholder"
67
- textwidth, textheight = d.textsize(text, font=fnt)
68
- width, height = img.size
69
- x = (width - textwidth) / 2
70
- y = (height - textheight) / 2
71
-
72
- # Draw the text onto the image
73
- d.text((x,y), text, font=fnt, fill='black')
74
-
75
- # Save the image to a file in buffer to return
76
- img.save('/tmp/dummy_image.png')
77
-
78
- return '/tmp/dummy_image.png'
79
-
80
- def display_image_based_on_dropdown_2(dropdown_value):
81
- # Create a white image
82
- img = Image.new('RGB', (200, 100), color='white')
83
- d = ImageDraw.Draw(img)
84
-
85
- # Specify a font. If you have a .ttf font file you can specify its path
86
- # fnt = ImageFont.truetype('/path/to/font.ttf', 40)
87
- # Otherwise, we'll use a default PIL font
88
- fnt = ImageFont.load_default()
89
-
90
- # Position the text in the center
91
- text = "Placeholder"
92
- textwidth, textheight = d.textsize(text, font=fnt)
93
- width, height = img.size
94
- x = (width - textwidth) / 2
95
- y = (height - textheight) / 2
96
-
97
- # Draw the text onto the image
98
- d.text((x,y), text, font=fnt, fill='black')
99
-
100
- # Save the image to a file in buffer to return
101
- img.save('/tmp/dummy_image.png')
102
-
103
- return '/tmp/dummy_image.png'
104
 
105
- with gr.Blocks() as demo:
106
- gr.Markdown("## Interactive Tree and Image Display")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
- with gr.Row():
109
- tree_output = gr.Plot(display_tree) # Connect the function directly
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  with gr.Row():
112
- with gr.Column():
113
- dropdown_1_nodename = gr.Dropdown(label="Select Option for Field 2", choices=["Option 1", "Option 2", "Option 3"])
114
- dropdown_1_protos = gr.Dropdown(label="Select Option for Field 2", choices=["Option 1", "Option 2", "Option 3"])
115
- image_output_1 = gr.Image('new_teaser (3)-1.png')
116
- with gr.Column():
117
- dropdown_2_nodename = gr.Dropdown(label="Select Option for Field 2", choices=["Option 1", "Option 2", "Option 3"])
118
- dropdown_2_protos = gr.Dropdown(label="Select Option for Field 2", choices=["Option 1", "Option 2", "Option 3"])
119
- image_output_2 = gr.Image('new_teaser (3)-1.png')
120
-
121
- # Initialize with placeholder images
122
- # image_output_1.update(display_image_based_on_dropdown_1)
123
- # image_output_2.update(display_image_based_on_dropdown_2)
124
 
125
  demo.launch()
 
1
+ # import gradio as gr
2
+ # import plotly.graph_objects as go
3
+ # import os
4
+ # from collections import defaultdict
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ # species_to_imgpath = {'bird': '/descendent_specific_topk=10_heatmap_withbb_ep=last_024+051'}
7
+
8
+ # # this has to be there for each species
9
+ # imgname_to_filepath = {} # this ignores the extension such as .png
10
+ # nodename_to_protoIDs = defaultdict()
11
+
12
+ # for species, imgpath in species_to_imgpath.items():
13
+ # for foldername in os.listdir(imgpath):
14
+ # if os.isdir(os.path.join(imgpath, foldername)):
15
+ # folderpath = os.path.join(imgpath, foldername)
16
+ # for filename in os.listdir(folderpath):
17
+ # if filename.endswith('png') or filename.endswith('jpg'):
18
+ # filepath = os.path.join(folderpath, filename)
19
+ # imgname_to_filepath[filename] = filepath
20
+ # nodename = filepath.split('.')[0].split('-')[0]
21
+ # protoID = filepath.split('.')[0].split('-')[1]
22
+ # nodename_to_protoIDs[nodename].append(protoID)
23
+
24
+
25
+ # def display_tree():
26
+ # # This function should create and return a Plotly figure of the tree
27
+ # # Currently returns a simple string, but should be replaced with actual graph
28
+
29
+ # # Define the nodes and edges for the graph
30
+ # nodes = ['Node 1', 'Node 2', 'Node 3', 'Node 4']
31
+ # edges = [(0, 1), (0, 2), (2, 3)] # Edges are tuples of node indices
32
+
33
+ # # Define positions for the nodes (you can use a layout algorithm for more complex graphs)
34
+ # positions = [(0, 0), (1, 2), (1, -2), (2, 0)]
35
+
36
+ # # Create traces for nodes and edges
37
+ # edge_x = []
38
+ # edge_y = []
39
+ # for edge in edges:
40
+ # x0, y0 = positions[edge[0]]
41
+ # x1, y1 = positions[edge[1]]
42
+ # edge_x.extend([x0, x1, None])
43
+ # edge_y.extend([y0, y1, None])
44
+
45
+ # edge_trace = go.Scatter(
46
+ # x=edge_x, y=edge_y,
47
+ # line=dict(width=2, color='Black'),
48
+ # hoverinfo='none',
49
+ # mode='lines')
50
+
51
+ # node_x = [pos[0] for pos in positions]
52
+ # node_y = [pos[1] for pos in positions]
53
+
54
+ # node_trace = go.Scatter(
55
+ # x=node_x, y=node_y,
56
+ # mode='markers+text',
57
+ # hoverinfo='text',
58
+ # marker=dict(showscale=False, size=10, color='Goldenrod'),
59
+ # text=nodes,
60
+ # textposition="top center"
61
+ # )
62
+
63
+ # # Define the layout of the graph
64
+ # layout = go.Layout(
65
+ # showlegend=False,
66
+ # hovermode='closest',
67
+ # margin=dict(b=0, l=0, r=0, t=0),
68
+ # xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
69
+ # yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
70
+ # )
71
+
72
+ # # Create the figure
73
+ # fig = go.Figure(data=[edge_trace, node_trace], layout=layout)
74
+ # return fig
75
+
76
+ # def display_image_based_on_dropdown_1(dropdown_value):
77
+ # # Create a white image
78
+ # img = Image.new('RGB', (200, 100), color='white')
79
+ # d = ImageDraw.Draw(img)
80
+
81
+ # # Specify a font. If you have a .ttf font file you can specify its path
82
+ # # fnt = ImageFont.truetype('/path/to/font.ttf', 40)
83
+ # # Otherwise, we'll use a default PIL font
84
+ # fnt = ImageFont.load_default()
85
+
86
+ # # Position the text in the center
87
+ # text = "Placeholder"
88
+ # textwidth, textheight = d.textsize(text, font=fnt)
89
+ # width, height = img.size
90
+ # x = (width - textwidth) / 2
91
+ # y = (height - textheight) / 2
92
+
93
+ # # Draw the text onto the image
94
+ # d.text((x,y), text, font=fnt, fill='black')
95
 
96
+ # # Save the image to a file in buffer to return
97
+ # img.save('/tmp/dummy_image.png')
98
+
99
+ # return '/tmp/dummy_image.png'
100
+
101
+ # def display_image_based_on_dropdown_2(dropdown_value):
102
+ # # Create a white image
103
+ # img = Image.new('RGB', (200, 100), color='white')
104
+ # d = ImageDraw.Draw(img)
105
+
106
+ # # Specify a font. If you have a .ttf font file you can specify its path
107
+ # # fnt = ImageFont.truetype('/path/to/font.ttf', 40)
108
+ # # Otherwise, we'll use a default PIL font
109
+ # fnt = ImageFont.load_default()
110
+
111
+ # # Position the text in the center
112
+ # text = "Placeholder"
113
+ # textwidth, textheight = d.textsize(text, font=fnt)
114
+ # width, height = img.size
115
+ # x = (width - textwidth) / 2
116
+ # y = (height - textheight) / 2
117
+
118
+ # # Draw the text onto the image
119
+ # d.text((x,y), text, font=fnt, fill='black')
120
 
121
+ # # Save the image to a file in buffer to return
122
+ # img.save('/tmp/dummy_image.png')
123
+
124
+ # return '/tmp/dummy_image.png'
125
+
126
+ # with gr.Blocks() as demo:
127
+ # gr.Markdown("## Interactive Tree and Image Display")
128
+
129
+ # with gr.Row():
130
+ # tree_output = gr.Plot(display_tree) # Connect the function directly
131
+
132
+ # with gr.Row():
133
+ # with gr.Column():
134
+ # dropdown_1_nodename = gr.Dropdown(label="Select any node name", choices=["Option 1", "Option 2", "Option 3"])
135
+ # dropdown_1_protos = gr.Dropdown(label="Select a prototype ID", choices=["Option 1", "Option 2", "Option 3"])
136
+ # image_output_1 = gr.Image('new_teaser (3)-1.png')
137
+ # with gr.Column():
138
+ # dropdown_2_nodename = gr.Dropdown(label="Select any node name", choices=["Option 1", "Option 2", "Option 3"])
139
+ # dropdown_2_protos = gr.Dropdown(label="Select a prototype ID", choices=["Option 1", "Option 2", "Option 3"])
140
+ # image_output_2 = gr.Image('new_teaser (3)-1.png')
141
+
142
+ # # Initialize with placeholder images
143
+ # # image_output_1.update(display_image_based_on_dropdown_1)
144
+ # # image_output_2.update(display_image_based_on_dropdown_2)
145
+
146
+ # demo.launch()
147
+
148
+
149
+ import gradio as gr
150
+
151
+ def update_options(selected_option):
152
+ # Logic to determine new options based on selected option
153
+ if selected_option == "Option 1":
154
+ new_options = ["Suboption 1.1", "Suboption 1.2"]
155
+ elif selected_option == "Option 2":
156
+ new_options = ["Suboption 2.1", "Suboption 2.2"]
157
+ else:
158
+ new_options = ["Suboption 3.1", "Suboption 3.2"]
159
+
160
+ # Return the new options to update the second dropdown
161
+ return new_options, selected_option # Return current selection to persist it in the first dropdown
162
+
163
+ with gr.Blocks() as demo:
164
  with gr.Row():
165
+ dropdown_1 = gr.Dropdown(["Option 1", "Option 2", "Option 3"], label="Main Options")
166
+ dropdown_2 = gr.Dropdown([], label="Sub Options")
167
+
168
+ # When the first dropdown changes, update the options in the second dropdown
169
+ dropdown_1.change(fn=update_options, inputs=dropdown_1, outputs=[dropdown_2, dropdown_1])
 
 
 
 
 
 
 
170
 
171
  demo.launch()