Zaherrr commited on
Commit
0d30669
1 Parent(s): efcd777

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -30
app.py CHANGED
@@ -3,68 +3,62 @@ from datasets import load_dataset, Dataset
3
  from PIL import Image
4
  import io
5
  import base64
6
- import json
7
-
8
  from graph_visualization import visualize_graph
9
 
10
  # Load the dataset
11
- dataset = load_dataset(
12
- "Zaherrr/OOP_KG_Dataset", split='data'#train'
13
- )
14
  print(f'This is the dataset: {dataset}')
15
 
16
-
17
  def reshape_json_data_to_fit_visualize_graph(graph_data):
18
-
19
  nodes = graph_data["nodes"]
20
  edges = graph_data["edges"]
21
-
22
  transformed_nodes = [
23
  {"id": nodes["id"][idx], "label": nodes["label"][idx]}
24
  for idx in range(len(nodes["id"]))
25
  ]
26
-
27
  transformed_edges = [
28
  {"source": edges["source"][idx], "target": edges["target"][idx], "type": "->"}
29
  for idx in range(len(edges["source"]))
30
  ]
31
-
32
- # print(f"transformed nodes = {transformed_nodes}")
33
-
34
  graph_data = {"nodes": transformed_nodes, "edges": transformed_edges}
35
-
36
  return graph_data
37
 
38
-
39
-
40
  def display_example(index):
41
  example = dataset[index]
42
  img = example["image"]
43
-
44
  # Prepare the graph data
45
  graph_data = {"nodes": example["nodes"], "edges": example["edges"]}
46
-
47
  transformed_graph_data = reshape_json_data_to_fit_visualize_graph(graph_data)
48
-
49
  # Generate the graph visualization
50
  graph_html = visualize_graph(transformed_graph_data)
51
-
 
 
 
 
 
 
 
52
  # Convert graph_data to a formatted JSON string
53
  json_data = json.dumps(transformed_graph_data, indent=2)
54
-
55
- return img, graph_html, json_data, transformed_graph_data
56
-
57
-
58
 
59
  def create_interface():
60
  with gr.Blocks() as demo:
61
  gr.Markdown("# Knowledge Graph Visualizer")
62
-
63
  with gr.Row():
64
  index_slider = gr.Slider(
65
- minimum=0, maximum=len(dataset) - 1, step=1, label="Example Index"
 
 
 
66
  )
67
-
68
  with gr.Row():
69
  image_output = gr.Image(type="pil", label="Image", height=300)
70
  graph_output = gr.HTML(label="Knowledge Graph")
@@ -76,17 +70,16 @@ def create_interface():
76
  placeholder="Text data will appear here",
77
  interactive=False,
78
  )
79
-
80
  index_slider.change(
81
  fn=display_example,
82
  inputs=[index_slider],
83
  outputs=[image_output, graph_output, json_output, text_output],
84
  )
85
-
86
  return demo
87
 
88
-
89
  # Create and launch the interface
90
  if __name__ == "__main__":
91
  demo = create_interface()
92
- demo.launch()
 
3
  from PIL import Image
4
  import io
5
  import base64
6
+ import json
 
7
  from graph_visualization import visualize_graph
8
 
9
  # Load the dataset
10
+ dataset = load_dataset("Zaherrr/OOP_KG_Dataset", split='data')
 
 
11
  print(f'This is the dataset: {dataset}')
12
 
 
13
  def reshape_json_data_to_fit_visualize_graph(graph_data):
 
14
  nodes = graph_data["nodes"]
15
  edges = graph_data["edges"]
 
16
  transformed_nodes = [
17
  {"id": nodes["id"][idx], "label": nodes["label"][idx]}
18
  for idx in range(len(nodes["id"]))
19
  ]
 
20
  transformed_edges = [
21
  {"source": edges["source"][idx], "target": edges["target"][idx], "type": "->"}
22
  for idx in range(len(edges["source"]))
23
  ]
 
 
 
24
  graph_data = {"nodes": transformed_nodes, "edges": transformed_edges}
 
25
  return graph_data
26
 
 
 
27
  def display_example(index):
28
  example = dataset[index]
29
  img = example["image"]
30
+
31
  # Prepare the graph data
32
  graph_data = {"nodes": example["nodes"], "edges": example["edges"]}
 
33
  transformed_graph_data = reshape_json_data_to_fit_visualize_graph(graph_data)
34
+
35
  # Generate the graph visualization
36
  graph_html = visualize_graph(transformed_graph_data)
37
+
38
+ # Wrap the graph HTML in a div with fixed height and scrolling
39
+ graph_html_with_style = f"""
40
+ <div style="height: 500px; overflow-y: auto;">
41
+ {graph_html}
42
+ </div>
43
+ """
44
+
45
  # Convert graph_data to a formatted JSON string
46
  json_data = json.dumps(transformed_graph_data, indent=2)
47
+
48
+ return img, graph_html_with_style, json_data, transformed_graph_data
 
 
49
 
50
  def create_interface():
51
  with gr.Blocks() as demo:
52
  gr.Markdown("# Knowledge Graph Visualizer")
53
+
54
  with gr.Row():
55
  index_slider = gr.Slider(
56
+ minimum=0,
57
+ maximum=len(dataset) - 1,
58
+ step=1,
59
+ label="Example Index"
60
  )
61
+
62
  with gr.Row():
63
  image_output = gr.Image(type="pil", label="Image", height=300)
64
  graph_output = gr.HTML(label="Knowledge Graph")
 
70
  placeholder="Text data will appear here",
71
  interactive=False,
72
  )
73
+
74
  index_slider.change(
75
  fn=display_example,
76
  inputs=[index_slider],
77
  outputs=[image_output, graph_output, json_output, text_output],
78
  )
79
+
80
  return demo
81
 
 
82
  # Create and launch the interface
83
  if __name__ == "__main__":
84
  demo = create_interface()
85
+ demo.launch()