Spaces:
Sleeping
Sleeping
artificialguybr
commited on
Commit
•
77f6b05
1
Parent(s):
b31a1e4
Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,11 @@ import gradio as gr
|
|
2 |
import openai
|
3 |
import json
|
4 |
from graphviz import Digraph
|
5 |
-
import base64
|
6 |
-
from PIL import Image
|
7 |
|
8 |
def generate_knowledge_graph(api_key, user_input):
|
9 |
-
print("Setting OpenAI API key...")
|
10 |
openai.api_key = api_key
|
11 |
|
12 |
-
|
13 |
completion = openai.ChatCompletion.create(
|
14 |
model="gpt-3.5-turbo-16k",
|
15 |
messages=[
|
@@ -17,65 +14,32 @@ def generate_knowledge_graph(api_key, user_input):
|
|
17 |
"role": "user",
|
18 |
"content": f"Help me understand following by describing as a detailed knowledge graph: {user_input}",
|
19 |
}
|
20 |
-
]
|
21 |
-
functions=[
|
22 |
-
{
|
23 |
-
"name": "knowledge_graph",
|
24 |
-
"description": "Generate a knowledge graph with entities and relationships.",
|
25 |
-
"parameters": {
|
26 |
-
"type": "object",
|
27 |
-
"properties": {
|
28 |
-
"metadata": {"type": "object"},
|
29 |
-
"nodes": {"type": "array"},
|
30 |
-
"edges": {"type": "array"}
|
31 |
-
},
|
32 |
-
"required": ["nodes", "edges"]
|
33 |
-
}
|
34 |
-
}
|
35 |
-
],
|
36 |
-
function_call={"name": "knowledge_graph"}
|
37 |
)
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
response_data = completion.choices[0]["message"]["function_call"]["arguments"]
|
41 |
-
print(f"Response data: {response_data}")
|
42 |
-
|
43 |
-
print("Converting response to JSON...")
|
44 |
-
response_dict = json.loads(response_data)
|
45 |
-
|
46 |
-
print("Generating knowledge graph using Graphviz...")
|
47 |
dot = Digraph(comment="Knowledge Graph")
|
48 |
-
|
49 |
-
# Add nodes to the graph
|
50 |
-
for node in response_dict.get("nodes", []):
|
51 |
dot.node(node["id"], f"{node['label']} ({node['type']})")
|
52 |
-
|
53 |
-
# Add edges to the graph
|
54 |
-
for edge in response_dict.get("edges", []):
|
55 |
dot.edge(edge["from"], edge["to"], label=edge["relationship"])
|
56 |
|
57 |
-
#
|
58 |
-
print("Rendering graph to PNG format...")
|
59 |
dot.format = "png"
|
60 |
dot.render(filename="knowledge_graph", cleanup=True)
|
61 |
|
62 |
-
|
63 |
-
print("Converting PNG to base64...")
|
64 |
-
with open("knowledge_graph.png", "rb") as img_file:
|
65 |
-
img_base64 = base64.b64encode(img_file.read()).decode()
|
66 |
-
|
67 |
-
print("Returning base64 image to Gradio interface.")
|
68 |
-
return f"data:image/png;base64,{img_base64}"
|
69 |
|
70 |
iface = gr.Interface(
|
71 |
-
fn=generate_knowledge_graph,
|
72 |
inputs=[
|
73 |
gr.inputs.Textbox(label="OpenAI API Key", type="password"),
|
74 |
-
gr.inputs.Textbox(label="
|
75 |
-
],
|
76 |
-
outputs=gr.outputs.Image(
|
77 |
live=False
|
78 |
)
|
79 |
|
80 |
-
print("Launching Gradio interface...")
|
81 |
iface.launch()
|
|
|
2 |
import openai
|
3 |
import json
|
4 |
from graphviz import Digraph
|
|
|
|
|
5 |
|
6 |
def generate_knowledge_graph(api_key, user_input):
|
|
|
7 |
openai.api_key = api_key
|
8 |
|
9 |
+
# Chamar a API da OpenAI
|
10 |
completion = openai.ChatCompletion.create(
|
11 |
model="gpt-3.5-turbo-16k",
|
12 |
messages=[
|
|
|
14 |
"role": "user",
|
15 |
"content": f"Help me understand following by describing as a detailed knowledge graph: {user_input}",
|
16 |
}
|
17 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
)
|
19 |
+
response_data = completion.choices[0].message.to_dict()
|
20 |
+
response_data = json.loads(response_data['content'])
|
21 |
|
22 |
+
# Visualizar o conhecimento usando Graphviz
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
dot = Digraph(comment="Knowledge Graph")
|
24 |
+
for node in response_data.get("nodes", []):
|
|
|
|
|
25 |
dot.node(node["id"], f"{node['label']} ({node['type']})")
|
26 |
+
for edge in response_data.get("edges", []):
|
|
|
|
|
27 |
dot.edge(edge["from"], edge["to"], label=edge["relationship"])
|
28 |
|
29 |
+
# Renderizar para o formato PNG
|
|
|
30 |
dot.format = "png"
|
31 |
dot.render(filename="knowledge_graph", cleanup=True)
|
32 |
|
33 |
+
return "knowledge_graph.png"
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
iface = gr.Interface(
|
36 |
+
fn=generate_knowledge_graph,
|
37 |
inputs=[
|
38 |
gr.inputs.Textbox(label="OpenAI API Key", type="password"),
|
39 |
+
gr.inputs.Textbox(label="User Input for Graph")
|
40 |
+
],
|
41 |
+
outputs=gr.outputs.Image(label="Generated Knowledge Graph"),
|
42 |
live=False
|
43 |
)
|
44 |
|
|
|
45 |
iface.launch()
|