sidagarwal04 commited on
Commit
d3d4ff5
·
verified ·
1 Parent(s): a175a14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -67
app.py CHANGED
@@ -1,42 +1,12 @@
1
- # -*- coding: utf-8 -*-
2
- """mahabharata-chatbot.ipynb
3
-
4
- Automatically generated by Colaboratory.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/14PZqWZU0cD12pm4gVs2BT4VpWlr1CHKx
8
-
9
- # Graph Powered NLP Workshop Python Notebook
10
-
11
- ### Installing necessary drivers - Google Generative AI, Neo4j and Gradio.
12
-
13
- **Instruction:** Don't forget to restart the runtime after running the below cell
14
- """
15
-
16
- ### pip install google-generativeai
17
- ### pip install -q neo4j-driver
18
- ### pip install -q gradio
19
-
20
- """### Import necessary libraries from installed packages/drivers - PaLM, base64, json, gradio and GraphDatabase"""
21
-
22
  import google.generativeai as genai
23
  import base64
24
  import json
 
25
  import gradio as gr
26
  from neo4j import GraphDatabase
27
 
28
- """### Add PaLM2 API Key from Google MakerSuite
29
-
30
- **Instruction:** Replace "API_KEY" with the value of API Key copied from MakerSuite as mentioned in Step #6 of Part #2 of [Step-by-Step Guide](https://github.com/sidagarwal04/graph-powered-nlp-workshop/blob/main/step-by-step-guide.md#part-2-create-google-makersuite-account-train--test-prompt-in-google-makersuite-and-get-google-palm-2-api-key). Don't forget to add to include the key in double-quotes (" ")
31
- """
32
-
33
  genai.configure(api_key = "AIzaSyA2eO7Mc1d_yTXBcJSH8w3XqdcVZD3Pl6s")
34
 
35
- """### Include the generated prompt from MakerSuite.
36
-
37
- **Instruction:** Remove the initial part of installing drivers and configuring the API key as it has already been done in previous steps. Also, put the entire code as a function with output to be returned instead of printing it.
38
- """
39
-
40
  def get_answer(input):
41
 
42
  # Set up the model
@@ -85,25 +55,10 @@ def get_answer(input):
85
 
86
  return response.text
87
 
88
- """### Testing the output of get_answer() function with a test input"""
89
-
90
- get_answer("Who killed Ghatotakach?")
91
-
92
- """### Initialize GraphDatabase driver
93
-
94
- **Instruction:** Replace URI, username and password before running the cell with the values from the txt file downloaded when creating Neo4j AuraDB instance in Step #4 of Part #1 of [Step By Step Guide](https://github.com/sidagarwal04/graph-powered-nlp-workshop/blob/main/step-by-step-guide.md#part-1-create-and-load-a-neo4j-instance) of this workshop.
95
- """
96
-
97
  driver = GraphDatabase.driver("neo4j+s://9bebfeb5.databases.neo4j.io",
98
  auth=("neo4j",
99
  "SQsRVyWfLuq8dl24WjcvlMhw7P20-TTT30Ywb-2miZM"))
100
 
101
- """### Import required library for processing regular expressions"""
102
-
103
- import re
104
-
105
- """### Function to clean the output query from get_answer() function by removing slash n's (\n) and substituting it with a space if it exists. Also, extract the string after RETURN expression in the output cypher query and utilize as a separate key to be used for printing the output in chatbot in later steps"""
106
-
107
  def extract_query_and_return_key(input_query_result):
108
  slash_n_pattern = r'[ \n]+'
109
  ret_pattern = r'RETURN\s+(.*)'
@@ -118,12 +73,6 @@ def extract_query_and_return_key(input_query_result):
118
  extracted_string = ""
119
  return cleaned_query, extracted_string
120
 
121
- """### Testing the extract_query_and_return_key() function with a test input in natural language"""
122
-
123
- extract_query_and_return_key(get_answer("Who killed Ghatotakach?"))
124
-
125
- """### format_names_with_ampersand() to return results as a comma-separated string of values with last value having '&' (ampersand/and) in case the output is a list of values."""
126
-
127
  def format_names_with_ampersand(names):
128
  if len(names) == 0:
129
  return ""
