pdfGPT
                                
                                 pdfGPT copied to clipboard
                                
                                    pdfGPT copied to clipboard
                            
                            
                            
                        Problem with class "SemanticSearch"
when running the application i get an error which says: "AttributeError: 'SemanticSearch' object has no attribute 'nn' "
any clues as to why and how to fix the issue? Anyway i love the idea thank you for the repo!
so I found the problem, if anybody got the same issue here's my fix the problem is inside the method "load_recommender", there is an if statement which checks if the pdf file is already embedded, when true line 110 fits the model without instantiating the class NearestNeighbors to fix this i created a method fit2 to be called in the if case
def fit2(self, data, embeddings_file, n_neighbors=5): self.data = data self.embeddings = np.load(embeddings_file) n_neighbors = min(n_neighbors, len(self.embeddings)) self.nn = NearestNeighbors(n_neighbors=n_neighbors) self.nn.fit(self.embeddings) self.fitted = True
you just have to add this to the original code replacing the original def fit or you just put in under the original? Any other changes that I need to make??? Sorry, I am not a coder.
no that would not work, you should create a new function inside the SemanticSearch class (so just paste fit2 under the existing fit function) and then you have to replace the load_recommender method with something like this:
def load_recommender(path, start_page=1): global recommender pdf_file = os.path.basename(path) embeddings_file = f"{pdf_file}_{start_page}.npy" texts = pdf_to_text(path, start_page=start_page) chunks = text_to_chunks(texts, start_page=start_page) if os.path.isfile(embeddings_file): recommender.fit2(chunks, embeddings_file) return "Embeddings loaded from file" recommender.fit(chunks) np.save(embeddings_file, recommender.embeddings) return 'Corpus Loaded.'
It works for the first question, after that I get this error. Any idea what might be wrong? Thank you very much for your help!!
Traceback (most recent call last):
File "C:\Users\Enrique\AppData\Local\Programs\Python\Python311\Lib\site-packages\gradio\routes.py", line 395, in run_predict
output = await app.get_blocks().process_api(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Enrique\AppData\Local\Programs\Python\Python311\Lib\site-packages\gradio\blocks.py", line 1193, in process_api
result = await self.call_function(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Enrique\AppData\Local\Programs\Python\Python311\Lib\site-packages\gradio\blocks.py", line 916, in call_function
prediction = await anyio.to_thread.run_sync(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Enrique\AppData\Local\Programs\Python\Python311\Lib\site-packages\anyio\to_thread.py", line 31, in run_sync
return await get_asynclib().run_sync_in_worker_thread(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Enrique\AppData\Local\Programs\Python\Python311\Lib\site-packages\anyio_backends_asyncio.py", line 937, in run_sync_in_worker_thread
return await future
^^^^^^^^^^^^
File "C:\Users\Enrique\AppData\Local\Programs\Python\Python311\Lib\site-packages\anyio_backends_asyncio.py", line 867, in run
result = context.run(func, *args)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Enrique\desktop\PROYECTO PDF CHAT\app23.py", line 200, in question_answer
return generate_answer(question,openAI_key)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Enrique\desktop\PROYECTO PDF CHAT\app23.py", line 156, in generate_answer
topn_chunks = recommender(question)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Enrique\desktop\PROYECTO PDF CHAT\app23.py", line 86, in call
return [self.data[i] for i in neighbors]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Enrique\desktop\PROYECTO PDF CHAT\app23.py", line 86, in 
Please get the latest code. It should work now.