Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
from pyvis.network import Network
|
3 |
-
import base64
|
4 |
|
5 |
def create_knowledge_graph():
|
6 |
# Создаем граф с pyvis
|
@@ -9,14 +8,14 @@ def create_knowledge_graph():
|
|
9 |
width="100%",
|
10 |
bgcolor="#222222",
|
11 |
font_color="white",
|
12 |
-
cdn_resources="in_line" #
|
13 |
)
|
14 |
-
|
15 |
# Добавляем узлы (концепции)
|
16 |
nodes = ["Python", "Gradio", "Pyvis", "Data Science", "Machine Learning"]
|
17 |
for node in nodes:
|
18 |
net.add_node(node, label=node)
|
19 |
-
|
20 |
# Добавляем ребра (связи между концепциями)
|
21 |
edges = [
|
22 |
("Python", "Gradio"),
|
@@ -28,23 +27,38 @@ def create_knowledge_graph():
|
|
28 |
]
|
29 |
for source, target in edges:
|
30 |
net.add_edge(source, target)
|
31 |
-
|
32 |
# Генерируем HTML-код графа
|
33 |
html_content = net.generate_html(notebook=False)
|
34 |
-
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
with gr.Blocks() as demo:
|
44 |
-
gr.Markdown("#
|
|
|
|
|
45 |
generate_button = gr.Button("Создать граф")
|
46 |
-
html_out = gr.HTML()
|
47 |
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
demo.launch(
|
|
|
1 |
import gradio as gr
|
2 |
from pyvis.network import Network
|
|
|
3 |
|
4 |
def create_knowledge_graph():
|
5 |
# Создаем граф с pyvis
|
|
|
8 |
width="100%",
|
9 |
bgcolor="#222222",
|
10 |
font_color="white",
|
11 |
+
cdn_resources="in_line" # Встраиваем JS/CSS в HTML
|
12 |
)
|
13 |
+
|
14 |
# Добавляем узлы (концепции)
|
15 |
nodes = ["Python", "Gradio", "Pyvis", "Data Science", "Machine Learning"]
|
16 |
for node in nodes:
|
17 |
net.add_node(node, label=node)
|
18 |
+
|
19 |
# Добавляем ребра (связи между концепциями)
|
20 |
edges = [
|
21 |
("Python", "Gradio"),
|
|
|
27 |
]
|
28 |
for source, target in edges:
|
29 |
net.add_edge(source, target)
|
30 |
+
|
31 |
# Генерируем HTML-код графа
|
32 |
html_content = net.generate_html(notebook=False)
|
33 |
+
|
34 |
+
# Экранируем двойные кавычки для корректной вставки в srcdoc
|
35 |
+
html_content_escaped = html_content.replace('"', '"')
|
36 |
+
|
37 |
+
# Оборачиваем HTML в iframe
|
38 |
+
iframe_code = f'''
|
39 |
+
<iframe
|
40 |
+
srcdoc="{html_content_escaped}"
|
41 |
+
width="100%"
|
42 |
+
height="600"
|
43 |
+
frameborder="0"
|
44 |
+
></iframe>
|
45 |
+
'''
|
46 |
+
return iframe_code
|
47 |
|
48 |
with gr.Blocks() as demo:
|
49 |
+
gr.Markdown("# Граф Знаний с Pyvis и Gradio (Blocks)")
|
50 |
+
|
51 |
+
# Кнопка для генерации графа
|
52 |
generate_button = gr.Button("Создать граф")
|
|
|
53 |
|
54 |
+
# Выводим результат в виде HTML
|
55 |
+
graph_html = gr.HTML()
|
56 |
+
|
57 |
+
# Связываем кнопку с функцией
|
58 |
+
generate_button.click(
|
59 |
+
fn=create_knowledge_graph,
|
60 |
+
inputs=[],
|
61 |
+
outputs=graph_html
|
62 |
+
)
|
63 |
|
64 |
+
demo.launch()
|