harishB97 commited on
Commit
525a6df
·
verified ·
1 Parent(s): 628334e

Update app.py

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