import streamlit as st
from langchain.llms import OpenAI
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.chains import RetrievalQA

def generate_response(uploaded_file, openai_api_key, query_text):
    # Load document if file is uploaded
    if uploaded_file is not None:
        documents = [uploaded_file.read().decode()]
    # Split documents into chunks
    text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
    texts = text_splitter.create_documents(documents)
    # Select embeddings
    embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
    # Create a vectorstore from documents
    db = Chroma.from_documents(texts, embeddings)
    # Create retriever interface
    retriever = db.as_retriever()
    # Create QA chain
    qa = RetrievalQA.from_chain_type(llm=OpenAI(openai_api_key=openai_api_key), chain_type='stuff', retriever=retriever)
    return qa.run(query_text)

# Page title
st.set_page_config(page_title='🦜🔗 Ask the Doc App')
st.title('🦜🔗 Ask the Doc App')

# File upload
uploaded_file = st.file_uploader('Upload an article', type='txt')
# Query text
query_text = st.text_input('Enter your question:', placeholder = 'Please provide a short summary.', disabled=not uploaded_file)

# Form input and query
result = []
with st.form('myform', clear_on_submit=True):
    openai_api_key = st.text_input('OpenAI API Key', type='password', disabled=not (uploaded_file and query_text))
    submitted = st.form_submit_button('Submit', disabled=not(uploaded_file and query_text))
    if submitted and openai_api_key.startswith('sk-'):
        with st.spinner('Calculating...'):
            response = generate_response(uploaded_file, openai_api_key, query_text)
            result.append(response)
            del openai_api_key

if len(result):
    st.info(response)

langchain.text_splitter.CharacterTextSplitter langchain.embeddings.OpenAIEmbeddings langchain.llms.OpenAI langchain.vectorstores.Chroma.from_documents
[((1040, 1091), 'streamlit.set_page_config', 'st.set_page_config', ([], {'page_title': '"""🦜🔗 Ask the Doc App"""'}), "(page_title='🦜🔗 Ask the Doc App')\n", (1058, 1091), True, 'import streamlit as st\n'), ((1092, 1122), 'streamlit.title', 'st.title', (['"""🦜🔗 Ask the Doc App"""'], {}), "('🦜🔗 Ask the Doc App')\n", (1100, 1122), True, 'import streamlit as st\n'), ((1154, 1203), 'streamlit.file_uploader', 'st.file_uploader', (['"""Upload an article"""'], {'type': '"""txt"""'}), "('Upload an article', type='txt')\n", (1170, 1203), True, 'import streamlit as st\n'), ((1230, 1347), 'streamlit.text_input', 'st.text_input', (['"""Enter your question:"""'], {'placeholder': '"""Please provide a short summary."""', 'disabled': '(not uploaded_file)'}), "('Enter your question:', placeholder=\n    'Please provide a short summary.', disabled=not uploaded_file)\n", (1243, 1347), True, 'import streamlit as st\n'), ((495, 550), 'langchain.text_splitter.CharacterTextSplitter', 'CharacterTextSplitter', ([], {'chunk_size': '(1000)', 'chunk_overlap': '(0)'}), '(chunk_size=1000, chunk_overlap=0)\n', (516, 550), False, 'from langchain.text_splitter import CharacterTextSplitter\n'), ((646, 693), 'langchain.embeddings.OpenAIEmbeddings', 'OpenAIEmbeddings', ([], {'openai_api_key': 'openai_api_key'}), '(openai_api_key=openai_api_key)\n', (662, 693), False, 'from langchain.embeddings import OpenAIEmbeddings\n'), ((745, 785), 'langchain.vectorstores.Chroma.from_documents', 'Chroma.from_documents', (['texts', 'embeddings'], {}), '(texts, embeddings)\n', (766, 785), False, 'from langchain.vectorstores import Chroma\n'), ((1386, 1425), 'streamlit.form', 'st.form', (['"""myform"""'], {'clear_on_submit': '(True)'}), "('myform', clear_on_submit=True)\n", (1393, 1425), True, 'import streamlit as st\n'), ((1448, 1546), 'streamlit.text_input', 'st.text_input', (['"""OpenAI API Key"""'], {'type': '"""password"""', 'disabled': '(not (uploaded_file and query_text))'}), "('OpenAI API Key', type='password', disabled=not (\n    uploaded_file and query_text))\n", (1461, 1546), True, 'import streamlit as st\n'), ((1558, 1634), 'streamlit.form_submit_button', 'st.form_submit_button', (['"""Submit"""'], {'disabled': '(not (uploaded_file and query_text))'}), "('Submit', disabled=not (uploaded_file and query_text))\n", (1579, 1634), True, 'import streamlit as st\n'), ((1904, 1921), 'streamlit.info', 'st.info', (['response'], {}), '(response)\n', (1911, 1921), True, 'import streamlit as st\n'), ((916, 953), 'langchain.llms.OpenAI', 'OpenAI', ([], {'openai_api_key': 'openai_api_key'}), '(openai_api_key=openai_api_key)\n', (922, 953), False, 'from langchain.llms import OpenAI\n'), ((1702, 1730), 'streamlit.spinner', 'st.spinner', (['"""Calculating..."""'], {}), "('Calculating...')\n", (1712, 1730), True, 'import streamlit as st\n')]