Spaces:
Sleeping
Sleeping
Commit
·
0ea720c
1
Parent(s):
9834f8b
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,20 +7,25 @@ import io
|
|
| 7 |
import requests
|
| 8 |
from bs4 import BeautifulSoup
|
| 9 |
|
| 10 |
-
# Function to
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
text =
|
| 18 |
-
|
| 19 |
-
|
| 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,55 +101,68 @@ def process_user_input(api_key, user_input):
|
|
| 96 |
],
|
| 97 |
function_call={"name": "knowledge_graph"},
|
| 98 |
)
|
|
|
|
| 99 |
response_data = completion.choices[0]["message"]["function_call"]["arguments"]
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
|
| 104 |
dot = Digraph(comment="Knowledge Graph", format='png')
|
| 105 |
dot.attr(dpi='300')
|
| 106 |
-
dot.attr(bgcolor='
|
|
|
|
|
|
|
| 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 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
if response.status_code != 200:
|
| 121 |
-
return "Error: Could not retrieve content from URL."
|
| 122 |
-
soup = BeautifulSoup(response.text, "html.parser")
|
| 123 |
-
paragraphs = soup.find_all("p")
|
| 124 |
-
text = " ".join([p.get_text() for p in paragraphs])
|
| 125 |
-
return text
|
| 126 |
|
|
|
|
| 127 |
title_and_description = """
|
| 128 |
# Instagraph - Knowledge Graph Generator
|
| 129 |
|
| 130 |
**Created by [ArtificialGuyBR](https://twitter.com/ArtificialGuyBR)**
|
| 131 |
|
| 132 |
-
This interactive knowledge graph generator
|
| 133 |
-
If you provide text, it will generate a knowledge graph based on the text you provide.
|
| 134 |
-
If you provide a URL, it will scrape the content from the webpage and generate a knowledge graph from that.
|
| 135 |
|
| 136 |
-
|
| 137 |
"""
|
| 138 |
|
|
|
|
| 139 |
iface = gr.Interface(
|
| 140 |
-
fn=
|
| 141 |
inputs=[
|
| 142 |
gr.inputs.Textbox(label="OpenAI API Key", type="password"),
|
| 143 |
-
gr.inputs.Textbox(label="
|
| 144 |
],
|
| 145 |
outputs=gr.outputs.Image(type="pil", label="Generated Knowledge Graph"),
|
| 146 |
live=False,
|
| 147 |
title=title_and_description,
|
| 148 |
)
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
iface.launch()
|
|
|
|
| 7 |
import requests
|
| 8 |
from bs4 import BeautifulSoup
|
| 9 |
|
| 10 |
+
# Function to scrape text from a website
|
| 11 |
+
def scrape_text_from_url(url):
|
| 12 |
+
response = requests.get(url)
|
| 13 |
+
if response.status_code != 200:
|
| 14 |
+
return "Error: Could not retrieve content from URL."
|
| 15 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 16 |
+
paragraphs = soup.find_all("p")
|
| 17 |
+
text = " ".join([p.get_text() for p in paragraphs])
|
| 18 |
+
return text
|
| 19 |
+
|
| 20 |
+
def generate_knowledge_graph(api_key, user_input):
|
|
|
|
|
|
|
| 21 |
openai.api_key = api_key
|
| 22 |
+
|
| 23 |
+
# Check if input is URL or text
|
| 24 |
+
if user_input.startswith("http://") or user_input.startswith("https://"):
|
| 25 |
+
user_input = scrape_text_from_url(user_input)
|
| 26 |
+
|
| 27 |
+
# Chamar a API da OpenAI
|
| 28 |
+
print("Chamando a API da OpenAI...")
|
| 29 |
completion = openai.ChatCompletion.create(
|
| 30 |
model="gpt-3.5-turbo-16k",
|
| 31 |
messages=[
|
|
|
|
| 101 |
],
|
| 102 |
function_call={"name": "knowledge_graph"},
|
| 103 |
)
|
| 104 |
+
|
| 105 |
response_data = completion.choices[0]["message"]["function_call"]["arguments"]
|
| 106 |
+
print(response_data)
|
| 107 |
+
print("Type of response_data:", type(response_data))
|
| 108 |
+
print("Value of response_data:", response_data)
|
| 109 |
+
|
| 110 |
+
# Convert to dictionary if it's a string
|
| 111 |
+
if isinstance(response_data, str):
|
| 112 |
+
response_data = json.loads(response_data)
|
| 113 |
|
| 114 |
+
# Visualizar o conhecimento usando Graphviz
|
| 115 |
+
print("Gerando o conhecimento usando Graphviz...")
|
| 116 |
dot = Digraph(comment="Knowledge Graph", format='png')
|
| 117 |
dot.attr(dpi='300')
|
| 118 |
+
dot.attr(bgcolor='transparent')
|
| 119 |
+
|
| 120 |
+
# Estilizar os nós
|
| 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 |
+
# Estilizar as arestas
|
| 127 |
dot.attr('edge', color='black', fontcolor='black')
|
| 128 |
+
|
| 129 |
for edge in response_data.get("edges", []):
|
| 130 |
dot.edge(edge["from"], edge["to"], label=edge["relationship"], color=edge.get("color", "black"))
|
| 131 |
+
|
| 132 |
+
# Renderizar para o formato PNG
|
| 133 |
+
print("Renderizando o gráfico para o formato PNG...")
|
| 134 |
image_data = dot.pipe()
|
| 135 |
image = Image.open(io.BytesIO(image_data))
|
|
|
|
| 136 |
|
| 137 |
+
print("Gráfico gerado com sucesso!")
|
| 138 |
+
|
| 139 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
# Define a title and description for the Gradio interface using Markdown
|
| 142 |
title_and_description = """
|
| 143 |
# Instagraph - Knowledge Graph Generator
|
| 144 |
|
| 145 |
**Created by [ArtificialGuyBR](https://twitter.com/ArtificialGuyBR)**
|
| 146 |
|
| 147 |
+
This interactive knowledge graph generator is inspired by [this GitHub project](https://github.com/yoheinakajima/instagraph/).
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
Enter your OpenAI API Key and a question, and let the AI create a detailed knowledge graph for you.
|
| 150 |
"""
|
| 151 |
|
| 152 |
+
# Create the Gradio interface with queueing enabled and concurrency_count set to 10
|
| 153 |
iface = gr.Interface(
|
| 154 |
+
fn=generate_knowledge_graph,
|
| 155 |
inputs=[
|
| 156 |
gr.inputs.Textbox(label="OpenAI API Key", type="password"),
|
| 157 |
+
gr.inputs.Textbox(label="User Input for Graph or URL", type="text"),
|
| 158 |
],
|
| 159 |
outputs=gr.outputs.Image(type="pil", label="Generated Knowledge Graph"),
|
| 160 |
live=False,
|
| 161 |
title=title_and_description,
|
| 162 |
)
|
| 163 |
|
| 164 |
+
# Enable queueing system for multiple users
|
| 165 |
+
iface.queue(concurrency_count=10)
|
| 166 |
+
|
| 167 |
+
print("Iniciando a interface Gradio...")
|
| 168 |
iface.launch()
|