| | |
| | import os |
| |
|
| | |
| | from dotenv import load_dotenv, find_dotenv |
| | import google.generativeai as palm |
| | import PyPDF2 |
| |
|
| | |
| | from langchain import PromptTemplate, LLMChain |
| | from langchain.chains import RetrievalQA |
| | from langchain.chains.question_answering import load_qa_chain |
| | from langchain.document_loaders import UnstructuredPDFLoader, UnstructuredURLLoader |
| | from langchain.embeddings import GooglePalmEmbeddings |
| | from langchain.indexes import VectorstoreIndexCreator |
| | from langchain.llms import GooglePalm |
| | from langchain.text_splitter import CharacterTextSplitter |
| |
|
| | |
| | import gradio as gr |
| |
|
| | |
| | load_dotenv(find_dotenv()) |
| |
|
| | |
| | api_key = os.environ["GOOGLE_API_KEY"] |
| | palm.configure(api_key=api_key) |
| |
|
| | |
| | llm = GooglePalm() |
| | llm.temperature = 0.1 |
| |
|
| | |
| | models = [ |
| | m for m in palm.list_models() if "generateText" in m.supported_generation_methods |
| | ] |
| | print(f"There are {len(models)} model(s) available.") |
| |
|
| | |
| | index_creator = VectorstoreIndexCreator( |
| | embedding=GooglePalmEmbeddings(), |
| | text_splitter=CharacterTextSplitter(chunk_size=1000, chunk_overlap=0), |
| | ) |
| |
|
| | |
| | def pdf_to_text(file_obj): |
| | |
| | pdf_file = open(file_obj.name, 'rb') |
| | pdf_reader = PyPDF2.PdfFileReader(pdf_file) |
| | |
| | num_pages = pdf_reader.numPages |
| | |
| | text_content = "" |
| | |
| | for i in range(num_pages): |
| | page = pdf_reader.getPage(i) |
| | text_content += page.extractText() |
| | |
| | pdf_file.close() |
| | |
| | return text_content |
| |
|
| | |
| | def answer_question(question, pdf_file): |
| | |
| | pdf_text = pdf_to_text(pdf_file) |
| | |
| | pdf_loader = UnstructuredPDFLoader(pdf_text) |
| | |
| | pdf_index = index_creator.from_loaders([pdf_loader]) |
| | |
| | pdf_chain = RetrievalQA.from_chain_type( |
| | llm=llm, |
| | chain_type="stuff", |
| | retriever=pdf_index.vectorstore.as_retriever(), |
| | input_key="question", |
| | ) |
| | |
| | return pdf_chain.run(question) |
| |
|
| | |
| | template = """ |
| | You are an artificial intelligence assistant working for Raising The village. You are asked to answer questions. The assistant gives helpful, detailed, and polite answers to the user's questions. |
| | |
| | {question} |
| | |
| | """ |
| |
|
| | |
| | prompt = PromptTemplate(template=template, input_variables=["question"]) |
| |
|
| | |
| | llm_chain = LLMChain(prompt=prompt, llm=llm, verbose=True) |
| |
|
| | |
| | interface = gr.Interface( |
| | fn=answer_question, |
| | inputs=["text", gr.inputs.File(file_types=['.pdf'])], |
| | outputs="text", |
| | title="AI Assistant", |
| | description="Ask me anything about Raising The Village" |
| | ) |
| |
|
| | |
| | interface.launch(share=True) |
| |
|