import os
import pandas as pd
import math
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import CharacterTextSplitter
from langchain import OpenAI, VectorDBQA, OpenAI
from langchain.llms import OpenAIChat
from langchain.document_loaders import DirectoryLoader
import langchain
from langchain.chains import RetrievalQA
from langchain.chat_models import ChatOpenAI
import streamlit as st
__import__('pysqlite3')
import sys
sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')


st.title("GPT module (TEST)")

openai_api_key = st.text_input(
        "API Key", 
        help="Enter Open Ai API Key")

os.environ["OPENAI_API_KEY"] = openai_api_key

query = st.text_input(
"User Query", 
help="Enter a question about rviews"
,value="What users complain about?")

# read file
uploaded_file = st.file_uploader("Choose a csv file")
if st.button("Let's go"):
    # if uploaded_file is not None:
    df = pd.read_csv(uploaded_file)
    st.write(df)

    loader = langchain.document_loaders.DataFrameLoader(df, 'review_text')
    documents = loader.load()
    text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
    texts = text_splitter.split_documents(documents)
    embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
    docsearch = Chroma.from_documents(texts, embeddings)
    qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever = docsearch.as_retriever())

    
    
    # if st.button("Get answer"):
    a=st.write(qa.run(query))
    st.write(a)
langchain.text_splitter.CharacterTextSplitter langchain.vectorstores.Chroma.from_documents langchain.document_loaders.DataFrameLoader langchain.OpenAI langchain.embeddings.openai.OpenAIEmbeddings
[((527, 555), 'sys.modules.pop', 'sys.modules.pop', (['"""pysqlite3"""'], {}), "('pysqlite3')\n", (542, 555), False, 'import sys\n'), ((558, 587), 'streamlit.title', 'st.title', (['"""GPT module (TEST)"""'], {}), "('GPT module (TEST)')\n", (566, 587), True, 'import streamlit as st\n'), ((606, 660), 'streamlit.text_input', 'st.text_input', (['"""API Key"""'], {'help': '"""Enter Open Ai API Key"""'}), "('API Key', help='Enter Open Ai API Key')\n", (619, 660), True, 'import streamlit as st\n'), ((735, 841), 'streamlit.text_input', 'st.text_input', (['"""User Query"""'], {'help': '"""Enter a question about rviews"""', 'value': '"""What users complain about?"""'}), "('User Query', help='Enter a question about rviews', value=\n    'What users complain about?')\n", (748, 841), True, 'import streamlit as st\n'), ((868, 905), 'streamlit.file_uploader', 'st.file_uploader', (['"""Choose a csv file"""'], {}), "('Choose a csv file')\n", (884, 905), True, 'import streamlit as st\n'), ((909, 930), 'streamlit.button', 'st.button', (['"""Let\'s go"""'], {}), '("Let\'s go")\n', (918, 930), True, 'import streamlit as st\n'), ((977, 1003), 'pandas.read_csv', 'pd.read_csv', (['uploaded_file'], {}), '(uploaded_file)\n', (988, 1003), True, 'import pandas as pd\n'), ((1008, 1020), 'streamlit.write', 'st.write', (['df'], {}), '(df)\n', (1016, 1020), True, 'import streamlit as st\n'), ((1035, 1096), 'langchain.document_loaders.DataFrameLoader', 'langchain.document_loaders.DataFrameLoader', (['df', '"""review_text"""'], {}), "(df, 'review_text')\n", (1077, 1096), False, 'import langchain\n'), ((1147, 1204), 'langchain.text_splitter.CharacterTextSplitter', 'CharacterTextSplitter', ([], {'chunk_size': '(1000)', 'chunk_overlap': '(100)'}), '(chunk_size=1000, chunk_overlap=100)\n', (1168, 1204), False, 'from langchain.text_splitter import CharacterTextSplitter\n'), ((1275, 1322), 'langchain.embeddings.openai.OpenAIEmbeddings', 'OpenAIEmbeddings', ([], {'openai_api_key': 'openai_api_key'}), '(openai_api_key=openai_api_key)\n', (1291, 1322), False, 'from langchain.embeddings.openai import OpenAIEmbeddings\n'), ((1339, 1379), 'langchain.vectorstores.Chroma.from_documents', 'Chroma.from_documents', (['texts', 'embeddings'], {}), '(texts, embeddings)\n', (1360, 1379), False, 'from langchain.vectorstores import Chroma\n'), ((1568, 1579), 'streamlit.write', 'st.write', (['a'], {}), '(a)\n', (1576, 1579), True, 'import streamlit as st\n'), ((1421, 1429), 'langchain.OpenAI', 'OpenAI', ([], {}), '()\n', (1427, 1429), False, 'from langchain import OpenAI, VectorDBQA, OpenAI\n')]