Spaces:
Sleeping
Sleeping
File size: 8,080 Bytes
711256b |
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
import openai
import pinecone
import gradio as gr
import os
import json
# initiates pinecone
def init_pinecone(apikey, environment):
pinecone.init(api_key=apikey, environment=environment)
def qa_gita(query, openaikey):
# Basic Checks
if not query:
return "Please enter your query."
if not openaikey:
return "Please enter openaikey."
openai.api_key = openaikey
response = openai.Embedding.create(
input=[query], model="text-embedding-ada-002")
embedding = response["data"][0]["embedding"]
pinecone_api_key_gita = os.getenv("PINECONE_API_KEY_GITA")
pinecone_environment = os.getenv("PINECONE_ENVIRONMENT_GITA")
init_pinecone(pinecone_api_key_gita, pinecone_environment)
index = pinecone.Index("gita")
with open("data.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["gita"][str(id)] + "\n\n"
systemprompt = f"You are not an AI Language model. You will be a Bhagwad Gita 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 qa_bible(query, openaikey):
# Basic Checks
if not query:
return "Please enter your query."
if not openaikey:
return "Please enter openaikey."
openai.api_key = openaikey
response = openai.Embedding.create(
input=[query], model="text-embedding-ada-002")
embedding = response["data"][0]["embedding"]
pinecone_api_key_bible = os.getenv("PINECONE_API_KEY_BIBLE")
pinecone_environment = os.getenv("PINECONE_ENVIRONMENT_BIBLE")
init_pinecone(pinecone_api_key_bible, pinecone_environment)
index = pinecone.Index("bible")
with open("data.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["bible"][str(id)] + "\n\n"
systemprompt = f"You are not an AI Language model. You will be a Bible 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 qa_quran(query, openaikey):
# Basic Checks
if not query:
return "Please enter your query."
if not openaikey:
return "Please enter openaikey."
openai.api_key = openaikey
response = openai.Embedding.create(
input=[query], model="text-embedding-ada-002")
embedding = response["data"][0]["embedding"]
pinecone_api_key_quran = os.getenv("PINECONE_API_KEY_QURAN")
pinecone_environment = os.getenv("PINECONE_ENVIRONMENT_QURAN")
init_pinecone(pinecone_api_key_quran, pinecone_environment)
index = pinecone.Index("quran")
with open("data.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["quran"][str(id)] + "\n\n"
systemprompt = f"You are not an AI Language model. You will be a Quran 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(query, output, references):
"""
Function to clear text
"""
return ["", "", ""]
with gr.Blocks() as demo:
gr.Markdown(
"""
<h1><center><b>HolyBot</center></h1>
"""
)
gr.Markdown(
"""
HolyBot answers your queries and gives relevant verses based on Bhagwad Gita/ Quran/ Bible holy books, built using OpenAI ChatGPT, and Pinecone Index.
- Get your [OpenAI API Key](https://platform.openai.com/account/api-keys) before proceeding further.
- Refer to the codebase for this project on [GitHub](https://github.com/ravi03071991/HolyBot)."""
)
with gr.Tabs():
openaikey = gr.Textbox(lines=1, label="Enter Your OpenAI Key")
with gr.TabItem("Bhagwad Gita"):
with gr.Row():
with gr.Column():
query1 = gr.Textbox(
lines=2, label="Enter Your Situation/ Query.")
submit_button1 = gr.Button("Submit")
with gr.Column():
ans_output1 = gr.Textbox(lines=5, label="Answer.")
references1 = gr.Textbox(
lines=10, label="Relevant Verses.")
clear_button1 = gr.Button("Clear")
with gr.TabItem("Quran"):
with gr.Row():
with gr.Column():
query2 = gr.Textbox(
lines=2, label="Enter Your Situation/ Query.")
submit_button2 = gr.Button("Submit")
with gr.Column():
ans_output2 = gr.Textbox(lines=5, label="Answer.")
references2 = gr.Textbox(
lines=10, label="Relevant Verses.")
clear_button2 = gr.Button("Clear")
with gr.TabItem("Bible"):
with gr.Row():
with gr.Column():
query3 = gr.Textbox(
lines=2, label="Enter Your Situation/ Query.")
submit_button3 = gr.Button("Submit")
with gr.Column():
ans_output3 = gr.Textbox(lines=5, label="Answer.")
references3 = gr.Textbox(
lines=10, label="Relevant Verses.")
clear_button3 = gr.Button("Clear")
# For Bhagwad Gita
# Submit button for submitting query.
submit_button1.click(qa_gita, inputs=[query1, openaikey], outputs=[
ans_output1, references1])
# Clear button for clearing query and answer.
clear_button1.click(
cleartext,
inputs=[query1, ans_output1, references1],
outputs=[query1, ans_output1, references1],
)
# For Quran
# Submit button for submitting query.
submit_button2.click(qa_quran, inputs=[query2, openaikey], outputs=[
ans_output2, references2])
# Clear button for clearing query and answer.
clear_button2.click(
cleartext,
inputs=[query2, ans_output2, references2],
outputs=[query2, ans_output2, references2],
)
# For Bible
# Submit button for submitting query.
submit_button3.click(qa_bible, inputs=[query3, openaikey], outputs=[
ans_output3, references3])
# Clear button for clearing query and answer.
clear_button3.click(
cleartext,
inputs=[query3, ans_output3, references3],
outputs=[query3, ans_output3, references3],
)
demo.launch(debug=True)
|