CoolFace
Apppublic

Balaji747/chat_with_docs

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
app.py94 linesDownload Raw Back to root
1import streamlit as st2import tempfile3import os4from langchain_community.document_loaders import CSVLoader, TextLoader5from langchain_text_splitters import RecursiveCharacterTextSplitter6from dotenv import load_dotenv7import google.generativeai as genai8from langchain_google_genai import GoogleGenerativeAIEmbeddings9from langchain_google_genai import ChatGoogleGenerativeAI10from langchain_community.vectorstores import FAISS11from langchain.prompts import PromptTemplate12from langchain.chains.question_answering import load_qa_chain13 14load_dotenv()15os.getenv("GOOGLE_API_KEY")16genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))17 18 19 20def data_loader(data_file, uploaded_file):21    if uploaded_file.name.endswith(".csv"):     22        loader = CSVLoader(file_path=data_file, encoding='utf-8', csv_args={'delimiter':','})23    elif uploaded_file.name.endswith(".txt"):24        loader = TextLoader(file_path=data_file)25    else:26        st.warning("Unsupported File")27    data = loader.load()28    return data29 30def text_splitter(data):31    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=500)32    chunks = text_splitter.split_documents(data)33    return chunks34 35def data_embadding(chunks):36    embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")37    db = FAISS.from_documents(chunks, embeddings)38    return db39 40def get_conversational_chain():41    prompt_template = """42    Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in43    provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n44    Context:\n {context}?\n45    Question: \n{question}\n46    Answer:47    """48    model = ChatGoogleGenerativeAI(model="gemini-pro",temperature=0.3)49    prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])50    chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)51    return chain52 53 54 55def user_input(user_question, db):56    57    doc = db.similarity_search(user_question,k=3)58    chain = get_conversational_chain()59    response = chain(60        {"input_documents":doc, "question": user_question},61        return_only_outputs=True)62    st.write("Reply: ", response["output_text"])63 64 65def main():66    st.set_page_config("Chat With Document")67    68    st.header("Chat With Documnet")69 70    uploaded_file = st.file_uploader("Upload Document-Support",type=((["csv","txt"])))71        72    #if st.button("Submit & Process"):73    if uploaded_file is not None:74        if uploaded_file.name.endswith((".csv", ".txt")):75            76            with tempfile.NamedTemporaryFile(delete=False) as tmp_file:77                tmp_file.write(uploaded_file.getvalue())78                tmp_file_path = tmp_file.name79                    80            data = data_loader(tmp_file_path,uploaded_file)    81            chunks = text_splitter(data)82            db = data_embadding(chunks)83            st.success("Done")84                85            user_question = st.text_input("Ask a Question from the Document")86            if user_question:87                user_input(user_question,db)88        else:89            st.warning("Support Only .csv,.txt Files")90    else:91        st.text("Kindly upload the Document")92            93if __name__ == "__main__":94    main()