File size: 5,100 Bytes
8ea927a
 
410031a
 
41813c2
 
8ea927a
410031a
8ea927a
410031a
77f6b05
2fce835
9ec289c
b31a1e4
 
9ec289c
b31a1e4
 
 
9ec289c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b31a1e4
9ec289c
 
8ea927a
9ec289c
 
2fce835
77f6b05
2fce835
410031a
77f6b05
9ec289c
77f6b05
9ec289c
410031a
77f6b05
2fce835
410031a
41813c2
 
410031a
2fce835
 
41813c2
8ea927a
 
9ec289c
410031a
9ec289c
 
 
4cc95b5
9ec289c
8ea927a
 
2fce835
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import gradio as gr
import openai
import json
from graphviz import Digraph
from PIL import Image
import io

def generate_knowledge_graph(api_key, user_input):
    openai.api_key = api_key

    # Chamar a API da OpenAI
    print("Chamando a API da OpenAI...")
    completion = openai.Completion.create(
        model="gpt-3.5-turbo-16k",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {
                "role": "user",
                "content": f"Help me understand following by describing as a detailed knowledge graph: {user_input}",
            },
        ],
        functions=[
            {
                "name": "knowledge_graph",
                "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.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "metadata": {
                            "type": "object",
                            "properties": {
                                "createdDate": {"type": "string"},
                                "lastUpdated": {"type": "string"},
                                "description": {"type": "string"},
                            },
                        },
                        "nodes": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "id": {"type": "string"},
                                    "label": {"type": "string"},
                                    "type": {"type": "string"},
                                    "color": {"type": "string"},  # Added color property
                                    "properties": {
                                        "type": "object",
                                        "description": "Additional attributes for the node",
                                    },
                                },
                                "required": [
                                    "id",
                                    "label",
                                    "type",
                                    "color",
                                ],  # Added color to required
                            },
                        },
                        "edges": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "from": {"type": "string"},
                                    "to": {"type": "string"},
                                    "relationship": {"type": "string"},
                                    "direction": {"type": "string"},
                                    "color": {"type": "string"},  # Added color property
                                    "properties": {
                                        "type": "object",
                                        "description": "Additional attributes for the edge",
                                    },
                                },
                                "required": [
                                    "from",
                                    "to",
                                    "relationship",
                                    "color",
                                ],  # Added color to required
                            },
                        },
                    },
                    "required": ["nodes", "edges"],
                },
            }
        ],
        response_format="json",
    )
    response_data = completion.choices[0]["message"]["function_results"]["knowledge_graph"]
    print(response_data)

    # Visualizar o conhecimento usando Graphviz
    print("Gerando o conhecimento usando Graphviz...")
    dot = Digraph(comment="Knowledge Graph")
    for node in response_data.get("nodes", []):
        dot.node(node["id"], f"{node['label']} ({node['type']})", color=node.get("color", "lightblue"))
    for edge in response_data.get("edges", []):
        dot.edge(edge["from"], edge["to"], label=edge["relationship"], color=edge.get("color", "black"))

    # Renderizar para o formato PNG
    print("Renderizando o gráfico para o formato PNG...")
    dot.format = "png"
    image_data = dot.pipe(format="png")
    image = Image.open(io.BytesIO(image_data))

    print("Gráfico gerado com sucesso!")

    return image

iface = gr.Interface(
    fn=generate_knowledge_graph,
    inputs=[
        gr.inputs.Textbox(label="OpenAI API Key", type="password"),
        gr.inputs.Textbox(label="User Input for Graph"),
    ],
    outputs=gr.outputs.Image(type="pil", label="Generated Knowledge Graph"),
    live=False,
)

print("Iniciando a interface Gradio...")
iface.launch()