Upload 2 files
Browse files- app.py +46 -0
- requirements.txt +5 -0
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,46 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import streamlit as st
         | 
| 2 | 
            +
            from langchain.llms import OpenAI
         | 
| 3 | 
            +
            from langchain.text_splitter import CharacterTextSplitter
         | 
| 4 | 
            +
            from langchain.embeddings import OpenAIEmbeddings
         | 
| 5 | 
            +
            from langchain.vectorstores import Chroma
         | 
| 6 | 
            +
            from langchain.chains import RetrievalQA
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            def generate_response(uploaded_file, openai_api_key, query_text):
         | 
| 9 | 
            +
                # Load document if file is uploaded
         | 
| 10 | 
            +
                if uploaded_file is not None:
         | 
| 11 | 
            +
                    documents = [uploaded_file.read().decode()]
         | 
| 12 | 
            +
                    # Split documents into chunks
         | 
| 13 | 
            +
                    text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
         | 
| 14 | 
            +
                    texts = text_splitter.create_documents(documents)
         | 
| 15 | 
            +
                    # Select embeddings
         | 
| 16 | 
            +
                    embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
         | 
| 17 | 
            +
                    # Create a vectorstore from documents
         | 
| 18 | 
            +
                    db = Chroma.from_documents(texts, embeddings)
         | 
| 19 | 
            +
                    # Create retriever interface
         | 
| 20 | 
            +
                    retriever = db.as_retriever()
         | 
| 21 | 
            +
                    # Create QA chain
         | 
| 22 | 
            +
                    qa = RetrievalQA.from_chain_type(llm=OpenAI(openai_api_key=openai_api_key), chain_type='stuff', retriever=retriever)
         | 
| 23 | 
            +
                    return qa.run(query_text)
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            # Page title
         | 
| 26 | 
            +
            st.set_page_config(page_title='π¦π Ask the Doc App')
         | 
| 27 | 
            +
            st.title('π¦π Ask the Doc App')
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            # File upload
         | 
| 30 | 
            +
            uploaded_file = st.file_uploader('Upload an article', type='txt')
         | 
| 31 | 
            +
            # Query text
         | 
| 32 | 
            +
            query_text = st.text_input('Enter your question:', placeholder = 'Please provide a short summary.', disabled=not uploaded_file)
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            # Form input and query
         | 
| 35 | 
            +
            result = []
         | 
| 36 | 
            +
            with st.form('myform', clear_on_submit=True):
         | 
| 37 | 
            +
                openai_api_key = st.text_input('OpenAI API Key', type='password', disabled=not (uploaded_file and query_text))
         | 
| 38 | 
            +
                submitted = st.form_submit_button('Submit', disabled=not(uploaded_file and query_text))
         | 
| 39 | 
            +
                if submitted and openai_api_key.startswith('sk-'):
         | 
| 40 | 
            +
                    with st.spinner('Calculating...'):
         | 
| 41 | 
            +
                        response = generate_response(uploaded_file, openai_api_key, query_text)
         | 
| 42 | 
            +
                        result.append(response)
         | 
| 43 | 
            +
                        del openai_api_key
         | 
| 44 | 
            +
             | 
| 45 | 
            +
            if len(result):
         | 
| 46 | 
            +
                st.info(response)
         | 
    	
        requirements.txt
    ADDED
    
    | @@ -0,0 +1,5 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            streamlit
         | 
| 2 | 
            +
            langchain-community
         | 
| 3 | 
            +
            openai
         | 
| 4 | 
            +
            chromadb
         | 
| 5 | 
            +
            tiktoken
         |