File size: 1,376 Bytes
8ea927a
 
410031a
 
8ea927a
410031a
8ea927a
410031a
77f6b05
b31a1e4
 
 
 
 
 
 
77f6b05
8ea927a
77f6b05
 
8ea927a
77f6b05
410031a
77f6b05
410031a
77f6b05
410031a
 
77f6b05
410031a
 
 
77f6b05
8ea927a
 
77f6b05
410031a
 
77f6b05
 
 
410031a
8ea927a
 
410031a
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
import gradio as gr
import openai
import json
from graphviz import Digraph

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

    # Chamar a API da OpenAI
    completion = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-16k",
        messages=[
            {
                "role": "user",
                "content": f"Help me understand following by describing as a detailed knowledge graph: {user_input}",
            }
        ]
    )
    response_data = completion.choices[0].message.to_dict()
    response_data = json.loads(response_data['content'])

    # Visualizar 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']})")
    for edge in response_data.get("edges", []):
        dot.edge(edge["from"], edge["to"], label=edge["relationship"])

    # Renderizar para o formato PNG
    dot.format = "png"
    dot.render(filename="knowledge_graph", cleanup=True)

    return "knowledge_graph.png"

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(label="Generated Knowledge Graph"),
    live=False
)

iface.launch()