drkareemkamal commited on
Commit
7a3a8ae
·
verified ·
1 Parent(s): bdc19db

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -109
app.py DELETED
@@ -1,109 +0,0 @@
1
- import streamlit as st
2
- import os
3
- from streamlit_chat import message
4
- import tempfile
5
- #from langchain_community.documentloader.csv_loader import CSVLoader
6
- from langchain_community.document_loaders.csv_loader import CSVLoader
7
- from langchain_community.embeddings import HuggingFaceEmbeddings
8
- #from langchain_community.embeddings import HuggingFaceBgeEmbeddings
9
- from langchain.text_splitter import RecursiveCharacterTextSplitter
10
- from langchain_community.vectorstores import FAISS
11
- #from langchain_community.llms import CTransformers
12
- from langchain_community.llms.ctransformers import CTransformers
13
-
14
- from langchain.chains.conversational_retrieval.base import ConversationalRetrievalChain
15
-
16
- #from langchain.chains.conversational_retrieval.base import ConversationalRetreievalChain
17
-
18
-
19
- DB_FAISS_PATH = 'vectorstore/db_faiss'
20
- TEMP_DIR = 'temp'
21
- if not os.path.exists(TEMP_DIR):
22
- os.makedirs(TEMP_DIR)
23
-
24
- def load_llm():
25
- # load model from hugging face repo
26
- llm = CTransformers(
27
- model = 'TheBloke/Llama-2-7B-Chat-GGML',
28
- model_type = 'llama',
29
- max_new_token = 512,
30
- temperature = 0.5
31
- )
32
- return llm
33
-
34
- st.title("Chat with CSV using Llma 2")
35
- st.markdown("<h1 style='text-align: center; color: blue;'>Chat with your PDF 📄 </h1>", unsafe_allow_html=True)
36
- st.markdown("<h3 style='text-align: center; color: grey;'>Built by <a href='https://github.com/DrKareemKAmal'>MindSparks ❤️ </a></h3>", unsafe_allow_html=True)
37
-
38
- uploaded_file = st.sidebar.file_uploader('Upload your data', type=['csv'])
39
-
40
- if uploaded_file:
41
- # with tempfile.NamedTemporaryFile(delete=False)as temp_file :
42
- # temp_file.write(uploaded_file.getvalue())
43
- # tempfile_path = temp_file.name
44
- file_path = os.path.join(TEMP_DIR, uploaded_file.name)
45
- with open(file_path, "wb") as f:
46
- f.write(uploaded_file.getvalue())
47
-
48
- st.write(f"Uploaded file: {uploaded_file.name}")
49
- st.write("Processing CSV file...")
50
-
51
- loader = CSVLoader(file_path = file_path, encoding = 'utf-8',
52
- csv_args = {'delimiter': ','} )
53
- data = loader.load()
54
- #st.json(data)
55
-
56
- text_splitter = RecursiveCharacterTextSplitter(chunk_size = 500 , chunk_overlap = 20)
57
- text_chunks = text_splitter.split_documents(data)
58
- st.write (f"Total text chunks : {len(text_chunks)}")
59
-
60
- embeddings = HuggingFaceEmbeddings(
61
- model_name = 'sentence-transformers/all-MiniLM-L6-v2',
62
- # model_kwargs = {'device': 'cpu'}
63
- )
64
-
65
-
66
- db = FAISS.from_documents(text_chunks, embeddings)
67
- db.save_local (DB_FAISS_PATH)
68
- llm = load_llm()
69
-
70
- chain = ConversationalRetrievalChain.from_llm(llm= llm , retriever = db.as_retriever())
71
-
72
- def conversational_chat(query):
73
- result = chain({"quetion": query ,
74
- "chat_history": st.session_state['history']})
75
- st.session_state['history'].append((query , result['answer']))
76
- return result['answer']
77
-
78
- if 'history' not in st.session_state :
79
- st.session_state['history'] = []
80
-
81
- if 'generated' not in st.session_state :
82
- st.session_state['generated'] = ['Hello, Ask me anything about ' + uploaded_file.name]
83
-
84
- if 'past' not in st.session_state :
85
- st.session_state['past'] = ['Hey !']
86
-
87
- # Container for the chat history
88
- response_container = st.container()
89
- container = st.container()
90
-
91
- with container :
92
- with st.form(key = 'my_form',
93
- clear_on_submit=True):
94
- user_input = st.text_input('Query:', placeholder= "Talk to youur CSV Data here ")
95
- submit_button = st.form_submit_button(label = 'chat')
96
-
97
- if submit_button and user_input :
98
- output = conversational_chat(user_input)
99
-
100
- st.session_state['past'].append(user_input)
101
- st.session_state['generated'].append(output)
102
-
103
- if st.session_state['generated'] :
104
- with response_container:
105
- for i in range(len(st.session_state['generated'])):
106
- message(st.session_state['past'][i], is_user = True , key=str(i) + '_user',
107
- avatar_style='big-smile')
108
- message(st.session_state['generated'][i], key = str(i), avatar_style='thumb')
109
-