How do I get a response when I call
response = gemini_pdf(message)
I want response
to contain the final output. I’m stuck here.
Here is my code:
def read_pdf(file_path):
global file_content
global file_name
# Read the contents of the PDF file
pdfreader = PdfReader(file_path)
from typing_extensions import Concatenate
# read text from pdf
raw_text = ''
try:
for i, page in enumerate(pdfreader.pages):
content = page.extract_text()
if content:
raw_text += content
print(raw_text)
text_splitter = CharacterTextSplitter(
separator = "n",
chunk_size = 800,
chunk_overlap = 200,
length_function = len,
)
new_content = text_splitter.split_text(raw_text)
len(new_content)
embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
file_content = FAISS.from_texts(new_content, embeddings)
except TypeError as e:
print("Sorry, an error occurred while processing your request.", f"{e}")
pass
except FileNotFoundError as e:
print("Sorry, an error occurred while processing your request.", f"{e}")
pass
except Exception as e:
print("Sorry, an error occurred while processing your request.", f"{e}")
tk.messagebox.showwarning("An error occurred", f"{e}")
return
def answer_ftom_pdf(query):
global file_content
model = ChatGoogleGenerativeAI(model="gemini-1.5-flash-latest",
temperature=0.3)
#prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
docs = file_content.similarity_search(query)
chain = load_qa_chain(model, chain_type="stuff")
if file_content != None:
response = chain.run(input_documents=docs, question=query)
print(response)
response = response.strip()
print("Here")
return response
if file_content == None:
print("Content is none")
def gemini_pdf(query=None):
global file_content
global file_name
if file_content is None:
# Open a file dialog to select the PDF file
file_path = filedialog.askopenfilename(filetypes=[("PDF files", "*.pdf")])
file_name = os.path.basename(file_path)
if file_name:
threading.Thread(target=read_pdf, args=(file_path,)).start()
elif file_content:
print("Same PDF")
threading.Thread(target=answer_ftom_pdf, args=(query,)).start()
How can I ensure response
contains the final output when calling gemini_pdf(message)