uyen13 commited on
Commit
360fa53
Β·
1 Parent(s): d83c9d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -57
app.py CHANGED
@@ -19,77 +19,72 @@ def load_llm():
19
  temperature=0.5
20
  )
21
  return llm
22
- # Function for conversational chat
23
- def conversational_chat(query):
24
- result = chain({"question": query, "chat_history": st.session_state['history']})
25
- st.session_state['history'].append((query, result["answer"]))
26
- return result["answer"]
27
- def main():
28
- # Set the title for the Streamlit app
29
- st.title("Llama2 Chat CSV - πŸ¦œπŸ¦™")
30
 
31
- # Create a file uploader in the sidebar
32
- uploaded_file = st.sidebar.file_uploader("Upload File", type="csv")
33
 
34
- # Handle file upload
35
- if uploaded_file:
36
- with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
37
- tmp_file.write(uploaded_file.getvalue())
38
- tmp_file_path = tmp_file.name
39
 
40
- # Load CSV data using CSVLoader
41
- loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8", csv_args={'delimiter': ','})
42
- data = loader.load()
 
 
43
 
44
- # Create embeddings using Sentence Transformers
45
- embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2', model_kwargs={'device': 'cpu'})
 
46
 
47
- # Create a FAISS vector store and save embeddings
48
- db = FAISS.from_documents(data, embeddings)
49
- db.save_local(DB_FAISS_PATH)
50
 
 
 
 
51
 
52
- # Load the language model
53
- llm = load_llm()
54
 
55
- # Create a conversational chain
56
- chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever())
57
 
58
-
 
59
 
60
- # Initialize chat history
61
- if 'history' not in st.session_state:
62
- st.session_state['history'] = []
 
 
63
 
64
- # Initialize messages
65
- if 'generated' not in st.session_state:
66
- st.session_state['generated'] = ["Hello ! Ask me(LLAMA2) about " + uploaded_file.name + " πŸ€—"]
67
 
68
- if 'past' not in st.session_state:
69
- st.session_state['past'] = ["Hey ! πŸ‘‹"]
 
70
 
71
- # Create containers for chat history and user input
72
- response_container = st.container()
73
- container = st.container()
74
 
75
- # User input form
76
- with container:
77
- with st.form(key='my_form', clear_on_submit=True):
78
- user_input = st.text_input("Query:", placeholder="Talk to csv data πŸ‘‰ (:", key='input')
79
- submit_button = st.form_submit_button(label='Send')
80
 
81
- if submit_button and user_input:
82
- output = conversational_chat(user_input)
83
- st.session_state['past'].append(user_input)
84
- st.session_state['generated'].append(output)
 
85
 
86
- # Display chat history
87
- if st.session_state['generated']:
88
- with response_container:
89
- for i in range(len(st.session_state['generated'])):
90
- message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
91
- message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")
92
 
93
-
94
- if __name__ == "__main__":
95
- main()
 
 
 
 
19
  temperature=0.5
20
  )
21
  return llm
 
 
 
 
 
 
 
 
22
 
23
+ # Set the title for the Streamlit app
24
+ st.title("Llama2 Chat CSV - πŸ¦œπŸ¦™")
25
 
26
+ # Create a file uploader in the sidebar
27
+ uploaded_file = st.sidebar.file_uploader("Upload File", type="csv")
 
 
 
28
 
29
+ # Handle file upload
30
+ if uploaded_file:
31
+ with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
32
+ tmp_file.write(uploaded_file.getvalue())
33
+ tmp_file_path = tmp_file.name
34
 
35
+ # Load CSV data using CSVLoader
36
+ loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8", csv_args={'delimiter': ','})
37
+ data = loader.load()
38
 
39
+ # Create embeddings using Sentence Transformers
40
+ embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2', model_kwargs={'device': 'cpu'})
 
41
 
42
+ # Create a FAISS vector store and save embeddings
43
+ db = FAISS.from_documents(data, embeddings)
44
+ db.save_local(DB_FAISS_PATH)
45
 
 
 
46
 
47
+ # Load the language model
48
+ llm = load_llm()
49
 
50
+ # Create a conversational chain
51
+ chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever())
52
 
53
+ # Function for conversational chat
54
+ def conversational_chat(query):
55
+ result = chain({"question": query, "chat_history": st.session_state['history']})
56
+ st.session_state['history'].append((query, result["answer"]))
57
+ return result["answer"]
58
 
59
+ # Initialize chat history
60
+ if 'history' not in st.session_state:
61
+ st.session_state['history'] = []
62
 
63
+ # Initialize messages
64
+ if 'generated' not in st.session_state:
65
+ st.session_state['generated'] = ["Hello ! Ask me(LLAMA2) about " + uploaded_file.name + " πŸ€—"]
66
 
67
+ if 'past' not in st.session_state:
68
+ st.session_state['past'] = ["Hey ! πŸ‘‹"]
 
69
 
70
+ # Create containers for chat history and user input
71
+ response_container = st.container()
72
+ container = st.container()
 
 
73
 
74
+ # User input form
75
+ with container:
76
+ with st.form(key='my_form', clear_on_submit=True):
77
+ user_input = st.text_input("Query:", placeholder="Talk to csv data πŸ‘‰ (:", key='input')
78
+ submit_button = st.form_submit_button(label='Send')
79
 
80
+ if submit_button and user_input:
81
+ output = conversational_chat(user_input)
82
+ st.session_state['past'].append(user_input)
83
+ st.session_state['generated'].append(output)
 
 
84
 
85
+ # Display chat history
86
+ if st.session_state['generated']:
87
+ with response_container:
88
+ for i in range(len(st.session_state['generated'])):
89
+ message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
90
+ message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")