Spaces:
Sleeping
Sleeping
Commit
·
9834f8b
1
Parent(s):
48b792e
Update app.py
Browse files
app.py
CHANGED
@@ -9,32 +9,18 @@ from bs4 import BeautifulSoup
|
|
9 |
|
10 |
# Function to generate a knowledge graph from text
|
11 |
def generate_knowledge_graph_from_text(api_key, user_input):
|
12 |
-
# Ensure the API key and user input are provided
|
13 |
-
if not api_key or not user_input:
|
14 |
-
raise ValueError("Please provide both the OpenAI API Key and User Input")
|
15 |
-
|
16 |
-
# Process user input
|
17 |
response_data = process_user_input(api_key, user_input)
|
18 |
return generate_knowledge_graph(response_data)
|
19 |
|
20 |
# Function to generate a knowledge graph from a URL
|
21 |
-
def generate_knowledge_graph_from_url(api_key,
|
22 |
-
|
23 |
-
if not api_key or not url:
|
24 |
-
raise ValueError("Please provide both the OpenAI API Key and a URL")
|
25 |
-
|
26 |
-
# Scrape text from the provided URL
|
27 |
-
text = scrape_text_from_url(url)
|
28 |
-
|
29 |
-
# Process the scraped text
|
30 |
response_data = process_user_input(api_key, text)
|
31 |
return generate_knowledge_graph(response_data)
|
32 |
|
33 |
# Function to process user input and call OpenAI API
|
34 |
def process_user_input(api_key, user_input):
|
35 |
openai.api_key = api_key
|
36 |
-
|
37 |
-
# Call the OpenAI API
|
38 |
completion = openai.ChatCompletion.create(
|
39 |
model="gpt-3.5-turbo-16k",
|
40 |
messages=[
|
@@ -110,33 +96,22 @@ def process_user_input(api_key, user_input):
|
|
110 |
],
|
111 |
function_call={"name": "knowledge_graph"},
|
112 |
)
|
113 |
-
|
114 |
response_data = completion.choices[0]["message"]["function_call"]["arguments"]
|
115 |
return response_data
|
116 |
|
117 |
# Function to generate a knowledge graph from response data
|
118 |
def generate_knowledge_graph(response_data):
|
119 |
-
# Visualizar o conhecimento usando Graphviz
|
120 |
dot = Digraph(comment="Knowledge Graph", format='png')
|
121 |
dot.attr(dpi='300')
|
122 |
dot.attr(bgcolor='white') # Set background color to white
|
123 |
-
|
124 |
-
# Estilizar os nós
|
125 |
dot.attr('node', shape='box', style='filled', fillcolor='lightblue', fontcolor='black')
|
126 |
-
|
127 |
for node in response_data.get("nodes", []):
|
128 |
dot.node(node["id"], f"{node['label']} ({node['type']})", color=node.get("color", "lightblue"))
|
129 |
-
|
130 |
-
# Estilizar as arestas
|
131 |
dot.attr('edge', color='black', fontcolor='black')
|
132 |
-
|
133 |
for edge in response_data.get("edges", []):
|
134 |
dot.edge(edge["from"], edge["to"], label=edge["relationship"], color=edge.get("color", "black"))
|
135 |
-
|
136 |
-
# Renderizar para o formato PNG
|
137 |
image_data = dot.pipe()
|
138 |
image = Image.open(io.BytesIO(image_data))
|
139 |
-
|
140 |
return image
|
141 |
|
142 |
# Function to scrape text from a website
|
@@ -149,7 +124,6 @@ def scrape_text_from_url(url):
|
|
149 |
text = " ".join([p.get_text() for p in paragraphs])
|
150 |
return text
|
151 |
|
152 |
-
# Define a title and description for the Gradio interface using Markdown
|
153 |
title_and_description = """
|
154 |
# Instagraph - Knowledge Graph Generator
|
155 |
|
@@ -162,7 +136,6 @@ If you provide a URL, it will scrape the content from the webpage and generate a
|
|
162 |
To get started, enter your OpenAI API Key and either your text or a URL.
|
163 |
"""
|
164 |
|
165 |
-
# Create the Gradio interface with queueing enabled and concurrency_count set to 10
|
166 |
iface = gr.Interface(
|
167 |
fn=generate_knowledge_graph_from_text,
|
168 |
inputs=[
|
@@ -174,8 +147,4 @@ iface = gr.Interface(
|
|
174 |
title=title_and_description,
|
175 |
)
|
176 |
|
177 |
-
# Enable queueing system for multiple users
|
178 |
-
iface.queue(concurrency_count=10)
|
179 |
-
|
180 |
-
print("Iniciando a interface Gradio...")
|
181 |
iface.launch()
|
|
|
9 |
|
10 |
# Function to generate a knowledge graph from text
|
11 |
def generate_knowledge_graph_from_text(api_key, user_input):
|
|
|
|
|
|
|
|
|
|
|
12 |
response_data = process_user_input(api_key, user_input)
|
13 |
return generate_knowledge_graph(response_data)
|
14 |
|
15 |
# Function to generate a knowledge graph from a URL
|
16 |
+
def generate_knowledge_graph_from_url(api_key, user_input):
|
17 |
+
text = scrape_text_from_url(user_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
response_data = process_user_input(api_key, text)
|
19 |
return generate_knowledge_graph(response_data)
|
20 |
|
21 |
# Function to process user input and call OpenAI API
|
22 |
def process_user_input(api_key, user_input):
|
23 |
openai.api_key = api_key
|
|
|
|
|
24 |
completion = openai.ChatCompletion.create(
|
25 |
model="gpt-3.5-turbo-16k",
|
26 |
messages=[
|
|
|
96 |
],
|
97 |
function_call={"name": "knowledge_graph"},
|
98 |
)
|
|
|
99 |
response_data = completion.choices[0]["message"]["function_call"]["arguments"]
|
100 |
return response_data
|
101 |
|
102 |
# Function to generate a knowledge graph from response data
|
103 |
def generate_knowledge_graph(response_data):
|
|
|
104 |
dot = Digraph(comment="Knowledge Graph", format='png')
|
105 |
dot.attr(dpi='300')
|
106 |
dot.attr(bgcolor='white') # Set background color to white
|
|
|
|
|
107 |
dot.attr('node', shape='box', style='filled', fillcolor='lightblue', fontcolor='black')
|
|
|
108 |
for node in response_data.get("nodes", []):
|
109 |
dot.node(node["id"], f"{node['label']} ({node['type']})", color=node.get("color", "lightblue"))
|
|
|
|
|
110 |
dot.attr('edge', color='black', fontcolor='black')
|
|
|
111 |
for edge in response_data.get("edges", []):
|
112 |
dot.edge(edge["from"], edge["to"], label=edge["relationship"], color=edge.get("color", "black"))
|
|
|
|
|
113 |
image_data = dot.pipe()
|
114 |
image = Image.open(io.BytesIO(image_data))
|
|
|
115 |
return image
|
116 |
|
117 |
# Function to scrape text from a website
|
|
|
124 |
text = " ".join([p.get_text() for p in paragraphs])
|
125 |
return text
|
126 |
|
|
|
127 |
title_and_description = """
|
128 |
# Instagraph - Knowledge Graph Generator
|
129 |
|
|
|
136 |
To get started, enter your OpenAI API Key and either your text or a URL.
|
137 |
"""
|
138 |
|
|
|
139 |
iface = gr.Interface(
|
140 |
fn=generate_knowledge_graph_from_text,
|
141 |
inputs=[
|
|
|
147 |
title=title_and_description,
|
148 |
)
|
149 |
|
|
|
|
|
|
|
|
|
150 |
iface.launch()
|