-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
138 lines (108 loc) · 4.18 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import openai
import pinecone
import gradio as gr
import json
import argparse
import logging
# Create a logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# Create a console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
# Create a formatter
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Add the formatter to the console handler
console_handler.setFormatter(formatter)
# Add the console handler to the logger
logger.addHandler(console_handler)
class HolyBot:
def __init__(self):
try:
logger.info("Parsing arguments")
# Parse command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument("--holybook", type=str, required=True)
parser.add_argument("--pinecone_apikey", type=str, required=True)
parser.add_argument("--pinecone_environment",
type=str, required=True)
parser.add_argument("--openaikey", type=str, required=True)
args = parser.parse_args()
self.holybook = args.holybook
self.pinecone_apikey = args.pinecone_apikey
self.pinecone_environment = args.pinecone_environment
self.openaikey = args.openaikey
except Exception as e:
logger.error("Error while parsing arguments: {}".format(e))
# initiates pinecone
def init_pinecone(self):
pinecone.init(api_key=self.pinecone_apikey,
environment=self.pinecone_environment)
def qa(self, query):
# Basic Checks
if not query:
return "Please enter your query."
openai.api_key = self.openaikey
response = openai.Embedding.create(
input=[query], model="text-embedding-ada-002")
embedding = response["data"][0]["embedding"]
self.init_pinecone()
index = pinecone.Index(self.holybook)
with open(f"{self.holybook}.json", "r") as f:
data = json.loads(f.read())
res = index.query(vector=(embedding), top_k=8)
ids = [i["id"] for i in res["matches"]]
context = ""
for id in ids:
context = context + str(id) + ": " + data[str(id)] + "\n\n"
if self.holybook == "gita":
book = "Bhagwad Gita"
else:
book = self.holybook
logger.info("Book: {}".format(book))
systemprompt = f"You are not an AI Language model. You will be a {book} Assistant to the user. Restrict Yourself to the context of the question."
userprompt = f"Verses:\n\n{context}\n\nQuestion: {query}\n\nAnswer:\n\n"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": systemprompt},
{"role": "user", "content": userprompt},
],
max_tokens=256,
temperature=0.0,
)
answer = response["choices"][0]["message"]["content"]
return answer, context
def cleartext(self, query, output, references):
"""
Function to clear text
"""
return ["", "", ""]
if __name__ == "__main__":
askbook = HolyBot()
with gr.Blocks() as demo:
gr.Markdown(
"""
<h1><center><b>Ask a Holy Book</center></h1>
"""
)
with gr.Row():
with gr.Column():
query = gr.Textbox(lines=2, label="Enter Your Query.")
submit_button = gr.Button("Submit")
with gr.Column():
ans_output = gr.Textbox(lines=5, label="Answer.")
references = gr.Textbox(
lines=10, label="Relevant Verses.")
clear_button = gr.Button("Clear")
# Submit button for submitting query.
submit_button.click(askbook.qa, inputs=[query], outputs=[
ans_output, references])
# Clear button for clearing query and answer.
clear_button.click(
askbook.cleartext,
inputs=[query, ans_output, references],
outputs=[query, ans_output, references],
)
demo.launch(debug=True)