Spaces:
Sleeping
Sleeping
Commit
·
a4b8ea3
1
Parent(s):
28894d6
Update app.py
Browse files
app.py
CHANGED
|
@@ -94,19 +94,28 @@ def generate_knowledge_graph(api_key, user_input):
|
|
| 94 |
# Convert to dictionary if it's a string
|
| 95 |
if isinstance(response_data, str):
|
| 96 |
response_data = json.loads(response_data)
|
| 97 |
-
|
| 98 |
# Visualizar o conhecimento usando Graphviz
|
| 99 |
print("Gerando o conhecimento usando Graphviz...")
|
| 100 |
-
dot = Digraph(comment="Knowledge Graph")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
for node in response_data.get("nodes", []):
|
| 102 |
dot.node(node["id"], f"{node['label']} ({node['type']})", color=node.get("color", "lightblue"))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
for edge in response_data.get("edges", []):
|
| 104 |
dot.edge(edge["from"], edge["to"], label=edge["relationship"], color=edge.get("color", "black"))
|
| 105 |
|
| 106 |
# Renderizar para o formato PNG
|
| 107 |
print("Renderizando o gráfico para o formato PNG...")
|
| 108 |
-
|
| 109 |
-
image_data = dot.pipe(format="png")
|
| 110 |
image = Image.open(io.BytesIO(image_data))
|
| 111 |
|
| 112 |
print("Gráfico gerado com sucesso!")
|
|
|
|
| 94 |
# Convert to dictionary if it's a string
|
| 95 |
if isinstance(response_data, str):
|
| 96 |
response_data = json.loads(response_data)
|
| 97 |
+
|
| 98 |
# Visualizar o conhecimento usando Graphviz
|
| 99 |
print("Gerando o conhecimento usando Graphviz...")
|
| 100 |
+
dot = Digraph(comment="Knowledge Graph", format='png')
|
| 101 |
+
dot.attr(dpi='300')
|
| 102 |
+
dot.attr(bgcolor='transparent')
|
| 103 |
+
|
| 104 |
+
# Estilizar os nós
|
| 105 |
+
dot.attr('node', shape='box', style='filled', fillcolor='lightblue', fontcolor='black')
|
| 106 |
+
|
| 107 |
for node in response_data.get("nodes", []):
|
| 108 |
dot.node(node["id"], f"{node['label']} ({node['type']})", color=node.get("color", "lightblue"))
|
| 109 |
+
|
| 110 |
+
# Estilizar as arestas
|
| 111 |
+
dot.attr('edge', color='black', fontcolor='black')
|
| 112 |
+
|
| 113 |
for edge in response_data.get("edges", []):
|
| 114 |
dot.edge(edge["from"], edge["to"], label=edge["relationship"], color=edge.get("color", "black"))
|
| 115 |
|
| 116 |
# Renderizar para o formato PNG
|
| 117 |
print("Renderizando o gráfico para o formato PNG...")
|
| 118 |
+
image_data = dot.pipe()
|
|
|
|
| 119 |
image = Image.open(io.BytesIO(image_data))
|
| 120 |
|
| 121 |
print("Gráfico gerado com sucesso!")
|