house_excel_query / qa_bot_chatgpt.py
arjunanand13's picture
Upload 19 files
7de61f5 verified
raw
history blame contribute delete
No virus
7.09 kB
import sqlite3
from sqlite3 import Error
from PyPDF2 import PdfReader
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, pipeline
import os
import torch
from huggingface_hub import login
import ast
from openai import OpenAI
import google.generativeai as genai
class QAInfer:
def __init__(self):
torch.cuda.empty_cache()
# self.chatgpt_client = OpenAI(api_key="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")
self.chatgpt_client = OpenAI(api_key="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")
self.genai = genai
self.genai.configure(api_key="AIzaSyAFG94rVbm9eWepO5uPGsMha8XJ-sHbMdA")
self.genai_model = genai.GenerativeModel('gemini-pro')
def extract_text_from_pdf(self, pdf_path):
"""Extract text from a PDF file."""
reader = PdfReader(pdf_path)
text = ''
for page in reader.pages:
text += page.extract_text()
return text
def qa_infer(self, query, rows, col):
"""QA inference function."""
print(query)
print(tuple(col))
file_index = -1
if "additional_info" not in col:
pass
else:
file_index = [i for i in range(len(col)) if col[i] == "additional_info"][0]
initiate_qa = input("\nDo you wish to ask questions about the properties [y/n]?: ").lower()
if initiate_qa in ['y', 'yes']:
for row in rows:
pdf_text = self.extract_text_from_pdf(row[file_index])
print("Extracted text from PDF", os.path.basename(row[file_index]))
while True:
user_question = input("\nWhat do you want to know about this property? (Press Enter to exit): ").strip()
if not user_question:
break
# Construct QA prompt directly here
question = user_question if user_question else "Who is lashkar e taiba"
prompt = f"""Below is a question and context, search the context to find the answer for the question and return the response ###question:{question} ###context:{pdf_text} ###response:"""
# Run the language model to generate a response
inputs = self.qa_tokenizer(prompt, return_tensors='pt', truncation=True, max_length=512)
pipe = pipeline(
"text-generation",
model=self.qa_model,
tokenizer=self.qa_tokenizer,
torch_dtype=torch.bfloat16,
device_map="auto"
)
sequences = pipe(
prompt,
do_sample=True,
max_new_tokens=200,
temperature=0.7,
top_k=50,
top_p=0.95,
num_return_sequences=1,
)
answer = sequences[0]['generated_text']
print("Answer:", answer)
else:
continue_to_next = input("Do you want to continue with the next property? [y/n]: ").lower()
if continue_to_next != 'y':
return
def qa_infer_interface(self, row, query_question):
"""This method is used for gradio interface only"""
file_path = row[-1] # Assuming the last element in row contains the PDF file path
pdf_text = self.extract_text_from_pdf(file_path)
# prompt = f"""Below is a question and context, search the context to find the answer for the question and return the response , if related answer cannot be found return "Answer not in the context" ###question:{query_question} ###context:{pdf_text} ###response:"""
prompt = f"""You have been provided with a question and a corresponding context. Your task is to search the context to find the answer to the question. If the answer is found, return the response. If the answer cannot be found in the context, please respond with "Answer not found in the context".
=== Question ===
{query_question}
=== Context ===
{pdf_text}
=== Response ===
Try mostly to answer from given pdf , if related answer is not found return 'Information not present in the pdf' and below it provide something related to the question .Note: return only answer dont include terms like 'Response','###','Answer'"""
print(prompt)
completion = self.chatgpt_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a expert PDF parser , go through the pdf and answer the question properly , if related answer is not found return 'Information not present in the pdf' and below it provide something related to the question"},
{"role": "user", "content": prompt }
]
)
return completion.choices[0].message.content
def qa_infer_interface_gemini(self, row, query_question):
"""This method is used for gradio interface only"""
file_path = row[-1] # Assuming the last element in row contains the PDF file path
pdf_text = self.extract_text_from_pdf(file_path)
# prompt = f"""Below is a question and context, search the context to find the answer for the question and return the response , if related answer cannot be found return "Answer not in the context" ###question:{query_question} ###context:{pdf_text} ###response:"""
prompt = f"""You have been provided with a question and a corresponding context. Your task is to search the context to find the answer to the question. If the answer is found, return the response. If the answer cannot be found in the context, please respond with "Answer not found in the context".
=== Question ===
{query_question}
=== Context ===
{pdf_text}
=== Response ===
If related answer is not found return 'Information not present in the pdf' and below it provide something related to the question"""
print(prompt)
completion = self.genai_model.generate_content(prompt)
generated_answer=completion.text
return generated_answer
if __name__ == '__main__':
qa_infer = QAInfer()
query = 'SELECT * FROM sql_pdf WHERE country = "India" '
rows = [
(3, 'Taj Mahal Palace', 455, 76, 0.15, 'Mumbai', 'India', 795, 748, 67000, 'pdf_files/pdf/Taj_Mahal_palace.pdf'),
(6, 'Antilia', 455, 70, 0.46, 'Mumbai', 'India', 612, 2520, 179000, 'pdf_files/pdf/Antilia.pdf')
]
col = [
"property_id", "name", "bed", "bath", "acre_lot", "city", "country",
"zip_code", "house_size", "price", "additional_info"
]
qa_infer.qa_infer(query, rows, col)