Zaherrr commited on
Commit
4525147
·
verified ·
1 Parent(s): 95af92d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -29
app.py CHANGED
@@ -1,15 +1,21 @@
1
  import gradio as gr
2
- from datasets import load_dataset
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_MarkMap_Synthetic_Dataset", split='data')
 
11
  print(f'This is the dataset: {dataset}')
 
12
  print(dataset.info)
 
13
  print(f'This is an example: {dataset[-5]}')
14
 
15
  def reshape_json_data_to_fit_visualize_graph(graph_data):
@@ -26,43 +32,33 @@ def reshape_json_data_to_fit_visualize_graph(graph_data):
26
  graph_data = {"nodes": transformed_nodes, "edges": transformed_edges}
27
  return graph_data
28
 
29
- def image_to_base64(img):
30
- """Convert a PIL image to a base64 string."""
31
- buffer = io.BytesIO()
32
- img.save(buffer, format="PNG")
33
- base64_str = base64.b64encode(buffer.getvalue()).decode("utf-8")
34
- return base64_str
35
-
36
  def display_example(index):
37
  example = dataset[index]
38
  img = example["image"]
39
 
40
- # Convert the image to a base64 string
41
- base64_image = image_to_base64(img)
42
-
43
- # Create the HTML for zoomable image
44
- img_html = f'''
45
- <div style="width:100%; height: 500px; overflow: hidden; position:relative;">
46
- <img src="data:image/png;base64,{base64_image}" style="width:100%; cursor: zoom-in;" onclick="this.style.cursor = 'zoom-out'; this.style.transform = 'scale(2)';" onmouseleave="this.style.cursor = 'zoom-in'; this.style.transform = 'scale(1)';" />
47
- </div>
48
- '''
49
-
50
  # Prepare the graph data
51
  graph_data = {"nodes": example["nodes"], "edges": example["edges"]}
52
  transformed_graph_data = reshape_json_data_to_fit_visualize_graph(graph_data)
53
 
54
  # Generate the graph visualization
55
  graph_html = visualize_graph(transformed_graph_data)
56
-
 
 
 
57
  # Convert graph_data to a formatted JSON string
58
  json_data = json.dumps(transformed_graph_data, indent=2)
59
 
60
- return img_html, graph_html, json_data, transformed_graph_data, f"Width: {img.width}px, Height: {img.height}px"
61
 
62
  def create_interface():
63
  with gr.Blocks() as demo:
 
64
  gr.Markdown("# Knowledge Graph Visualizer for the [Zaherrr/OOP_KG_MarkMap_Synthetic_Dataset](https://huggingface.co/datasets/Zaherrr/OOP_KG_MarkMap_Synthetic_Dataset) dataset")
65
-
66
  with gr.Row():
67
  index_slider = gr.Slider(
68
  minimum=0,
@@ -70,9 +66,9 @@ def create_interface():
70
  step=1,
71
  label="Example Index"
72
  )
73
-
74
  with gr.Row():
75
- image_output = gr.HTML(label="Zoomable Image")
76
  graph_output = gr.HTML(label="Knowledge Graph")
77
 
78
  with gr.Row():
@@ -81,7 +77,7 @@ def create_interface():
81
  placeholder="Width and Height will appear here",
82
  interactive=False,
83
  )
84
-
85
  with gr.Row():
86
  json_output = gr.Code(language="json", label="Graph JSON Data")
87
  text_output = gr.Textbox(
@@ -89,16 +85,16 @@ def create_interface():
89
  placeholder="Text data will appear here",
90
  interactive=False,
91
  )
92
-
93
  index_slider.change(
94
  fn=display_example,
95
  inputs=[index_slider],
96
  outputs=[image_output, graph_output, json_output, text_output, dimensions_output],
97
  )
98
-
99
  return demo
100
 
101
  # Create and launch the interface
102
  if __name__ == "__main__":
103
  demo = create_interface()
104
- demo.launch()
 
1
  import gradio as gr
2
+ from datasets import load_dataset, Dataset
3
  from PIL import Image
4
  import io
5
  import base64
6
+ import json
7
  from graph_visualization import visualize_graph
8
 
9
+ # branch_name = "edges-sorted-ascending"
10
+ branch_name = "Sorted_edges"
11
+
12
  # Load the dataset
13
+ # dataset = load_dataset("Zaherrr/OOP_KG_Dataset", split='data', revision=branch_name)
14
+ dataset = load_dataset("Zaherrr/OOP_KG_MarkMap_Synthetic_Dataset", split='data') #, revision=branch_name)
15
  print(f'This is the dataset: {dataset}')
16
+
17
  print(dataset.info)
18
+
19
  print(f'This is an example: {dataset[-5]}')
20
 
21
  def reshape_json_data_to_fit_visualize_graph(graph_data):
 
32
  graph_data = {"nodes": transformed_nodes, "edges": transformed_edges}
33
  return graph_data
34
 
 
 
 
 
 
 
 
35
  def display_example(index):
36
  example = dataset[index]
37
  img = example["image"]
38
 
39
+ # Get image dimensions
40
+ img_width, img_height = img.size
41
+
 
 
 
 
 
 
 
42
  # Prepare the graph data
43
  graph_data = {"nodes": example["nodes"], "edges": example["edges"]}
44
  transformed_graph_data = reshape_json_data_to_fit_visualize_graph(graph_data)
45
 
46
  # Generate the graph visualization
47
  graph_html = visualize_graph(transformed_graph_data)
48
+
49
+ # Modify the iframe to have a fixed height
50
+ graph_html = graph_html.replace('height: 100vh;', 'height: 500px;')
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, f"Width: {img_width}px, Height: {img_height}px"
56
 
57
  def create_interface():
58
  with gr.Blocks() as demo:
59
+
60
  gr.Markdown("# Knowledge Graph Visualizer for the [Zaherrr/OOP_KG_MarkMap_Synthetic_Dataset](https://huggingface.co/datasets/Zaherrr/OOP_KG_MarkMap_Synthetic_Dataset) dataset")
61
+
62
  with gr.Row():
63
  index_slider = gr.Slider(
64
  minimum=0,
 
66
  step=1,
67
  label="Example Index"
68
  )
69
+
70
  with gr.Row():
71
+ image_output = gr.Image(type="pil", label="Image", height=500)
72
  graph_output = gr.HTML(label="Knowledge Graph")
73
 
74
  with gr.Row():
 
77
  placeholder="Width and Height will appear here",
78
  interactive=False,
79
  )
80
+
81
  with gr.Row():
82
  json_output = gr.Code(language="json", label="Graph JSON Data")
83
  text_output = gr.Textbox(
 
85
  placeholder="Text data will appear here",
86
  interactive=False,
87
  )
88
+
89
  index_slider.change(
90
  fn=display_example,
91
  inputs=[index_slider],
92
  outputs=[image_output, graph_output, json_output, text_output, dimensions_output],
93
  )
94
+
95
  return demo
96
 
97
  # Create and launch the interface
98
  if __name__ == "__main__":
99
  demo = create_interface()
100
+ demo.launch()