What I’m trying to do:
the python code below uses the open API to read and query a pdf document found in a google drive and display on a anvil built webpag. when the user inputs a question and clicks submit, the code will read the document.
What I’ve tried and what’s not working:
I believe the code is not properly connected to the anvil site. But I am not sure.
**Code Sample:
!pip install anvil-uplink
anvil.server
anvil.server.connect("server_5YKPABRFZBFLS3ZEVOL4DEOD-QHNRIHAH2NGYOK2A")
!pip install langchain
!pip install openai
!pip install PyPDF2
!pip install faiss-cpu
!pip install tiktoken
from PyPDF2 import PdfReader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import ElasticVectorSearch, Pinecone, Weaviate, FAISS
import os
os.environ["OPENAI_API_KEY"] = "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"
from google.colab import drive
drive.mount('/content/gdrive', force_remount=True)
root_dir = "/content/gdrive/My Drive/"
reader = PdfReader('/content/gdrive/My Drive/Google Docs/AI—Blessing or Curse.pdf')
reader
raw_text = ''
for i, page in enumerate(reader.pages):
text = page.extract_text()
if text:
raw_text += text
raw_text[:100]
text_splitter = CharacterTextSplitter(
separator = "\n",
chunk_size = 1000,
chunk_overlap = 200,
length_function = len,
)
texts = text_splitter.split_text(raw_text)
len(texts)
embeddings = OpenAIEmbeddings()
docsearch = FAISS.from_texts(texts, embeddings)
docsearch
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
chain = load_qa_chain(OpenAI(), chain_type="stuff")
@anvil.server.callable
def enter_question(question):
results = chain.search(docsearch, question)
return results
@anvil.server.callable
def answer_question(results):
top_result = results[0]
answer = top_result['answer']
confidence = top_result['confidence']
success = top_result['success']
return answer, confidence, success
anvil.server.wait_forever()
Clone link:
share a copy of your app
