MahmoudRox commited on
Commit
18127f7
1 Parent(s): 487f6c0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +251 -0
app.py ADDED
@@ -0,0 +1,251 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+
3
+ #sub_p_res = subprocess.run(['pip', 'install', 'langchain', 'sentence-transformers', 'transformers', 'faiss-gpu', 'PyPDF2', 'torch','llama-cpp-python'], stdout=subprocess.PIPE).stdout.decode('utf-8') #<cc-cm>
4
+ #print("pip install downloded ", sub_p_res)
5
+
6
+
7
+ #command = 'CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python'
8
+
9
+ #sub_p_res = subprocess.run(command, shell=True, check=True)
10
+
11
+ #print("llama-cpp-python GPU downloaded ",sub_p_res)
12
+
13
+
14
+ from langchain.document_loaders.text import TextLoader
15
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
16
+ from langchain.schema import Document
17
+ from langchain.embeddings import HuggingFaceEmbeddings
18
+ from langchain import PromptTemplate
19
+ from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
20
+ from langchain.callbacks.manager import CallbackManager
21
+
22
+
23
+ from langchain.vectorstores import FAISS
24
+ from langchain.chains import RetrievalQA
25
+
26
+ from langchain.memory import ConversationBufferMemory
27
+ from langchain.chains import ConversationalRetrievalChain
28
+
29
+ from huggingface_hub import hf_hub_download
30
+ from langchain.llms import LlamaCpp
31
+
32
+ import time
33
+
34
+ import streamlit as st
35
+
36
+ #from PyPDF2 import PdfReader
37
+
38
+ # from google.colab import drive
39
+ # drive.mount('/content/drive')
40
+
41
+ loader = TextLoader("./blog_data_1.txt")
42
+ pages = loader.load()
43
+
44
+ def split_text(documents: list[Document]):
45
+ text_splitter = RecursiveCharacterTextSplitter(
46
+ chunk_size=1000,
47
+ chunk_overlap=150,
48
+ length_function=len,
49
+ add_start_index=True,
50
+ )
51
+ chunks = text_splitter.split_documents(documents)
52
+ print(f"Split {len(documents)} documents into {len(chunks)} chunks.")
53
+
54
+ document = chunks[10]
55
+ print(document.page_content)
56
+ print(document.metadata)
57
+
58
+ return chunks
59
+
60
+ chunks_text = split_text(pages)
61
+
62
+ print("chunks")
63
+
64
+
65
+ # def Pdf_to_text(path) :
66
+ # pdf_reader = PdfReader(path)
67
+
68
+ # text = ""
69
+ # for page in pdf_reader.pages:
70
+ # text += page.extract_text()
71
+
72
+ # text_splitter = RecursiveCharacterTextSplitter(
73
+ # chunk_size=1000,
74
+ # chunk_overlap=200,
75
+ # length_function=len
76
+ # )
77
+ # chunks = text_splitter.split_text(text=text)
78
+ # return chunks
79
+
80
+ #chunks_pdf = Pdf_to_text("./drive/MyDrive/Colab Notebooks/hackathon_MoroccoAI/Doing-Business-Guide-Morocco.pdf")
81
+
82
+ #embeddings = HuggingFaceEmbeddings(model_name="all-mpnet-base-v2")
83
+ embedding = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2') # machi top
84
+
85
+ docs_text = [doc.page_content for doc in chunks_text]
86
+
87
+ # final_chunks = []
88
+
89
+ # # for chunk in chunks_pdf :
90
+ # # final_chunks.append(chunk)
91
+
92
+ # for chunk in docs_text :
93
+ # final_chunks.append(chunk)
94
+
95
+ VectorStore = FAISS.from_texts(docs_text, embedding=embedding)
96
+
97
+ MODEL_ID = "TheBloke/Mistral-7B-OpenOrca-GGUF"
98
+ MODEL_BASENAME = "mistral-7b-openorca.Q4_K_M.gguf"
99
+
100
+ model_path = hf_hub_download(
101
+ repo_id=MODEL_ID,
102
+ filename=MODEL_BASENAME,
103
+ resume_download=True,
104
+ )
105
+
106
+ print("model_path : ", model_path)
107
+
108
+ callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
109
+
110
+
111
+ CONTEXT_WINDOW_SIZE = 1500
112
+ MAX_NEW_TOKENS = 2000
113
+ N_BATCH = 512
114
+ n_gpu_layers = 40
115
+ kwargs = {
116
+ "model_path": model_path,
117
+ "n_ctx": CONTEXT_WINDOW_SIZE,
118
+ "max_tokens": MAX_NEW_TOKENS,
119
+ "n_batch": N_BATCH,
120
+ "n_gpu_layers": n_gpu_layers,
121
+ "callback_manager": callback_manager,
122
+ "verbose":True,
123
+ }
124
+
125
+ from langchain.callbacks.manager import CallbackManager
126
+ from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
127
+ from langchain.chains import LLMChain
128
+ from langchain.llms import LlamaCpp
129
+
130
+ # Callbacks support token-wise streaming
131
+ callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
132
+
133
+ n_gpu_layers = 40 # Change this value based on your model and your GPU VRAM pool.
134
+ n_batch = 512 # Should be between 1 and n_ctx, consider the amount of VRAM in your GPU.
135
+ max_tokens = 2000
136
+ # Make sure the model path is correct for your system!
137
+ llm = LlamaCpp(
138
+ model_path=model_path,
139
+ n_gpu_layers=n_gpu_layers,
140
+
141
+ n_batch=n_batch,
142
+ max_tokens= max_tokens,
143
+ callback_manager=callback_manager,
144
+ verbose=True, # Verbose is required to pass to the callback manager
145
+ )
146
+
147
+ llm = LlamaCpp(**kwargs)
148
+
149
+ memory = ConversationBufferMemory(
150
+ memory_key="chat_history",
151
+ return_messages=True,
152
+ input_key='question',
153
+ output_key='answer'
154
+ )
155
+
156
+ # memory.clear()
157
+
158
+ qa = ConversationalRetrievalChain.from_llm(
159
+ llm,
160
+ chain_type="stuff",
161
+ retriever=VectorStore.as_retriever(search_kwargs={"k": 5}),
162
+ memory=memory,
163
+ return_source_documents=True,
164
+ verbose=False,
165
+ )
166
+
167
+ # start = time.time()
168
+ # res = qa(f"""
169
+ # I'm intressted in starting the buisness in Casa , what I should do next?""")
170
+ # end = time.time()
171
+ # execution_time = end - start
172
+
173
+
174
+ #---------------------------------------------------------
175
+
176
+ import streamlit as st
177
+ import time
178
+
179
+ # App title
180
+ st.set_page_config(page_title="🤖💼 🇲🇦 Financial advisor is Here")
181
+
182
+ # Replicate Credentials
183
+ with st.sidebar:
184
+ st.title(' Mokawil.AI is Here 🤖💼 🇲🇦')
185
+ st.markdown('📖 an AI-powered advisor designed to assist founders (or anyone aspiring to start their own company) with various aspects of business in Morocco, including legal considerations, budget planning, available investors, and strategies for success.')
186
+
187
+ # Store LLM generated responses
188
+ if "messages" not in st.session_state.keys():
189
+ st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
190
+
191
+ # Display or clear chat messages
192
+ for message in st.session_state.messages:
193
+ if message["role"] == "user" :
194
+ with st.chat_message(message["role"], avatar="👨‍💻"):
195
+ st.write(message["content"])
196
+ else :
197
+ with st.chat_message(message["role"], avatar="🤖"):
198
+ st.write(message["content"])
199
+
200
+ def clear_chat_history():
201
+ st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
202
+
203
+ st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
204
+
205
+ # Function for generating LLaMA2 response
206
+ def generate_llama2_response(prompt_input):
207
+ res = qa(f'''{prompt_input}''')
208
+ return res['answer']
209
+
210
+ # User-provided prompt
211
+ if prompt := st.chat_input("What is up?"):
212
+ st.session_state.messages.append({"role": "user", "content": prompt})
213
+ with st.chat_message("user", avatar="👨‍💻"):
214
+ st.write(prompt)
215
+
216
+ # Generate a new response if last message is not from assistant
217
+ if st.session_state.messages[-1]["role"] != "assistant":
218
+ with st.chat_message("assistant", avatar="🤖"):
219
+ with st.spinner("Thinking..."):
220
+ response = generate_llama2_response(st.session_state.messages[-1]["content"])
221
+ placeholder = st.empty()
222
+ full_response = ''
223
+ for item in response:
224
+ full_response += item
225
+ placeholder.markdown(full_response)
226
+ time.sleep(0.05)
227
+ placeholder.markdown(full_response)
228
+ message = {"role": "assistant", "content": full_response}
229
+ st.session_state.messages.append(message)
230
+
231
+ # Example prompt
232
+ with st.sidebar :
233
+ st.title('Input examples')
234
+ def promptExample1():
235
+ prompt = "how can I start my company example 1"
236
+ st.session_state.messages.append({"role": "user", "content": prompt})
237
+
238
+ # Example prompt
239
+ def promptExample2():
240
+ prompt = "how can I start my company example 2"
241
+ st.session_state.messages.append({"role": "user", "content": prompt})
242
+
243
+ # Example prompt
244
+ def promptExample3():
245
+ prompt = "how can I start my company example 3"
246
+ st.session_state.messages.append({"role": "user", "content": prompt})
247
+
248
+
249
+ st.sidebar.button('how can I start my company in morocco?', on_click=promptExample1)
250
+ st.sidebar.button('What are some recommended cities for starting a business in finance', on_click=promptExample2)
251
+ st.sidebar.button('what is the estimate money I need for starting my company', on_click=promptExample3)