Spaces:
Sleeping
Sleeping
Commit
·
b2968ad
1
Parent(s):
7af8c4a
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ from PIL import Image
|
|
6 |
import io
|
7 |
import requests
|
8 |
from bs4 import BeautifulSoup
|
|
|
9 |
|
10 |
# Function to scrape text from a website
|
11 |
def scrape_text_from_url(url):
|
@@ -102,46 +103,36 @@ def generate_knowledge_graph(api_key, user_input):
|
|
102 |
)
|
103 |
|
104 |
response_data = completion.choices[0]["message"]["function_call"]["arguments"]
|
105 |
-
# Debugging: Print the type and value of response_data
|
106 |
-
print(f"Type of response_data: {type(response_data)}")
|
107 |
-
print(f"Value of response_data: {response_data}")
|
108 |
|
109 |
try:
|
110 |
-
# Convert to dictionary if it's a string
|
111 |
if isinstance(response_data, str):
|
112 |
-
response_data =
|
113 |
-
except
|
114 |
-
print(f"JSON
|
115 |
return "Error in decoding JSON"
|
116 |
-
|
117 |
-
# Convert to dictionary if it's a string
|
118 |
-
if isinstance(response_data, str):
|
119 |
-
response_data = json.loads(response_data)
|
120 |
|
121 |
-
|
|
|
|
|
|
|
122 |
dot = Digraph(comment="Knowledge Graph", format='png')
|
123 |
dot.attr(dpi='300')
|
124 |
dot.attr(bgcolor='white')
|
125 |
-
|
126 |
-
# Estilizar os nós
|
127 |
dot.attr('node', shape='box', style='filled', fillcolor='lightblue', fontcolor='black')
|
128 |
|
129 |
for node in response_data.get("nodes", []):
|
130 |
dot.node(node["id"], f"{node['label']} ({node['type']})", color=node.get("color", "lightblue"))
|
131 |
|
132 |
-
# Estilizar as arestas
|
133 |
dot.attr('edge', color='black', fontcolor='black')
|
134 |
|
135 |
for edge in response_data.get("edges", []):
|
136 |
dot.edge(edge["from"], edge["to"], label=edge["relationship"], color=edge.get("color", "black"))
|
137 |
|
138 |
-
# Renderizar para o formato PNG
|
139 |
image_data = dot.pipe()
|
140 |
image = Image.open(io.BytesIO(image_data))
|
141 |
|
142 |
return image
|
143 |
|
144 |
-
# Define a title and description for the Gradio interface using Markdown
|
145 |
title_and_description = """
|
146 |
# Instagraph - Knowledge Graph Generator
|
147 |
|
@@ -169,7 +160,6 @@ with gr.Blocks() as app:
|
|
169 |
outputs=[result_image]
|
170 |
)
|
171 |
|
172 |
-
# Enable queueing system for multiple users
|
173 |
app.queue(concurrency_count=10)
|
174 |
|
175 |
print("Iniciando a interface Gradio...")
|
|
|
6 |
import io
|
7 |
import requests
|
8 |
from bs4 import BeautifulSoup
|
9 |
+
from ast import literal_eval
|
10 |
|
11 |
# Function to scrape text from a website
|
12 |
def scrape_text_from_url(url):
|
|
|
103 |
)
|
104 |
|
105 |
response_data = completion.choices[0]["message"]["function_call"]["arguments"]
|
|
|
|
|
|
|
106 |
|
107 |
try:
|
|
|
108 |
if isinstance(response_data, str):
|
109 |
+
response_data = literal_eval(response_data)
|
110 |
+
except (ValueError, SyntaxError) as e:
|
111 |
+
print(f"Error in decoding JSON or literal_eval: {e}")
|
112 |
return "Error in decoding JSON"
|
|
|
|
|
|
|
|
|
113 |
|
114 |
+
if not isinstance(response_data, dict):
|
115 |
+
print("Unexpected data type for response_data")
|
116 |
+
return "Error: Unexpected data type"
|
117 |
+
|
118 |
dot = Digraph(comment="Knowledge Graph", format='png')
|
119 |
dot.attr(dpi='300')
|
120 |
dot.attr(bgcolor='white')
|
|
|
|
|
121 |
dot.attr('node', shape='box', style='filled', fillcolor='lightblue', fontcolor='black')
|
122 |
|
123 |
for node in response_data.get("nodes", []):
|
124 |
dot.node(node["id"], f"{node['label']} ({node['type']})", color=node.get("color", "lightblue"))
|
125 |
|
|
|
126 |
dot.attr('edge', color='black', fontcolor='black')
|
127 |
|
128 |
for edge in response_data.get("edges", []):
|
129 |
dot.edge(edge["from"], edge["to"], label=edge["relationship"], color=edge.get("color", "black"))
|
130 |
|
|
|
131 |
image_data = dot.pipe()
|
132 |
image = Image.open(io.BytesIO(image_data))
|
133 |
|
134 |
return image
|
135 |
|
|
|
136 |
title_and_description = """
|
137 |
# Instagraph - Knowledge Graph Generator
|
138 |
|
|
|
160 |
outputs=[result_image]
|
161 |
)
|
162 |
|
|
|
163 |
app.queue(concurrency_count=10)
|
164 |
|
165 |
print("Iniciando a interface Gradio...")
|