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lib.graph_extract import triplextract, parse_triples
|
7 |
+
from lib.visualize import create_graph, create_bokeh_plot, create_plotly_plot
|
8 |
+
from lib.samples import snippets
|
9 |
+
|
10 |
+
WORD_LIMIT = 300
|
11 |
+
|
12 |
+
def process_text(text, entity_types, predicates, layout_type, visualization_type):
|
13 |
+
if not text:
|
14 |
+
return None, None, "Please enter some text."
|
15 |
+
|
16 |
+
words = text.split()
|
17 |
+
if len(words) > WORD_LIMIT:
|
18 |
+
return None, None, f"Please limit your input to {WORD_LIMIT} words. Current word count: {len(words)}"
|
19 |
+
|
20 |
+
entity_types = [et.strip() for et in entity_types.split(",") if et.strip()]
|
21 |
+
predicates = [p.strip() for p in predicates.split(",") if p.strip()]
|
22 |
+
|
23 |
+
if not entity_types:
|
24 |
+
return None, None, "Please enter at least one entity type."
|
25 |
+
if not predicates:
|
26 |
+
return None, None, "Please enter at least one predicate."
|
27 |
+
|
28 |
+
try:
|
29 |
+
prediction = triplextract(text, entity_types, predicates)
|
30 |
+
if prediction.startswith("Error"):
|
31 |
+
return None, None, prediction
|
32 |
+
|
33 |
+
entities, relationships = parse_triples(prediction)
|
34 |
+
|
35 |
+
if not entities and not relationships:
|
36 |
+
return None, None, "No entities or relationships found. Try different text or check your input."
|
37 |
+
|
38 |
+
G = create_graph(entities, relationships)
|
39 |
+
|
40 |
+
if visualization_type == 'Bokeh':
|
41 |
+
fig = create_bokeh_plot(G, layout_type)
|
42 |
+
else:
|
43 |
+
fig = create_plotly_plot(G, layout_type)
|
44 |
+
|
45 |
+
output_text = f"Entities: {entities}\nRelationships: {relationships}\n\nRaw output:\n{prediction}"
|
46 |
+
return G, fig, output_text
|
47 |
+
except Exception as e:
|
48 |
+
print(f"Error in process_text: {str(e)}")
|
49 |
+
return None, None, f"An error occurred: {str(e)}"
|
50 |
+
|
51 |
+
def update_graph(G, layout_type, visualization_type):
|
52 |
+
if G is None:
|
53 |
+
return None, "Please process text first."
|
54 |
+
|
55 |
+
try:
|
56 |
+
if visualization_type == 'Bokeh':
|
57 |
+
fig = create_bokeh_plot(G, layout_type)
|
58 |
+
else:
|
59 |
+
fig = create_plotly_plot(G, layout_type)
|
60 |
+
return fig, ""
|
61 |
+
except Exception as e:
|
62 |
+
print(f"Error in update_graph: {e}")
|
63 |
+
return None, f"An error occurred while updating the graph: {str(e)}"
|
64 |
+
|
65 |
+
def update_inputs(sample_name):
|
66 |
+
sample = snippets[sample_name]
|
67 |
+
return sample.text_input, sample.entity_types, sample.predicates
|
68 |
+
|
69 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
70 |
+
gr.Markdown("# Knowledge Graph Extractor")
|
71 |
+
|
72 |
+
default_sample_name = random.choice(list(snippets.keys()))
|
73 |
+
default_sample = snippets[default_sample_name]
|
74 |
+
|
75 |
+
with gr.Row():
|
76 |
+
with gr.Column(scale=1):
|
77 |
+
sample_dropdown = gr.Dropdown(choices=list(snippets.keys()), label="Select Sample", value=default_sample_name)
|
78 |
+
input_text = gr.Textbox(label="Input Text", lines=5, value=default_sample.text_input)
|
79 |
+
entity_types = gr.Textbox(label="Entity Types", value=default_sample.entity_types)
|
80 |
+
predicates = gr.Textbox(label="Predicates", value=default_sample.predicates)
|
81 |
+
layout_type = gr.Dropdown(choices=['spring', 'fruchterman_reingold', 'circular', 'random', 'spectral', 'shell'],
|
82 |
+
label="Layout Type", value='spring')
|
83 |
+
visualization_type = gr.Radio(choices=['Bokeh', 'Plotly'], label="Visualization Type", value='Bokeh')
|
84 |
+
process_btn = gr.Button("Process Text")
|
85 |
+
with gr.Column(scale=2):
|
86 |
+
output_graph = gr.Plot(label="Knowledge Graph")
|
87 |
+
error_message = gr.Textbox(label="Textual Output")
|
88 |
+
|
89 |
+
graph_state = gr.State(None)
|
90 |
+
|
91 |
+
def process_and_update(text, entity_types, predicates, layout_type, visualization_type):
|
92 |
+
G, fig, output = process_text(text, entity_types, predicates, layout_type, visualization_type)
|
93 |
+
return G, fig, output
|
94 |
+
|
95 |
+
def update_graph_wrapper(G, layout_type, visualization_type):
|
96 |
+
if G is not None:
|
97 |
+
fig, _ = update_graph(G, layout_type, visualization_type)
|
98 |
+
return fig
|
99 |
+
|
100 |
+
sample_dropdown.change(update_inputs, inputs=[sample_dropdown], outputs=[input_text, entity_types, predicates])
|
101 |
+
|
102 |
+
process_btn.click(process_and_update,
|
103 |
+
inputs=[input_text, entity_types, predicates, layout_type, visualization_type],
|
104 |
+
outputs=[graph_state, output_graph, error_message])
|
105 |
+
|
106 |
+
layout_type.change(update_graph_wrapper,
|
107 |
+
inputs=[graph_state, layout_type, visualization_type],
|
108 |
+
outputs=[output_graph])
|
109 |
+
|
110 |
+
visualization_type.change(update_graph_wrapper,
|
111 |
+
inputs=[graph_state, layout_type, visualization_type],
|
112 |
+
outputs=[output_graph])
|
113 |
+
|
114 |
+
if __name__ == "__main__":
|
115 |
+
demo.launch(share=True)import random
|
116 |
+
|
117 |
+
import gradio as gr
|
118 |
+
import networkx as nx
|
119 |
+
|
120 |
+
from lib.graph_extract import triplextract, parse_triples
|
121 |
+
from lib.visualize import create_graph, create_bokeh_plot, create_plotly_plot
|
122 |
+
from lib.samples import snippets
|
123 |
+
|
124 |
+
WORD_LIMIT = 300
|
125 |
+
|
126 |
+
def process_text(text, entity_types, predicates, layout_type, visualization_type):
|
127 |
+
if not text:
|
128 |
+
return None, None, "Please enter some text."
|
129 |
+
|
130 |
+
words = text.split()
|
131 |
+
if len(words) > WORD_LIMIT:
|
132 |
+
return None, None, f"Please limit your input to {WORD_LIMIT} words. Current word count: {len(words)}"
|
133 |
+
|
134 |
+
entity_types = [et.strip() for et in entity_types.split(",") if et.strip()]
|
135 |
+
predicates = [p.strip() for p in predicates.split(",") if p.strip()]
|
136 |
+
|
137 |
+
if not entity_types:
|
138 |
+
return None, None, "Please enter at least one entity type."
|
139 |
+
if not predicates:
|
140 |
+
return None, None, "Please enter at least one predicate."
|
141 |
+
|
142 |
+
try:
|
143 |
+
prediction = triplextract(text, entity_types, predicates)
|
144 |
+
if prediction.startswith("Error"):
|
145 |
+
return None, None, prediction
|
146 |
+
|
147 |
+
entities, relationships = parse_triples(prediction)
|
148 |
+
|
149 |
+
if not entities and not relationships:
|
150 |
+
return None, None, "No entities or relationships found. Try different text or check your input."
|
151 |
+
|
152 |
+
G = create_graph(entities, relationships)
|
153 |
+
|
154 |
+
if visualization_type == 'Bokeh':
|
155 |
+
fig = create_bokeh_plot(G, layout_type)
|
156 |
+
else:
|
157 |
+
fig = create_plotly_plot(G, layout_type)
|
158 |
+
|
159 |
+
output_text = f"Entities: {entities}\nRelationships: {relationships}\n\nRaw output:\n{prediction}"
|
160 |
+
return G, fig, output_text
|
161 |
+
except Exception as e:
|
162 |
+
print(f"Error in process_text: {str(e)}")
|
163 |
+
return None, None, f"An error occurred: {str(e)}"
|
164 |
+
|
165 |
+
def update_graph(G, layout_type, visualization_type):
|
166 |
+
if G is None:
|
167 |
+
return None, "Please process text first."
|
168 |
+
|
169 |
+
try:
|
170 |
+
if visualization_type == 'Bokeh':
|
171 |
+
fig = create_bokeh_plot(G, layout_type)
|
172 |
+
else:
|
173 |
+
fig = create_plotly_plot(G, layout_type)
|
174 |
+
return fig, ""
|
175 |
+
except Exception as e:
|
176 |
+
print(f"Error in update_graph: {e}")
|
177 |
+
return None, f"An error occurred while updating the graph: {str(e)}"
|
178 |
+
|
179 |
+
def update_inputs(sample_name):
|
180 |
+
sample = snippets[sample_name]
|
181 |
+
return sample.text_input, sample.entity_types, sample.predicates
|
182 |
+
|
183 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
184 |
+
gr.Markdown("# Knowledge Graph Extractor")
|
185 |
+
|
186 |
+
default_sample_name = random.choice(list(snippets.keys()))
|
187 |
+
default_sample = snippets[default_sample_name]
|
188 |
+
|
189 |
+
with gr.Row():
|
190 |
+
with gr.Column(scale=1):
|
191 |
+
sample_dropdown = gr.Dropdown(choices=list(snippets.keys()), label="Select Sample", value=default_sample_name)
|
192 |
+
input_text = gr.Textbox(label="Input Text", lines=5, value=default_sample.text_input)
|
193 |
+
entity_types = gr.Textbox(label="Entity Types", value=default_sample.entity_types)
|
194 |
+
predicates = gr.Textbox(label="Predicates", value=default_sample.predicates)
|
195 |
+
layout_type = gr.Dropdown(choices=['spring', 'fruchterman_reingold', 'circular', 'random', 'spectral', 'shell'],
|
196 |
+
label="Layout Type", value='spring')
|
197 |
+
visualization_type = gr.Radio(choices=['Bokeh', 'Plotly'], label="Visualization Type", value='Bokeh')
|
198 |
+
process_btn = gr.Button("Process Text")
|
199 |
+
with gr.Column(scale=2):
|
200 |
+
output_graph = gr.Plot(label="Knowledge Graph")
|
201 |
+
error_message = gr.Textbox(label="Textual Output")
|
202 |
+
|
203 |
+
graph_state = gr.State(None)
|
204 |
+
|
205 |
+
def process_and_update(text, entity_types, predicates, layout_type, visualization_type):
|
206 |
+
G, fig, output = process_text(text, entity_types, predicates, layout_type, visualization_type)
|
207 |
+
return G, fig, output
|
208 |
+
|
209 |
+
def update_graph_wrapper(G, layout_type, visualization_type):
|
210 |
+
if G is not None:
|
211 |
+
fig, _ = update_graph(G, layout_type, visualization_type)
|
212 |
+
return fig
|
213 |
+
|
214 |
+
sample_dropdown.change(update_inputs, inputs=[sample_dropdown], outputs=[input_text, entity_types, predicates])
|
215 |
+
|
216 |
+
process_btn.click(process_and_update,
|
217 |
+
inputs=[input_text, entity_types, predicates, layout_type, visualization_type],
|
218 |
+
outputs=[graph_state, output_graph, error_message])
|
219 |
+
|
220 |
+
layout_type.change(update_graph_wrapper,
|
221 |
+
inputs=[graph_state, layout_type, visualization_type],
|
222 |
+
outputs=[output_graph])
|
223 |
+
|
224 |
+
visualization_type.change(update_graph_wrapper,
|
225 |
+
inputs=[graph_state, layout_type, visualization_type],
|
226 |
+
outputs=[output_graph])
|
227 |
+
|
228 |
+
if __name__ == "__main__":
|
229 |
+
demo.launch(share=True)
|