avantol commited on
Commit
e1dd4c7
·
1 Parent(s): 7fe6ec9

feat(app): UI improvement by adding tables for nodes & properties

Browse files
Files changed (2) hide show
  1. app.py +29 -23
  2. utils.py +26 -7
app.py CHANGED
@@ -2,6 +2,7 @@ import copy
2
  import json
3
  import os
4
  import zipfile
 
5
 
6
  import gradio as gr
7
  import spaces
@@ -140,7 +141,7 @@ def gen_output_from_files_uploaded(filepaths: list[str] = None):
140
  sql = ""
141
 
142
  # Create Summary Table
143
- nodes_df, properties_df = {}, {}
144
  try:
145
  nodes_df, properties_df = create_summary_tables(model_response_json)
146
  except Exception as exc:
@@ -153,16 +154,18 @@ def gen_output_from_example_simple():
153
  model_response = get_example_ai_model_output_simple()
154
  model_response_json = json.loads(model_response)
155
  sql, validation = dd_to_sql(model_response_json)
 
156
 
157
- return model_response, sql
158
 
159
 
160
  def gen_output_from_example_many():
161
  model_response = get_example_ai_model_output_many()
162
  model_response_json = json.loads(model_response)
163
  sql, validation = dd_to_sql(model_response_json)
 
164
 
165
- return model_response, sql
166
 
167
 
168
  def zip_tsvs():
@@ -209,44 +212,47 @@ with gr.Blocks() as demo:
209
  )
210
 
211
  # Define Outputs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
  with gr.Row():
213
- with gr.Column(scale=7):
214
- json_out = gr.Code(
215
- label="Generated Data Model Output",
216
- value=json.dumps([]),
217
- language="json",
218
- interactive=True,
219
- show_label=True,
220
- container=True,
221
- elem_id="json-output",
222
- )
223
- with gr.Column(scale=7):
224
- sql_out = gr.Textbox(
225
- label="SQL Defined Relational Schema",
226
- value=[],
227
- show_label=True,
228
- container=True,
229
- )
230
 
231
  # If files are uploaded, generate prompt and run model
232
  if model_loaded:
233
  files.upload(
234
  fn=gen_output_from_files_uploaded,
235
  inputs=files,
236
- outputs=[json_out, sql_out],
237
  )
238
 
239
  gr.Markdown("Run out of FreeGPU or having issues? Try the example outputs!")
240
  demo_btn2 = gr.Button("Manually Load 'Simple' Example Output from Previous Run")
241
  demo_btn2.click(
242
  fn=gen_output_from_example_simple,
243
- outputs=[json_out, sql_out],
244
  )
245
 
246
  demo_btn = gr.Button("Manually Load 'Many' Example Output from Previous Run")
247
  demo_btn.click(
248
  fn=gen_output_from_example_many,
249
- outputs=[json_out, sql_out],
250
  )
251
 
252
  if __name__ == "__main__":
 
2
  import json
3
  import os
4
  import zipfile
5
+ import pandas as pd
6
 
7
  import gradio as gr
8
  import spaces
 
141
  sql = ""
142
 
143
  # Create Summary Table
144
+ nodes_df, properties_df = pd.DataFrame(), pd.DataFrame()
145
  try:
146
  nodes_df, properties_df = create_summary_tables(model_response_json)
147
  except Exception as exc:
 
154
  model_response = get_example_ai_model_output_simple()
155
  model_response_json = json.loads(model_response)
156
  sql, validation = dd_to_sql(model_response_json)
157
+ nodes_df, properties_df = create_summary_tables(model_response_json)
158
 
159
+ return model_response, sql, nodes_df, properties_df
160
 
161
 
162
  def gen_output_from_example_many():
163
  model_response = get_example_ai_model_output_many()
164
  model_response_json = json.loads(model_response)
165
  sql, validation = dd_to_sql(model_response_json)
166
+ nodes_df, properties_df = create_summary_tables(model_response_json)
167
 
168
+ return model_response, sql, nodes_df, properties_df
169
 
170
 
171
  def zip_tsvs():
 
212
  )
213
 
214
  # Define Outputs
