Spaces:
Running
Running
File size: 4,799 Bytes
b82f276 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | {
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "356e8efe-9836-42f4-b2b2-38513a0a573a",
"metadata": {},
"outputs": [],
"source": [
"import streamlit as st\n",
"from PyPDF2 import PdfReader\n",
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
"import os\n",
"from langchain_google_genai import GoogleGenerativeAIEmbeddings\n",
"import google.generativeai as genai\n",
"from langchain.vectorstores import FAISS\n",
"from langchain_google_genai import ChatGoogleGenerativeAI\n",
"from langchain.chains.question_answering import load_qa_chain\n",
"from langchain.prompts import PromptTemplate\n",
"from dotenv import load_dotenv\n",
"\n",
"load_dotenv()\n",
"os.getenv(\"GOOGLE_API_KEY\")\n",
"genai.configure(api_key=os.getenv(\"GOOGLE_API_KEY\"))\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"def get_pdf_text(pdf_docs):\n",
" text=\"\"\n",
" for pdf in pdf_docs:\n",
" pdf_reader= PdfReader(pdf)\n",
" for page in pdf_reader.pages:\n",
" text+= page.extract_text()\n",
" return text\n",
"\n",
"\n",
"\n",
"def get_text_chunks(text):\n",
" text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)\n",
" chunks = text_splitter.split_text(text)\n",
" return chunks\n",
"\n",
"\n",
"def get_vector_store(text_chunks):\n",
" embeddings = GoogleGenerativeAIEmbeddings(model = \"models/embedding-001\")\n",
" vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)\n",
" vector_store.save_local(\"faiss_index\")\n",
"\n",
"\n",
"def get_conversational_chain():\n",
"\n",
" prompt_template = \"\"\"\n",
" Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in\n",
" provided context just say, \"answer is not available in the context\", don't provide the wrong answer\\n\\n\n",
" Context:\\n {context}?\\n\n",
" Question: \\n{question}\\n\n",
"\n",
" Answer:\n",
" \"\"\"\n",
"\n",
" model = ChatGoogleGenerativeAI(model=\"gemini-pro\",\n",
" temperature=0.3)\n",
"\n",
" prompt = PromptTemplate(template = prompt_template, input_variables = [\"context\", \"question\"])\n",
" chain = load_qa_chain(model, chain_type=\"stuff\", prompt=prompt)\n",
"\n",
" return chain\n",
"\n",
"\n",
"\n",
"def user_input(user_question):\n",
" embeddings = GoogleGenerativeAIEmbeddings(model = \"models/embedding-001\")\n",
" \n",
" new_db = FAISS.load_local(\"faiss_index\", embeddings)\n",
" docs = new_db.similarity_search(user_question)\n",
"\n",
" chain = get_conversational_chain()\n",
"\n",
" \n",
" response = chain(\n",
" {\"input_documents\":docs, \"question\": user_question}\n",
" , return_only_outputs=True)\n",
"\n",
" print(response)\n",
" st.write(\"Reply: \", response[\"output_text\"])\n",
"\n",
"\n",
"\n",
"\n",
"def main():\n",
" st.set_page_config(\"Chat PDF\")\n",
" st.header(\"Chat with PDF using Gemini💁\")\n",
"\n",
" user_question = st.text_input(\"Ask a Question from the PDF Files\")\n",
"\n",
" if user_question:\n",
" user_input(user_question)\n",
"\n",
" with st.sidebar:\n",
" st.title(\"Menu:\")\n",
" pdf_docs = st.file_uploader(\"Upload your PDF Files and Click on the Submit & Process Button\", accept_multiple_files=True)\n",
" if st.button(\"Submit & Process\"):\n",
" with st.spinner(\"Processing...\"):\n",
" raw_text = get_pdf_text(pdf_docs)\n",
" text_chunks = get_text_chunks(raw_text)\n",
" get_vector_store(text_chunks)\n",
" st.success(\"Done\")\n",
"\n",
"\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|