Spaces:
Running
Running
Krish Patel
commited on
Commit
·
1c838ea
1
Parent(s):
98e05ff
Enhanced the knowledge graph
Browse files- __pycache__/final.cpython-312.pyc +0 -0
- app.py +243 -16
__pycache__/final.cpython-312.pyc
ADDED
Binary file (9.75 kB). View file
|
|
app.py
CHANGED
@@ -32,6 +32,92 @@ def initialize_models():
|
|
32 |
class NewsInput(BaseModel):
|
33 |
text: str
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
def generate_knowledge_graph_viz(text, nlp, tokenizer, model):
|
36 |
kg_builder = KnowledgeGraphBuilder()
|
37 |
|
@@ -42,18 +128,29 @@ def generate_knowledge_graph_viz(text, nlp, tokenizer, model):
|
|
42 |
# Update knowledge graph
|
43 |
kg_builder.update_knowledge_graph(text, not is_fake, nlp)
|
44 |
|
45 |
-
#
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
# Create a new graph with selected edges
|
50 |
selected_graph = nx.DiGraph()
|
51 |
selected_graph.add_nodes_from(kg_builder.knowledge_graph.nodes(data=True))
|
52 |
-
selected_graph.add_edges_from(
|
53 |
|
54 |
pos = nx.spring_layout(selected_graph)
|
55 |
|
56 |
-
|
|
|
57 |
x=[], y=[],
|
58 |
line=dict(
|
59 |
width=2,
|
@@ -63,14 +160,136 @@ def generate_knowledge_graph_viz(text, nlp, tokenizer, model):
|
|
63 |
mode='lines'
|
64 |
)
|
65 |
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
x=[], y=[],
|
71 |
line=dict(
|
72 |
-
width=2,
|
73 |
-
color='rgba(255,0,0,0.7)' if is_fake else 'rgba(0,255,0,0.7)'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
),
|
75 |
hoverinfo='none',
|
76 |
mode='lines'
|
@@ -89,22 +308,30 @@ def generate_knowledge_graph_viz(text, nlp, tokenizer, model):
|
|
89 |
text=[]
|
90 |
)
|
91 |
|
92 |
-
# Add edges
|
93 |
-
for edge in
|
94 |
x0, y0 = pos[edge[0]]
|
95 |
x1, y1 = pos[edge[1]]
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
# Add nodes
|
100 |
-
for node in
|
101 |
x, y = pos[node]
|
102 |
node_trace['x'] += (x,)
|
103 |
node_trace['y'] += (y,)
|
104 |
node_trace['text'] += (node,)
|
105 |
|
106 |
fig = go.Figure(
|
107 |
-
data=[
|
108 |
layout=go.Layout(
|
109 |
showlegend=False,
|
110 |
hovermode='closest',
|
|
|
32 |
class NewsInput(BaseModel):
|
33 |
text: str
|
34 |
|
35 |
+
# def generate_knowledge_graph_viz(text, nlp, tokenizer, model):
|
36 |
+
# kg_builder = KnowledgeGraphBuilder()
|
37 |
+
|
38 |
+
# # Get prediction
|
39 |
+
# prediction, _ = predict_with_model(text, tokenizer, model)
|
40 |
+
# is_fake = prediction == "FAKE"
|
41 |
+
|
42 |
+
# # Update knowledge graph
|
43 |
+
# kg_builder.update_knowledge_graph(text, not is_fake, nlp)
|
44 |
+
|
45 |
+
# # Randomly select subset of edges (e.g. 10% of edges)
|
46 |
+
# edges = list(kg_builder.knowledge_graph.edges())
|
47 |
+
# selected_edges = random.sample(edges, k=int(len(edges) * 0.3))
|
48 |
+
|
49 |
+
# # Create a new graph with selected edges
|
50 |
+
# selected_graph = nx.DiGraph()
|
51 |
+
# selected_graph.add_nodes_from(kg_builder.knowledge_graph.nodes(data=True))
|
52 |
+
# selected_graph.add_edges_from(selected_edges)
|
53 |
+
|
54 |
+
# pos = nx.spring_layout(selected_graph)
|
55 |
+
|
56 |
+
# edge_trace = go.Scatter(
|
57 |
+
# x=[], y=[],
|
58 |
+
# line=dict(
|
59 |
+
# width=2,
|
60 |
+
# color='rgba(255,0,0,0.7)' if is_fake else 'rgba(0,255,0,0.7)'
|
61 |
+
# ),
|
62 |
+
# hoverinfo='none',
|
63 |
+
# mode='lines'
|
64 |
+
# )
|
65 |
+
|
66 |
+
# # Create visualization
|
67 |
+
# pos = nx.spring_layout(kg_builder.knowledge_graph)
|
68 |
+
|
69 |
+
# edge_trace = go.Scatter(
|
70 |
+
# x=[], y=[],
|
71 |
+
# line=dict(
|
72 |
+
# width=2,
|
73 |
+
# color='rgba(255,0,0,0.7)' if is_fake else 'rgba(0,255,0,0.7)'
|
74 |
+
# ),
|
75 |
+
# hoverinfo='none',
|
76 |
+
# mode='lines'
|
77 |
+
# )
|
78 |
+
|
79 |
+
# node_trace = go.Scatter(
|
80 |
+
# x=[], y=[],
|
81 |
+
# mode='markers+text',
|
82 |
+
# hoverinfo='text',
|
83 |
+
# textposition='top center',
|
84 |
+
# marker=dict(
|
85 |
+
# size=15,
|
86 |
+
# color='white',
|
87 |
+
# line=dict(width=2, color='black')
|
88 |
+
# ),
|
89 |
+
# text=[]
|
90 |
+
# )
|
91 |
+
|
92 |
+
# # Add edges
|
93 |
+
# for edge in selected_graph.edges():
|
94 |
+
# x0, y0 = pos[edge[0]]
|
95 |
+
# x1, y1 = pos[edge[1]]
|
96 |
+
# edge_trace['x'] += (x0, x1, None)
|
97 |
+
# edge_trace['y'] += (y0, y1, None)
|
98 |
+
|
99 |
+
# # Add nodes
|
100 |
+
# for node in kg_builder.knowledge_graph.nodes():
|
101 |
+
# x, y = pos[node]
|
102 |
+
# node_trace['x'] += (x,)
|
103 |
+
# node_trace['y'] += (y,)
|
104 |
+
# node_trace['text'] += (node,)
|
105 |
+
|
106 |
+
# fig = go.Figure(
|
107 |
+
# data=[edge_trace, node_trace],
|
108 |
+
# layout=go.Layout(
|
109 |
+
# showlegend=False,
|
110 |
+
# hovermode='closest',
|
111 |
+
# margin=dict(b=0,l=0,r=0,t=0),
|
112 |
+
# xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
113 |
+
# yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
114 |
+
# plot_bgcolor='rgba(0,0,0,0)',
|
115 |
+
# paper_bgcolor='rgba(0,0,0,0)'
|
116 |
+
# )
|
117 |
+
# )
|
118 |
+
|
119 |
+
# return fig
|
120 |
+
|
121 |
def generate_knowledge_graph_viz(text, nlp, tokenizer, model):
|
122 |
kg_builder = KnowledgeGraphBuilder()
|
123 |
|
|
|
128 |
# Update knowledge graph
|
129 |
kg_builder.update_knowledge_graph(text, not is_fake, nlp)
|
130 |
|
131 |
+
# Get all edges from the knowledge graph
|
132 |
+
all_edges = list(kg_builder.knowledge_graph.edges())
|
133 |
+
total_edges = len(all_edges)
|
134 |
+
|
135 |
+
# Select only 50% of edges to display
|
136 |
+
display_edge_count = int(total_edges * 0.5)
|
137 |
+
display_edges = random.sample(all_edges, k=min(display_edge_count, total_edges))
|
138 |
+
|
139 |
+
# Determine how many edges should be the opposite color (15% of displayed edges)
|
140 |
+
opposite_color_count = int(len(display_edges) * 0.15)
|
141 |
+
|
142 |
+
# Randomly select which edges will have the opposite color
|
143 |
+
opposite_color_edges = set(random.sample(display_edges, k=opposite_color_count))
|
144 |
|
145 |
# Create a new graph with selected edges
|
146 |
selected_graph = nx.DiGraph()
|
147 |
selected_graph.add_nodes_from(kg_builder.knowledge_graph.nodes(data=True))
|
148 |
+
selected_graph.add_edges_from(display_edges)
|
149 |
|
150 |
pos = nx.spring_layout(selected_graph)
|
151 |
|
152 |
+
# Create two edge traces - one for dominant color, one for opposite color
|
153 |
+
dominant_edge_trace = go.Scatter(
|
154 |
x=[], y=[],
|
155 |
line=dict(
|
156 |
width=2,
|
|
|
160 |
mode='lines'
|
161 |
)
|
162 |
|
163 |
+
opposite_edge_trace = go.Scatter(
|
164 |
+
x=[], y=[],
|
165 |
+
line=dict(
|
166 |
+
width=2,
|
167 |
+
color='rgba(0,255,0,0.7)' if is_fake else 'rgba(255,0,0,0.7)'
|
168 |
+
),
|
169 |
+
hoverinfo='none',
|
170 |
+
mode='lines'
|
171 |
+
)
|
172 |
|
173 |
+
node_trace = go.Scatter(
|
174 |
+
x=[], y=[],
|
175 |
+
mode='markers+text',
|
176 |
+
hoverinfo='text',
|
177 |
+
textposition='top center',
|
178 |
+
marker=dict(
|
179 |
+
size=15,
|
180 |
+
color='white',
|
181 |
+
line=dict(width=2, color='black')
|
182 |
+
),
|
183 |
+
text=[]
|
184 |
+
)
|
185 |
+
|
186 |
+
# Add edges with appropriate colors
|
187 |
+
for edge in display_edges:
|
188 |
+
x0, y0 = pos[edge[0]]
|
189 |
+
x1, y1 = pos[edge[1]]
|
190 |
+
|
191 |
+
if edge in opposite_color_edges:
|
192 |
+
opposite_edge_trace['x'] += (x0, x1, None)
|
193 |
+
opposite_edge_trace['y'] += (y0, y1, None)
|
194 |
+
else:
|
195 |
+
dominant_edge_trace['x'] += (x0, x1, None)
|
196 |
+
dominant_edge_trace['y'] += (y0, y1, None)
|
197 |
+
|
198 |
+
# Add nodes
|
199 |
+
for node in selected_graph.nodes():
|
200 |
+
x, y = pos[node]
|
201 |
+
node_trace['x'] += (x,)
|
202 |
+
node_trace['y'] += (y,)
|
203 |
+
node_trace['text'] += (node,)
|
204 |
+
|
205 |
+
fig = go.Figure(
|
206 |
+
data=[dominant_edge_trace, opposite_edge_trace, node_trace],
|
207 |
+
layout=go.Layout(
|
208 |
+
showlegend=False,
|
209 |
+
hovermode='closest',
|
210 |
+
margin=dict(b=0,l=0,r=0,t=0),
|
211 |
+
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
212 |
+
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
213 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
214 |
+
paper_bgcolor='rgba(0,0,0,0)'
|
215 |
+
)
|
216 |
+
)
|
217 |
+
|
218 |
+
return fig
|
219 |
+
|
220 |
+
def generate_knowledge_graph_viz(text, nlp, tokenizer, model):
|
221 |
+
kg_builder = KnowledgeGraphBuilder()
|
222 |
+
|
223 |
+
# Get prediction
|
224 |
+
prediction, _ = predict_with_model(text, tokenizer, model)
|
225 |
+
is_fake = prediction == "FAKE"
|
226 |
+
|
227 |
+
# Update knowledge graph
|
228 |
+
kg_builder.update_knowledge_graph(text, not is_fake, nlp)
|
229 |
+
|
230 |
+
# Get all edges from the knowledge graph
|
231 |
+
all_edges = list(kg_builder.knowledge_graph.edges())
|
232 |
+
total_edges = len(all_edges)
|
233 |
+
|
234 |
+
# Select only 60% of edges to display (0.3 + 0.15 + 0.15)
|
235 |
+
display_edge_count = int(total_edges * 0.6)
|
236 |
+
display_edges = random.sample(all_edges, k=min(display_edge_count, total_edges))
|
237 |
+
|
238 |
+
# Determine edge counts for each color
|
239 |
+
primary_color_count = int(total_edges * 0.3) # 30% primary color (green for real, red for fake)
|
240 |
+
opposite_color_count = int(total_edges * 0.15) # 15% opposite color
|
241 |
+
orange_color_count = int(total_edges * 0.15) # 15% orange
|
242 |
+
|
243 |
+
# Ensure we don't exceed the number of display edges
|
244 |
+
total_colored = primary_color_count + opposite_color_count + orange_color_count
|
245 |
+
if total_colored > len(display_edges):
|
246 |
+
ratio = len(display_edges) / total_colored
|
247 |
+
primary_color_count = int(primary_color_count * ratio)
|
248 |
+
opposite_color_count = int(opposite_color_count * ratio)
|
249 |
+
orange_color_count = int(orange_color_count * ratio)
|
250 |
+
|
251 |
+
# Shuffle display edges to ensure random distribution
|
252 |
+
random.shuffle(display_edges)
|
253 |
+
|
254 |
+
# Assign colors to edges
|
255 |
+
primary_color_edges = set(display_edges[:primary_color_count])
|
256 |
+
opposite_color_edges = set(display_edges[primary_color_count:primary_color_count+opposite_color_count])
|
257 |
+
orange_color_edges = set(display_edges[primary_color_count+opposite_color_count:
|
258 |
+
primary_color_count+opposite_color_count+orange_color_count])
|
259 |
+
|
260 |
+
# Create a new graph with selected edges
|
261 |
+
selected_graph = nx.DiGraph()
|
262 |
+
selected_graph.add_nodes_from(kg_builder.knowledge_graph.nodes(data=True))
|
263 |
+
selected_graph.add_edges_from(display_edges)
|
264 |
+
|
265 |
+
pos = nx.spring_layout(selected_graph)
|
266 |
+
|
267 |
+
# Create three edge traces - primary, opposite, and orange
|
268 |
+
primary_edge_trace = go.Scatter(
|
269 |
x=[], y=[],
|
270 |
line=dict(
|
271 |
+
width=2,
|
272 |
+
color='rgba(255,0,0,0.7)' if is_fake else 'rgba(0,255,0,0.7)' # Red if fake, green if real
|
273 |
+
),
|
274 |
+
hoverinfo='none',
|
275 |
+
mode='lines'
|
276 |
+
)
|
277 |
+
|
278 |
+
opposite_edge_trace = go.Scatter(
|
279 |
+
x=[], y=[],
|
280 |
+
line=dict(
|
281 |
+
width=2,
|
282 |
+
color='rgba(0,255,0,0.7)' if is_fake else 'rgba(255,0,0,0.7)' # Green if fake, red if real
|
283 |
+
),
|
284 |
+
hoverinfo='none',
|
285 |
+
mode='lines'
|
286 |
+
)
|
287 |
+
|
288 |
+
orange_edge_trace = go.Scatter(
|
289 |
+
x=[], y=[],
|
290 |
+
line=dict(
|
291 |
+
width=2,
|
292 |
+
color='rgba(255,165,0,0.7)' # Orange
|
293 |
),
|
294 |
hoverinfo='none',
|
295 |
mode='lines'
|
|
|
308 |
text=[]
|
309 |
)
|
310 |
|
311 |
+
# Add edges with appropriate colors
|
312 |
+
for edge in display_edges:
|
313 |
x0, y0 = pos[edge[0]]
|
314 |
x1, y1 = pos[edge[1]]
|
315 |
+
|
316 |
+
if edge in primary_color_edges:
|
317 |
+
primary_edge_trace['x'] += (x0, x1, None)
|
318 |
+
primary_edge_trace['y'] += (y0, y1, None)
|
319 |
+
elif edge in opposite_color_edges:
|
320 |
+
opposite_edge_trace['x'] += (x0, x1, None)
|
321 |
+
opposite_edge_trace['y'] += (y0, y1, None)
|
322 |
+
elif edge in orange_color_edges:
|
323 |
+
orange_edge_trace['x'] += (x0, x1, None)
|
324 |
+
orange_edge_trace['y'] += (y0, y1, None)
|
325 |
|
326 |
# Add nodes
|
327 |
+
for node in selected_graph.nodes():
|
328 |
x, y = pos[node]
|
329 |
node_trace['x'] += (x,)
|
330 |
node_trace['y'] += (y,)
|
331 |
node_trace['text'] += (node,)
|
332 |
|
333 |
fig = go.Figure(
|
334 |
+
data=[primary_edge_trace, opposite_edge_trace, orange_edge_trace, node_trace],
|
335 |
layout=go.Layout(
|
336 |
showlegend=False,
|
337 |
hovermode='closest',
|