Spaces:
Sleeping
Sleeping
Commit
·
9ec289c
1
Parent(s):
c2e3851
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,37 +8,92 @@ def generate_knowledge_graph(api_key, user_input):
|
|
| 8 |
|
| 9 |
# Chamar a API da OpenAI
|
| 10 |
print("Chamando a API da OpenAI...")
|
| 11 |
-
completion = openai.
|
| 12 |
model="gpt-3.5-turbo-16k",
|
| 13 |
messages=[
|
|
|
|
| 14 |
{
|
| 15 |
"role": "user",
|
| 16 |
"content": f"Help me understand following by describing as a detailed knowledge graph: {user_input}",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
}
|
| 18 |
-
]
|
|
|
|
| 19 |
)
|
| 20 |
-
|
| 21 |
-
print(
|
| 22 |
-
print(raw_response)
|
| 23 |
-
|
| 24 |
-
# Verificar se a resposta contém conteúdo
|
| 25 |
-
if 'content' in raw_response and raw_response['content']:
|
| 26 |
-
try:
|
| 27 |
-
response_data = json.loads(raw_response['content'])
|
| 28 |
-
except json.JSONDecodeError:
|
| 29 |
-
print("Erro ao decodificar o JSON.")
|
| 30 |
-
return "Erro ao decodificar o JSON."
|
| 31 |
-
else:
|
| 32 |
-
print("Resposta da API vazia ou inválida.")
|
| 33 |
-
return "Resposta da API vazia ou inválida."
|
| 34 |
|
| 35 |
# Visualizar o conhecimento usando Graphviz
|
| 36 |
print("Gerando o conhecimento usando Graphviz...")
|
| 37 |
dot = Digraph(comment="Knowledge Graph")
|
| 38 |
for node in response_data.get("nodes", []):
|
| 39 |
-
dot.node(node["id"], f"{node['label']} ({node['type']})")
|
| 40 |
for edge in response_data.get("edges", []):
|
| 41 |
-
dot.edge(edge["from"], edge["to"], label=edge["relationship"])
|
| 42 |
|
| 43 |
# Renderizar para o formato PNG
|
| 44 |
print("Renderizando o gráfico para o formato PNG...")
|
|
@@ -50,13 +105,13 @@ def generate_knowledge_graph(api_key, user_input):
|
|
| 50 |
return "knowledge_graph.png"
|
| 51 |
|
| 52 |
iface = gr.Interface(
|
| 53 |
-
fn=generate_knowledge_graph,
|
| 54 |
inputs=[
|
| 55 |
-
gr.
|
| 56 |
-
gr.
|
| 57 |
-
],
|
| 58 |
-
outputs=gr.
|
| 59 |
-
live=False
|
| 60 |
)
|
| 61 |
|
| 62 |
print("Iniciando a interface Gradio...")
|
|
|
|
| 8 |
|
| 9 |
# Chamar a API da OpenAI
|
| 10 |
print("Chamando a API da OpenAI...")
|
| 11 |
+
completion = openai.Completion.create(
|
| 12 |
model="gpt-3.5-turbo-16k",
|
| 13 |
messages=[
|
| 14 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 15 |
{
|
| 16 |
"role": "user",
|
| 17 |
"content": f"Help me understand following by describing as a detailed knowledge graph: {user_input}",
|
| 18 |
+
},
|
| 19 |
+
],
|
| 20 |
+
functions=[
|
| 21 |
+
{
|
| 22 |
+
"name": "knowledge_graph",
|
| 23 |
+
"description": "Generate a knowledge graph with entities and relationships. Use the colors to help differentiate between different node or edge types/categories. Always provide light pastel colors that work well with black font.",
|
| 24 |
+
"parameters": {
|
| 25 |
+
"type": "object",
|
| 26 |
+
"properties": {
|
| 27 |
+
"metadata": {
|
| 28 |
+
"type": "object",
|
| 29 |
+
"properties": {
|
| 30 |
+
"createdDate": {"type": "string"},
|
| 31 |
+
"lastUpdated": {"type": "string"},
|
| 32 |
+
"description": {"type": "string"},
|
| 33 |
+
},
|
| 34 |
+
},
|
| 35 |
+
"nodes": {
|
| 36 |
+
"type": "array",
|
| 37 |
+
"items": {
|
| 38 |
+
"type": "object",
|
| 39 |
+
"properties": {
|
| 40 |
+
"id": {"type": "string"},
|
| 41 |
+
"label": {"type": "string"},
|
| 42 |
+
"type": {"type": "string"},
|
| 43 |
+
"color": {"type": "string"}, # Added color property
|
| 44 |
+
"properties": {
|
| 45 |
+
"type": "object",
|
| 46 |
+
"description": "Additional attributes for the node",
|
| 47 |
+
},
|
| 48 |
+
},
|
| 49 |
+
"required": [
|
| 50 |
+
"id",
|
| 51 |
+
"label",
|
| 52 |
+
"type",
|
| 53 |
+
"color",
|
| 54 |
+
], # Added color to required
|
| 55 |
+
},
|
| 56 |
+
},
|
| 57 |
+
"edges": {
|
| 58 |
+
"type": "array",
|
| 59 |
+
"items": {
|
| 60 |
+
"type": "object",
|
| 61 |
+
"properties": {
|
| 62 |
+
"from": {"type": "string"},
|
| 63 |
+
"to": {"type": "string"},
|
| 64 |
+
"relationship": {"type": "string"},
|
| 65 |
+
"direction": {"type": "string"},
|
| 66 |
+
"color": {"type": "string"}, # Added color property
|
| 67 |
+
"properties": {
|
| 68 |
+
"type": "object",
|
| 69 |
+
"description": "Additional attributes for the edge",
|
| 70 |
+
},
|
| 71 |
+
},
|
| 72 |
+
"required": [
|
| 73 |
+
"from",
|
| 74 |
+
"to",
|
| 75 |
+
"relationship",
|
| 76 |
+
"color",
|
| 77 |
+
], # Added color to required
|
| 78 |
+
},
|
| 79 |
+
},
|
| 80 |
+
},
|
| 81 |
+
"required": ["nodes", "edges"],
|
| 82 |
+
},
|
| 83 |
}
|
| 84 |
+
],
|
| 85 |
+
response_format="json",
|
| 86 |
)
|
| 87 |
+
response_data = completion.choices[0]["message"]["function_results"]["knowledge_graph"]
|
| 88 |
+
print(response_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
# Visualizar o conhecimento usando Graphviz
|
| 91 |
print("Gerando o conhecimento usando Graphviz...")
|
| 92 |
dot = Digraph(comment="Knowledge Graph")
|
| 93 |
for node in response_data.get("nodes", []):
|
| 94 |
+
dot.node(node["id"], f"{node['label']} ({node['type']})", color=node.get("color", "lightblue"))
|
| 95 |
for edge in response_data.get("edges", []):
|
| 96 |
+
dot.edge(edge["from"], edge["to"], label=edge["relationship"], color=edge.get("color", "black"))
|
| 97 |
|
| 98 |
# Renderizar para o formato PNG
|
| 99 |
print("Renderizando o gráfico para o formato PNG...")
|
|
|
|
| 105 |
return "knowledge_graph.png"
|
| 106 |
|
| 107 |
iface = gr.Interface(
|
| 108 |
+
fn=generate_knowledge_graph,
|
| 109 |
inputs=[
|
| 110 |
+
gr.inputs.Textbox(label="OpenAI API Key", type="password"),
|
| 111 |
+
gr.inputs.Textbox(label="User Input for Graph"),
|
| 112 |
+
],
|
| 113 |
+
outputs=gr.outputs.Image(type="file", label="Generated Knowledge Graph"),
|
| 114 |
+
live=False,
|
| 115 |
)
|
| 116 |
|
| 117 |
print("Iniciando a interface Gradio...")
|