@@ -133,12 +82,6 @@ def format_names_with_ampersand(names):
133
  formatted_names = ", ".join(names[:-1]) + " & " + names[-1]
134
  return formatted_names
135
 
136
- """### Testing format_names_with_ampersand() with sample input having list of values"""
137
-
138
- format_names_with_ampersand(["Karna"])
139
-
140
- """### run_cypher_on_neo4j() to pass the output query from get_answer() to the Neo4j Database. If the length of output list is more than 1, format_name_with_ampersand() will further format the list and if the length of output list is equal to 1, output list is returned as it is. In case the output list is empty, an empty string is returned"""
141
-
142
  def run_cypher_on_neo4j(inp_query, inp_key):
143
  out_list = []
144
  with driver.session() as session:
@@ -153,26 +96,17 @@ def run_cypher_on_neo4j(inp_query, inp_key):
153
  else:
154
  return ""
155
 
156
- """### Additional generate_and_exec_cypher() to parse and format the output of get_answer() and pass it to run_cypher_on_neo4j()"""
157
-
158
  def generate_and_exec_cypher(input_query):
159
  gen_query, gen_key = extract_query_and_return_key(get_answer(input_query))
160
  return run_cypher_on_neo4j(gen_query, gen_key)
161
 
162
  generate_and_exec_cypher("Who killed Ghatotakach?")
163
 
164
- """### chatbot() to initiliaze the chatbot and pass the output of generate_and_exec_cypher to be displayed in the chatbot"""
165
-
166
  def chatbot(input, history=[]):
167
  output = str(generate_and_exec_cypher(input))
168
  history.append((input, output))
169
  return history, history
170
 
171
- """### Initializing Gradio interface to run the chatbot.
172
-
173
- **Instruction:** Run the chatbot in the localhost url generated after running the cell and play aroung with input and output in natural language while fetching the results from the Neo4j Database using PaLM 2 API for converting input text into cypher code.
174
- """
175
-
176
  gr.Interface(fn = chatbot,
177
  inputs = ["text",'state'],
178
  outputs = ["chatbot",'state']).launch(debug = True, share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import google.generativeai as genai
2
  import base64
3
  import json
4
+ import re
5
  import gradio as gr
6
  from neo4j import GraphDatabase
7
 
 
 
 
 
 
8
  genai.configure(api_key = "AIzaSyA2eO7Mc1d_yTXBcJSH8w3XqdcVZD3Pl6s")
9
 
 
 
 
 
 
10
  def get_answer(input):
11
 
12
  # Set up the model
 
55
 
56
  return response.text
57
 
 
 
 
 
 
 
 
 
 
58
  driver = GraphDatabase.driver("neo4j+s://9bebfeb5.databases.neo4j.io",
59
  auth=("neo4j",
60
  "SQsRVyWfLuq8dl24WjcvlMhw7P20-TTT30Ywb-2miZM"))
61
 
 
 
 
 
 
 
62
  def extract_query_and_return_key(input_query_result):
63
  slash_n_pattern = r'[ \n]+'
64
  ret_pattern = r'RETURN\s+(.*)'
 
73
  extracted_string = ""
74
  return cleaned_query, extracted_string
75
 
 
 
 
 
 
 
76
  def format_names_with_ampersand(names):
77
  if len(names) == 0:
78
  return ""
 
82
  formatted_names = ", ".join(names[:-1]) + " & " + names[-1]
83
  return formatted_names
84
 
 
 
 
 
 
 
85
  def run_cypher_on_neo4j(inp_query, inp_key):
86
  out_list = []
87
  with driver.session() as session:
 
96
  else:
97
  return ""
98
 
 
 
99
  def generate_and_exec_cypher(input_query):
100
  gen_query, gen_key = extract_query_and_return_key(get_answer(input_query))
101
  return run_cypher_on_neo4j(gen_query, gen_key)
102
 
103
  generate_and_exec_cypher("Who killed Ghatotakach?")
104
 
 
 
105
  def chatbot(input, history=[]):
106
  output = str(generate_and_exec_cypher(input))
107
  history.append((input, output))
108
  return history, history
109
 
 
 
 
 
 
110
  gr.Interface(fn = chatbot,
111
  inputs = ["text",'state'],
112
  outputs = ["chatbot",'state']).launch(debug = True, share=True)