Spaces:
Sleeping
Sleeping
Commit
·
b79f8d4
1
Parent(s):
72d1247
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,36 @@
|
|
| 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 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 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 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
dot = Digraph(comment="Knowledge Graph")
|
| 29 |
|
| 30 |
# Add nodes to the graph
|
|
@@ -36,13 +42,16 @@ def generate_knowledge_graph(api_key, user_input):
|
|
| 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(
|
|
@@ -55,4 +64,5 @@ iface = gr.Interface(
|
|
| 55 |
live=False
|
| 56 |
)
|
| 57 |
|
|
|
|
| 58 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
import json
|
|
|
|
|
|
|
| 4 |
from graphviz import Digraph
|
| 5 |
import base64
|
| 6 |
from io import BytesIO
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
def generate_knowledge_graph(api_key, user_input):
|
| 10 |
+
print("Setting OpenAI API key...")
|
| 11 |
openai.api_key = api_key
|
| 12 |
|
| 13 |
+
print("Making API call to OpenAI...")
|
| 14 |
+
completion = openai.Completion.create(
|
| 15 |
+
engine="text-davinci-002",
|
| 16 |
+
prompt=f"Help me understand the following by describing it as a detailed knowledge graph: {user_input}",
|
| 17 |
+
max_tokens=100
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
+
print("Received response from OpenAI.")
|
| 21 |
+
response_data = completion.choices[0].text
|
| 22 |
+
print(f"Response data: {response_data}")
|
| 23 |
|
| 24 |
+
# For demonstration, let's assume the response_data is a JSON string that can be converted to a dictionary.
|
| 25 |
+
# You'll need to write code to interpret the text-based response to generate this dictionary.
|
| 26 |
+
print("Converting response to JSON...")
|
| 27 |
+
try:
|
| 28 |
+
response_dict = json.loads(response_data)
|
| 29 |
+
except json.JSONDecodeError:
|
| 30 |
+
print("Failed to decode JSON. Using empty dictionary as a fallback.")
|
| 31 |
+
response_dict = {}
|
| 32 |
+
|
| 33 |
+
print("Generating knowledge graph using Graphviz...")
|
| 34 |
dot = Digraph(comment="Knowledge Graph")
|
| 35 |
|
| 36 |
# Add nodes to the graph
|
|
|
|
| 42 |
dot.edge(edge["from"], edge["to"], label=edge["relationship"])
|
| 43 |
|
| 44 |
# Render to PNG format
|
| 45 |
+
print("Rendering graph to PNG format...")
|
| 46 |
dot.format = "png"
|
| 47 |
dot.render(filename="knowledge_graph", cleanup=True)
|
| 48 |
|
| 49 |
# Convert PNG to base64 to display in Gradio
|
| 50 |
+
print("Converting PNG to base64...")
|
| 51 |
with open("knowledge_graph.png", "rb") as img_file:
|
| 52 |
img_base64 = base64.b64encode(img_file.read()).decode()
|
| 53 |
|
| 54 |
+
print("Returning base64 image to Gradio interface.")
|
| 55 |
return f"data:image/png;base64,{img_base64}"
|
| 56 |
|
| 57 |
iface = gr.Interface(
|
|
|
|
| 64 |
live=False
|
| 65 |
)
|
| 66 |
|
| 67 |
+
print("Launching Gradio interface...")
|
| 68 |
iface.launch()
|