215
+ with gr.Row(equal_height=True):
216
+ json_out = gr.Code(
217
+ label="Generated Data Model Output",
218
+ value=json.dumps([]),
219
+ language="json",
220
+ interactive=True,
221
+ show_label=True,
222
+ container=True,
223
+ elem_id="json-output",
224
+ )
225
+ sql_out = gr.Textbox(
226
+ label="SQL Defined Relational Schema",
227
+ value=[],
228
+ show_label=True,
229
+ container=True,
230
+ )
231
+
232
+ with gr.Row():
233
+ nodes_df_out = gr.Dataframe(label="Generated Node/Table Descriptions")
234
  with gr.Row():
235
+ properties_df_out = gr.Dataframe(label="Generated Property Descriptions")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
 
237
  # If files are uploaded, generate prompt and run model
238
  if model_loaded:
239
  files.upload(
240
  fn=gen_output_from_files_uploaded,
241
  inputs=files,
242
+ outputs=[json_out, sql_out, nodes_df_out, properties_df_out],
243
  )
244
 
245
  gr.Markdown("Run out of FreeGPU or having issues? Try the example outputs!")
246
  demo_btn2 = gr.Button("Manually Load 'Simple' Example Output from Previous Run")
247
  demo_btn2.click(
248
  fn=gen_output_from_example_simple,
249
+ outputs=[json_out, sql_out, nodes_df_out, properties_df_out],
250
  )
251
 
252
  demo_btn = gr.Button("Manually Load 'Many' Example Output from Previous Run")
253
  demo_btn.click(
254
  fn=gen_output_from_example_many,
255
+ outputs=[json_out, sql_out, nodes_df_out, properties_df_out],
256
  )
257
 
258
  if __name__ == "__main__":
utils.py CHANGED
@@ -2,6 +2,7 @@ import csv
2
  import os
3
  from io import BytesIO
4
  from string import Template
 
5
 
6
  import matplotlib.pyplot as plt
7
  import networkx as nx
@@ -97,16 +98,34 @@ def create_summary_tables(json_response):
97
  node_descriptions = {}
98
  node_property_descriptions = {}
99
 
100
- for node in json_response["nodes"]:
101
- node_descriptions[node["name"]] = node["description"]
102
 
103
- properties_dict = {}
104
- for prop in node["properties"]:
105
- properties_dict[prop["name"]] = prop["description"]
 
106
 
107
- node_property_descriptions[node["name"]] = properties_dict
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
- return node_descriptions, node_property_descriptions
110
 
111
 
112
  def get_example_ai_model_output_simple():
 
2
  import os
3
  from io import BytesIO
4
  from string import Template
5
+ import pandas as pd
6
 
7
  import matplotlib.pyplot as plt
8
  import networkx as nx
 
98
  node_descriptions = {}
99
  node_property_descriptions = {}
100
 
101
+ for node in json_response.get("nodes", []):
102
+ node_descriptions[node.get("name", "N/A")] = node.get("description", "N/A")
103
 
104
+ for prop in node.get("properties", []):
105
+ node_property_descriptions[
106
+ f"{node.get('name', 'N/A')}.{prop.get('name', 'N/A')}"
107
+ ] = [prop.get("type", "N/A"), prop.get("description", "N/A")]
108
 
109
+ node_descriptions_df = pd.DataFrame.from_dict(
110
+ node_descriptions, orient="index"
111
+ ).reset_index()
112
+ node_descriptions_df.rename(
113
+ columns={"index": "Node Name", 0: "Generated Description"}, inplace=True
114
+ )
115
+
116
+ node_property_descriptions_df = pd.DataFrame.from_dict(
117
+ node_property_descriptions, orient="index"
118
+ ).reset_index()
119
+ node_property_descriptions_df.rename(
120
+ columns={
121
+ "index": "Node.Property Name",
122
+ 0: "Generated Data Type",
123
+ 1: "Generated Description",
124
+ },
125
+ inplace=True,
126
+ )
127
 
128
+ return node_descriptions_df, node_property_descriptions_df
129
 
130
 
131
  def get_example_ai_model_output_simple():