prithivMLmods commited on
Commit
90dd2c3
·
verified ·
1 Parent(s): 45359c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  from collections.abc import Iterator
3
  from threading import Thread
4
  import networkx as nx
5
- from pyvis.network import Network
6
  import gradio as gr
7
  import spaces
8
  import torch
@@ -37,10 +37,18 @@ def generate_knowledge_graph():
37
  G.add_edge("AI", "ML")
38
  G.add_edge("ML", "DL")
39
 
40
- # Visualize the graph using pyvis
41
- net = Network(notebook=True, cdn_resources='remote')
42
- net.from_nx(G)
43
- net.show("knowledge_graph.html")
 
 
 
 
 
 
 
 
44
 
45
  @spaces.GPU(duration=120)
46
  def generate(
@@ -89,8 +97,8 @@ def generate(
89
  # Ensure the thread is joined after completion
90
  t.join()
91
 
92
- # Generate the knowledge graph HTML file
93
- generate_knowledge_graph()
94
 
95
  demo = gr.ChatInterface(
96
  fn=generate,
@@ -130,7 +138,7 @@ demo = gr.ChatInterface(
130
  step=0.05,
131
  value=1.2,
132
  ),
133
- gr.HTML("<a href='knowledge_graph.html' target='_blank'>View Knowledge Graph</a>")
134
  ],
135
  stop_btn=None,
136
  examples=[
 
2
  from collections.abc import Iterator
3
  from threading import Thread
4
  import networkx as nx
5
+ import matplotlib.pyplot as plt
6
  import gradio as gr
7
  import spaces
8
  import torch
 
37
  G.add_edge("AI", "ML")
38
  G.add_edge("ML", "DL")
39
 
40
+ # Draw the graph using matplotlib
41
+ plt.figure(figsize=(8, 6))
42
+ pos = nx.spring_layout(G)
43
+ nx.draw(G, pos, with_labels=True, node_size=3000, node_color="lightblue", font_size=10, font_weight="bold")
44
+ plt.title("Knowledge Graph")
45
+
46
+ # Save the graph as a PDF
47
+ pdf_path = "knowledge_graph.pdf"
48
+ plt.savefig(pdf_path, format="pdf")
49
+ plt.close()
50
+
51
+ return pdf_path
52
 
53
  @spaces.GPU(duration=120)
54
  def generate(
 
97
  # Ensure the thread is joined after completion
98
  t.join()
99
 
100
+ # Generate the knowledge graph PDF file
101
+ pdf_path = generate_knowledge_graph()
102
 
103
  demo = gr.ChatInterface(
104
  fn=generate,
 
138
  step=0.05,
139
  value=1.2,
140
  ),
141
+ gr.File(label="Download Knowledge Graph (PDF)", value=pdf_path, visible=True),
142
  ],
143
  stop_btn=None,
144
  examples=[