artificialguybr commited on
Commit
410031a
·
1 Parent(s): 136194b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -85
app.py CHANGED
@@ -1,18 +1,17 @@
1
  import gradio as gr
2
- import os
3
  import openai
 
 
 
 
 
 
 
4
 
5
- # Defina sua chave da API OpenAI
6
- api_key = None
7
-
8
- def create_knowledge_graph(user_input):
9
- global api_key
10
- if not api_key:
11
- return "Por favor, insira sua chave da API OpenAI."
12
-
13
- # Configurar a chamada para a API OpenAI
14
  openai.api_key = api_key
15
- response = openai.ChatCompletion.create(
 
16
  model="gpt-3.5-turbo-16k",
17
  messages=[
18
  {
@@ -20,84 +19,40 @@ def create_knowledge_graph(user_input):
20
  "content": f"Help me understand following by describing as a detailed knowledge graph: {user_input}",
21
  }
22
  ],
23
- functions=[
24
- {
25
- "name": "knowledge_graph",
26
- "description": "Generate a knowledge graph with entities and relationships...",
27
- "parameters": {
28
- "type": "object",
29
- "properties": {
30
- "metadata": {
31
- "type": "object",
32
- "properties": {
33
- "createdDate": {"type": "string"},
34
- "lastUpdated": {"type": "string"},
35
- "description": {"type": "string"},
36
- },
37
- },
38
- "nodes": {
39
- "type": "array",
40
- "items": {
41
- "type": "object",
42
- "properties": {
43
- "id": {"type": "string"},
44
- "label": {"type": "string"},
45
- "type": {"type": "string"},
46
- "color": {"type": "string"},
47
- "properties": {
48
- "type": "object",
49
- "description": "Additional attributes for the node",
50
- },
51
- },
52
- "required": [
53
- "id",
54
- "label",
55
- "type",
56
- "color",
57
- ],
58
- },
59
- },
60
- "edges": {
61
- "type": "array",
62
- "items": {
63
- "type": "object",
64
- "properties": {
65
- "from": {"type": "string"},
66
- "to": {"type": "string"},
67
- "relationship": {"type": "string"},
68
- "direction": {"type": "string"},
69
- "color": {"type": "string"},
70
- "properties": {
71
- "type": "object",
72
- "description": "Additional attributes for the edge",
73
- },
74
- },
75
- "required": [
76
- "from",
77
- "to",
78
- "relationship",
79
- "color",
80
- ],
81
- },
82
- },
83
- },
84
- "required": ["nodes", "edges"],
85
- },
86
- }
87
- ],
88
  function_call={"name": "knowledge_graph"},
89
  )
90
 
91
- response_data = response.choices[0]["message"]["function_call"]["arguments"]
92
- return response_data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
- # Defina a interface Gradio
95
  iface = gr.Interface(
96
- fn=create_knowledge_graph,
97
- inputs=gr.Textbox("Texto para criar o gráfico de conhecimento:"),
98
- outputs=gr.Image(type="pil"), # Imagem de saída para o gráfico
99
- live=True,
 
 
 
100
  )
101
 
102
- if __name__ == "__main__":
103
- iface.launch()
 
1
  import gradio as gr
 
2
  import openai
3
+ import json
4
+ import requests
5
+ from bs4 import BeautifulSoup
6
+ from graphviz import Digraph
7
+ import base64
8
+ from io import BytesIO
9
+ from PIL import Image
10
 
11
+ def generate_knowledge_graph(api_key, user_input):
 
 
 
 
 
 
 
 
12
  openai.api_key = api_key
13
+
14
+ completion = openai.ChatCompletion.create(
15
  model="gpt-3.5-turbo-16k",
16
  messages=[
17
  {
 
19
  "content": f"Help me understand following by describing as a detailed knowledge graph: {user_input}",
20
  }
21
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  function_call={"name": "knowledge_graph"},
23
  )
24
 
25
+ response_data = completion.choices[0]["message"]["function_call"]["arguments"]
26
+ response_dict = json.loads(response_data)
27
+
28
+ dot = Digraph(comment="Knowledge Graph")
29
+
30
+ # Add nodes to the graph
31
+ for node in response_dict.get("nodes", []):
32
+ dot.node(node["id"], f"{node['label']} ({node['type']})")
33
+
34
+ # Add edges to the graph
35
+ for edge in response_dict.get("edges", []):
36
+ dot.edge(edge["from"], edge["to"], label=edge["relationship"])
37
+
38
+ # Render to PNG format
39
+ dot.format = "png"
40
+ dot.render(filename="knowledge_graph", cleanup=True)
41
+
42
+ # Convert PNG to base64 to display in Gradio
43
+ with open("knowledge_graph.png", "rb") as img_file:
44
+ img_base64 = base64.b64encode(img_file.read()).decode()
45
+
46
+ return f"data:image/png;base64,{img_base64}"
47
 
 
48
  iface = gr.Interface(
49
+ fn=generate_knowledge_graph,
50
+ inputs=[
51
+ gr.inputs.Textbox(label="OpenAI API Key", type="password"),
52
+ gr.inputs.Textbox(label="Text to Generate Knowledge Graph")
53
+ ],
54
+ outputs=gr.outputs.Image(type="pil", label="Generated Knowledge Graph"),
55
+ live=False
56
  )
57
 
58
+ iface.launch()