helliun commited on
Commit
402a83c
1 Parent(s): 6826a9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -2,39 +2,39 @@ from GPT4KG import KnowledgeGraph
2
  import gradio as gr
3
  from PIL import Image
4
 
5
- def generate_graph(input_text,api_key):
6
- try:
7
- kg = KnowledgeGraph(api_key,"temp.kg")
8
- except:
9
  kg = KnowledgeGraph(api_key)
 
 
 
10
  kg.learn(str(input_text))
11
- kg.save_graph("temp.kg")
12
- kg.display_graph("temp.png")
13
- return Image.open("temp.png")
14
 
15
- def answer_question(question,api_key):
16
- try:
17
- kg = KnowledgeGraph(api_key,"temp.kg")
18
- except:
19
  kg = KnowledgeGraph(api_key)
 
 
 
20
  return kg.chat_qa(question)
21
 
22
  title = "GPT-4 Knowledge Graph Generator"
23
  description = "Enter text to generate a knowledge graph using GPT4KG:"
24
 
25
  with gr.Blocks() as demo:
26
- with open("temp.kg","w") as f:
27
- f.write("")
28
  gr.Markdown("""<h1><center>GPT-4 Knowledge Graph Generator</center></h1>""")
29
  output_image = gr.Image(label="Knowledge Graph", type="pil")
30
  api_key = gr.Textbox(lines=1, label="OpenAI API Key")
 
31
  input_text = gr.Textbox(lines=5, label="Information to be added to graph")
32
  submit_btn = gr.Button("Add info to graph")
33
- submit_btn.click(fn=generate_graph, inputs=[input_text,api_key], outputs=[output_image])
34
 
35
  question = gr.Textbox(lines=1, label="Question about the info in this graph")
36
  answer = gr.Textbox(lines=1, label="Answer")
37
  qa_btn = gr.Button("Ask question")
38
- qa_btn.click(fn=answer_question, inputs=[question,api_key], outputs=[answer])
39
 
40
  demo.launch()
 
2
  import gradio as gr
3
  from PIL import Image
4
 
5
+ def generate_graph(input_text,api_key,graph):
6
+ if graph[0] =="":
 
 
7
  kg = KnowledgeGraph(api_key)
8
+ graph[0] = kg
9
+ else:
10
+ kg = graph[0]
11
  kg.learn(str(input_text))
12
+ img = kg.display_graph()
13
+ return img,graph
 
14
 
15
+ def answer_question(question,api_key,graph):
16
+ if graph[0] =="":
 
 
17
  kg = KnowledgeGraph(api_key)
18
+ graph[0] = kg
19
+ else:
20
+ kg = graph[0]
21
  return kg.chat_qa(question)
22
 
23
  title = "GPT-4 Knowledge Graph Generator"
24
  description = "Enter text to generate a knowledge graph using GPT4KG:"
25
 
26
  with gr.Blocks() as demo:
 
 
27
  gr.Markdown("""<h1><center>GPT-4 Knowledge Graph Generator</center></h1>""")
28
  output_image = gr.Image(label="Knowledge Graph", type="pil")
29
  api_key = gr.Textbox(lines=1, label="OpenAI API Key")
30
+ graph = gr.State([""])
31
  input_text = gr.Textbox(lines=5, label="Information to be added to graph")
32
  submit_btn = gr.Button("Add info to graph")
33
+ submit_btn.click(fn=generate_graph, inputs=[input_text,api_key,graph], outputs=[output_image,graph])
34
 
35
  question = gr.Textbox(lines=1, label="Question about the info in this graph")
36
  answer = gr.Textbox(lines=1, label="Answer")
37
  qa_btn = gr.Button("Ask question")
38
+ qa_btn.click(fn=answer_question, inputs=[question,api_key,graph], outputs=[answer])
39
 
40
  demo.launch()