Team Ai
Apppublic

devopscloudram/policyAI

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py103 linesDownload Raw Back to root
1import streamlit as st2from PyPDF2 import PdfReader3from langchain.text_splitter import RecursiveCharacterTextSplitter4import os5from langchain_google_genai import GoogleGenerativeAIEmbeddings6import google.generativeai as genai7from langchain.vectorstores import FAISS8from langchain_google_genai import ChatGoogleGenerativeAI9from langchain.chains.question_answering import load_qa_chain10from langchain.prompts import PromptTemplate11from dotenv import load_dotenv12from langchain_community.vectorstores import FAISS13import asyncio14 15load_dotenv()16os.getenv("GOOGLE_API_KEY")17genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))18 19 20def get_pdf_text(pdf_docs):21    text=""22    for pdf in pdf_docs:23        pdf_reader= PdfReader(pdf)24        for page in pdf_reader.pages:25            text+= page.extract_text()26    return  text27 28 29 30def get_text_chunks(text):31    text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)32    chunks = text_splitter.split_text(text)33    return chunks34 35 36def get_vector_store(text_chunks):37    embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")38    vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)39    vector_store.save_local("faiss_index")40 41 42def get_conversational_chain():43 44    prompt_template = """45    Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in46    provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n47    Context:\n {context}?\n48    Question: \n{question}\n49 50    Answer:51    """52 53    model = ChatGoogleGenerativeAI(model="gemini-pro",54                             temperature=0.3)55 56    prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])57    chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)58 59    return chain60 61def user_input(user_question):62    embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")63    64    new_db = FAISS.load_local("faiss_index", embeddings,allow_dangerous_deserialization=True)65    docs = new_db.similarity_search(user_question)66 67    chain = get_conversational_chain()68 69    70    response = chain(71        {"input_documents":docs, "question": user_question}72        , return_only_outputs=True)73 74    print(response)75    st.write("Reply: ", response["output_text"])76 77async def your_async_function():78#def main():79    st.set_page_config("GenAI Ramz")80 81    st.header("Chat with Ramz using Gen-AI💁")82 83    user_question = st.text_input("Ask a Question from the PDF Files :question:")84 85    if user_question:86        user_input(user_question)87 88    with st.sidebar:89        st.title("AI-manifest:")90        pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)91        if st.button("Submit & Process :point_left:"):92            with st.spinner('Processing... :person_in_motorized_wheelchair:'):93                raw_text = get_pdf_text(pdf_docs)94                text_chunks = get_text_chunks(raw_text)95                get_vector_store(text_chunks)96                st.success("Upload Done :wave:")97 98 99 100if __name__ == "__main__":101    #main()102    asyncio.run(your_async_function())103