File size: 4,160 Bytes
2aea9eb
 
 
 
 
77d8ee7
2aea9eb
 
 
dc22f3f
2aea9eb
dc22f3f
2aea9eb
 
 
 
 
 
 
 
 
 
344a7e8
2aea9eb
 
 
344a7e8
 
 
 
 
 
2aea9eb
 
344a7e8
 
2aea9eb
 
 
 
 
dc4d491
 
2aea9eb
 
 
 
 
 
 
dc22f3f
 
 
 
 
 
 
 
 
 
2aea9eb
 
 
 
 
 
 
 
dc22f3f
2aea9eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0684c75
 
 
 
4965282
 
 
 
 
 
 
 
 
 
2aea9eb
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
import streamlit as st
import pandas as pd
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_openai.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from langchain.text_splitter import CharacterTextSplitter
from time import sleep

OPENAI_API_KEY = "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"
os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY

# Initialize variables
vectorstore = None
conversation_chain = None
chat_history = []

# Function to process uploaded CSV file
def process_csv(csv_file):
    try:
        df = pd.read_csv(csv_file, encoding='latin-1')  # Specify encoding as latin-1 or ISO-8859-1
        text = df.to_string(index=False)
        text_chunks = get_text_chunks(text)
        vectorstore = get_vectorstore(text_chunks)
        if vectorstore:
            conversation_chain = get_conversation_chain(vectorstore)
            return conversation_chain
        else:
            st.error("Failed to create vectorstore. Rate limit exceeded. Please try again later.")
            return None
    except Exception as e:
        st.error(f"Error processing CSV file: {e}")
        return None


# Function to split text into chunks
def get_text_chunks(text):
    text_splitter = CharacterTextSplitter(
        separator="\n",
        chunk_size=2000,  # Increased chunk size for larger datasets
        chunk_overlap=40,  # Increased chunk overlap for larger datasets
        length_function=len
    )
    chunks = text_splitter.split_text(text)
    return chunks

# Function to create vectorstore from text chunks
def get_vectorstore(text_chunks):
    retries = 5  # Increased number of retries for larger datasets
    for i in range(retries):
        try:
            embeddings = OpenAIEmbeddings()
            vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
            return vectorstore
        except Exception as e:
            st.warning(f"Retry {i+1}/{retries}: Waiting for 20 seconds due to rate limit exceeded.")
            sleep(20)
    return None

# Function to create conversation chain
def get_conversation_chain(vectorstore):
    llm = ChatOpenAI()
    memory = ConversationBufferMemory(
        memory_key='chat_history', return_messages=True)
    conversation_chain = ConversationalRetrievalChain.from_llm(
        llm=llm,
        retriever=vectorstore.as_retriever(),  # This might still raise an error if vectorstore is None
        memory=memory
    )
    return conversation_chain

# Streamlit app
def main():
    global vectorstore, conversation_chain, chat_history

    st.title('CSV Chatbot')

    # Page to upload CSV file
    st.subheader('Upload CSV File')
    csv_file = st.file_uploader('Upload CSV', type=['csv'])

    if csv_file:
        conversation_chain = process_csv(csv_file)

        if conversation_chain:
            # Chat interface
            st.subheader('Chat Interface')
            user_question = st.text_input('Ask a question:')
            if st.button('Ask'):
                # Split the user question into smaller parts
                questions = user_question.split('?')
                for question in questions:
                    response = conversation_chain.invoke({'question': question.strip()})
                    if 'chat_history' in response:
                        chat_history = response['chat_history']
                        for message in chat_history:
                            if isinstance(message, dict) and 'role' in message and 'content' in message:
                                if message['role'] == 'user':
                                    st.write(f"You: {message['content']}")
                                elif message['role'] == 'assistant':
                                    st.write(f"Assistant: {message['content']}")
                    else:
                        st.error("Failed to get response. Please try again.")
        else:
            st.error("Failed to process CSV file. Please try again.")

if __name__ == '__main__':
    main()