File size: 11,804 Bytes
b2ad712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
365d7fc
 
 
 
b2ad712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import gradio as gr
import json
import os
from PIL import Image
from database_operations import Neo4jDatabase
from graph_visualization import visualize_graph
from utils import extract_label_prefix, strip_keys, format_json, validate_json
from models.gemini_image_to_json import fetch_gemini_response
from models.openai_image_to_json import openaiprocess_image_to_json
from any_to_image import pdf_to_images, process_image

# Initialize Neo4j database
db = Neo4jDatabase("bolt://localhost:7687", "neo4j", "password123")

def dump_to_neo4j_with_confirmation(json_content, file_path, history, previous_states):
    if not file_path:
        return "No image uploaded or invalid file", history, previous_states, None

    try:
        json_data = json.loads(json_content)
    except json.JSONDecodeError:
        return "Invalid JSON data. Please check your input.", history, previous_states, None

    label_prefix = extract_label_prefix(file_path)
    
    if db.check_existing_graph(label_prefix):
        previous_state = db.get_graph_data(label_prefix)
        return f"A graph with label prefix '{label_prefix}' already exists in the database. Do you want to overwrite it?", history, previous_states, label_prefix
    else:
        json_data = strip_keys(json_data)
        db.dump_to_neo4j(json_data['nodes'], json_data['edges'], label_prefix)
        result = f"Data successfully dumped into the database with label prefix '{label_prefix}'."
        new_history = f"{history}\n[NEW ENTRY] {result}" if history else f"[NEW ENTRY] {result}"
        previous_states[label_prefix] = []
        return result, new_history, previous_states, None

def confirm_overwrite(confirmation, gradio_state, json_content, file_path, history, previous_states):
    if confirmation.lower() == 'yes':
        try:
            label_prefix = extract_label_prefix(file_path)
            previous_state = db.get_graph_data(label_prefix)
            # print(f'previous_state from the confirm_overwrite function: {previous_state}')
            # print(f'label_prefix from the confirm_overwrite function: {label_prefix}')
            # print(f'previouse_states from the confirm_overwrite function: {previous_states}')
            
            if label_prefix not in previous_states:
                previous_states[label_prefix] = []
                previous_states[label_prefix].append(previous_state)
            else:    
                previous_states[label_prefix].append(previous_state)
            
            if len(previous_states[label_prefix]) > 3:
                previous_states[label_prefix] = previous_states[label_prefix][-3:]
                
            db.delete_graph(label_prefix)
            
            json_data = json.loads(json_content)
            json_data = strip_keys(json_data)
            db.dump_to_neo4j(json_data['nodes'], json_data['edges'], label_prefix)
            result = f"Data successfully overwritten in the database with label prefix '{label_prefix}'."
            new_history = f"{history}\n[OVERWRITE] {result}" if history else f"[OVERWRITE] {result}"
            return result, new_history, previous_states, ""
        except json.JSONDecodeError:
            return "Invalid JSON data. Please check your input.", history, previous_states, ""
    else:
        return "Operation cancelled. The existing graph was not overwritten.", history, previous_states, ""

def revert_last_action(history, previous_states):
    if not history:
        return "No actions to revert.", history, previous_states

    last_action = history.split('\n')[-1]
    label_prefix = last_action.split("'")[1]

    if label_prefix in previous_states and previous_states[label_prefix]:
        db.delete_graph(label_prefix)
        db.dump_to_neo4j(previous_states[label_prefix][-1]['nodes'], previous_states[label_prefix][-1]['edges'], label_prefix)
        new_history = history + f"\n[REVERT] Reverted overwrite of graph with label prefix '{label_prefix}'"
        previous_states[label_prefix].pop()
        return f"Reverted last action: {last_action}", new_history, previous_states
    elif label_prefix in previous_states and not previous_states[label_prefix]:
        db.delete_graph(label_prefix)
        new_history = history + f"\n[REVERT] Deleted newly added graph with label prefix '{label_prefix}'"
        del previous_states[label_prefix]
        return f"Reverted last action: {last_action}", new_history, previous_states
    else:
        return "Unable to revert the last action.", history, previous_states

def update_graph_from_edited_json(json_content, physics_enabled):
    try:
        json_data = json.loads(json_content)
        json_data = strip_keys(json_data)
        validate_json(json_data)
        return visualize_graph(json_data, physics_enabled), ""
    except json.JSONDecodeError as e:
        return None, f"Invalid JSON format: {str(e)}"
    except ValueError as e:
        return None, f"Invalid graph structure: {str(e)}"
    except Exception as e:
        return None, f"An unexpected error occurred: {str(e)}"

def fetch_kg(image_file_path, model_choice_state):
    if image_file_path:
        mind_map_image = Image.open(image_file_path)
        
        if model_choice_state == 'Gemini':
            print(f'model choice is gemini')
            kg_json_text = fetch_gemini_response(mind_map_image)
        elif model_choice_state == 'OpenAI':
            print(f'model choice is openai')
            kg_json_text = openaiprocess_image_to_json(mind_map_image)
            
        json_data = json.loads(kg_json_text)
        return format_json(json_data), ""
    return "", "No image uploaded or invalid file"

def input_file_handler(file_path):
    if file_path:
        image_path, error = process_image(file_path)
        return image_path, error
    
    return "", "No image uploaded or invalid file"

# Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("## Image to Knowledge Graph Transformation")

    with gr.Row():
        file_input = gr.File(label="Upload File", file_count="single",
                             type="filepath",
                             file_types=[".pdf", ".png", ".jpeg", ".jpg", ".heic"])
        image_file = gr.Image(label="Input Image", type="filepath", visible=False)
        json_editor = gr.Textbox(label="Edit JSON", lines=15, placeholder="JSON data will appear here after image upload")

    with gr.Row():
        with gr.Column():        
            with gr.Row():
                CCW_rotate_button = gr.Button('Rotate Image Counter-Clockwise')
                CW_rotate_button = gr.Button('Rotate Image Clockwise')
        with gr.Column():
            model_call = gr.Button('Transform Image into KG representation', scale=2)
    with gr.Row():
        physics_button = gr.Checkbox(value=True, label="Enable Graph Physics")
        model_choice = gr.Radio(label="Select Model", choices=["OpenAI", "Gemini"], value="Gemini", interactive=True)

    graph_output = gr.HTML(label="Graph Output")
    error_output = gr.Textbox(label="Error Messages", interactive=False)

    update_button = gr.Button("Update Graph")
    dump_button = gr.Button("Dump to Neo4j")
    revert_button = gr.Button("Revert Last Action")

    history_block = gr.Textbox(label="History", placeholder="Graphs pushed to the Database", interactive=False, lines=5, max_lines=50)
    history_state = gr.State("")
    previous_states = gr.State({})

    confirmation_output = gr.Textbox(label="Confirmation Message", visible=False, interactive=False)
    confirmation_input = gr.Textbox(label="Type 'yes' to confirm overwrite", visible=False, interactive=True)
    confirm_button = gr.Button("Confirm Overwrite", visible=False)

    # Added 2 examples for this deployment only
    examples_list = ["image_examples/image1.png", "image_examples/image2.png"]
    example_component = gr.Examples(examples_list, input=file_input)
    
    file_input.upload(
        fn=input_file_handler,
        inputs=[file_input],
        outputs=[image_file, error_output]
    ).then(
        lambda image_file: (
            gr.Image(value=image_file, visible=True),
            gr.File(visible=False)
        ),
        inputs=[image_file],
        outputs=[image_file, file_input]
    )
    
    image_file.clear(
        lambda file_input, image_file: (
            gr.File(visible=True),
            gr.Image(visible=False)
            ),
        inputs=[file_input, image_file],
        outputs=[file_input, image_file]
    )
    
    def rotate_image_to_left(image_path):
        if image_path:
            image = Image.open(image_path)
            image = image.rotate(-90, expand=True)
            image.save(image_path)
            return image_path
    
    CW_rotate_button.click(
        fn=rotate_image_to_left,
        inputs=[image_file],
        outputs=[image_file]
    )
    
    def rotate_image_to_right(image_path):
        if image_path:
            image = Image.open(image_path)
            image = image.rotate(90, expand=True)
            image.save(image_path)
            return image_path
    
    CCW_rotate_button.click(
        fn=rotate_image_to_right,
        inputs=[image_file],
        outputs=[image_file]
    )
    
    dump_button.click(
        dump_to_neo4j_with_confirmation,
        inputs=[json_editor, image_file, history_state, previous_states],
        outputs=[confirmation_output, history_state, previous_states, gr.State()]
    ).then(
        lambda message, history, previous_states, label_prefix: (
            gr.Textbox(value=message, visible=True),
            gr.Textbox(visible=True),
            gr.Button(visible=True),
            history,
            previous_states,
            label_prefix
        ),
        inputs=[confirmation_output, history_state, previous_states, gr.State()],
        outputs=[confirmation_output, confirmation_input, confirm_button, history_state, previous_states, gr.State()]
    ).then(
        lambda history: history,
        inputs=[history_state],
        outputs=[history_block]
    )

    gr.on(
        triggers=[confirm_button.click, confirmation_input.submit],
        fn=confirm_overwrite,
        inputs=[confirmation_input, gr.State(), json_editor, image_file, history_state, previous_states],
        outputs=[confirmation_output, history_state, previous_states, confirmation_input]
    ).then(
        lambda confirmation_output, confirmation_input: (
            gr.Textbox(value=confirmation_output, visible=True),
            gr.Textbox(value='', visible=False),
            gr.Button(visible=False)
        ),
        inputs=[confirmation_output, confirmation_input],
        outputs=[confirmation_output, confirmation_input, confirm_button]
    ).then(
        lambda history: history,
        inputs=[history_state],
        outputs=[history_block]
    )

    revert_button.click(
        revert_last_action,
        inputs=[history_state, previous_states],
        outputs=[confirmation_output, history_state, previous_states]
    ).then(
        lambda confirmation_output: gr.Textbox(value=confirmation_output, visible=True),
        inputs=[confirmation_output],
        outputs=[confirmation_output]
    ).then(
        lambda history: history,
        inputs=[history_state],
        outputs=[history_block]
    )
    
    update_button.click(
        update_graph_from_edited_json,
        inputs=[json_editor, physics_button],
        outputs=[graph_output, error_output]
    )
    
    physics_button.change(
        update_graph_from_edited_json,
        inputs=[json_editor, physics_button],
        outputs=[graph_output, error_output]
    )

    model_call.click(
        fn=fetch_kg,
        inputs=[image_file, model_choice],
        outputs=[json_editor, error_output]
    )

if __name__ == "__main__":
    demo.launch()