helliun commited on
Commit
77fbdd0
1 Parent(s): 8968917

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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(share=True)