Spaces:
Sleeping
Sleeping
Commit
·
8ea927a
1
Parent(s):
850424f
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
{
|
| 19 |
+
"role": "user",
|
| 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()
|