Spaces:
Runtime error
Runtime error
File size: 8,426 Bytes
18127f7 0af588c 18127f7 0af588c 18127f7 0af588c 18127f7 0af588c 18127f7 0af588c 18127f7 0af588c 18127f7 0af588c 18127f7 0af588c 18127f7 0af588c 18127f7 0af588c 18127f7 0af588c 18127f7 0af588c 18127f7 0af588c 18127f7 |
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
import subprocess
#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>
#print("pip install downloded ", sub_p_res)
#command = 'CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python'
#sub_p_res = subprocess.run(command, shell=True, check=True)
#print("llama-cpp-python GPU downloaded ",sub_p_res)
from langchain.document_loaders.text import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.schema import Document
from langchain.embeddings import HuggingFaceEmbeddings
from langchain import PromptTemplate
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.callbacks.manager import CallbackManager
from langchain.vectorstores import FAISS
from langchain.chains import RetrievalQA
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from huggingface_hub import hf_hub_download
from langchain.llms import LlamaCpp
import time
import streamlit as st
#from PyPDF2 import PdfReader
# from google.colab import drive
# drive.mount('/content/drive')
loader = TextLoader("./blog_data_1.txt")
pages = loader.load()
def split_text(documents: list[Document]):
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=150,
length_function=len,
add_start_index=True,
)
chunks = text_splitter.split_documents(documents)
print(f"Split {len(documents)} documents into {len(chunks)} chunks.")
document = chunks[10]
print(document.page_content)
print(document.metadata)
return chunks
chunks_text = split_text(pages)
print("chunks")
# def Pdf_to_text(path) :
# pdf_reader = PdfReader(path)
# text = ""
# for page in pdf_reader.pages:
# text += page.extract_text()
# text_splitter = RecursiveCharacterTextSplitter(
# chunk_size=1000,
# chunk_overlap=200,
# length_function=len
# )
# chunks = text_splitter.split_text(text=text)
# return chunks
#chunks_pdf = Pdf_to_text("./drive/MyDrive/Colab Notebooks/hackathon_MoroccoAI/Doing-Business-Guide-Morocco.pdf")
#embeddings = HuggingFaceEmbeddings(model_name="all-mpnet-base-v2")
embedding = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2') # machi top
docs_text = [doc.page_content for doc in chunks_text]
# final_chunks = []
# # for chunk in chunks_pdf :
# # final_chunks.append(chunk)
# for chunk in docs_text :
# final_chunks.append(chunk)
VectorStore = FAISS.from_texts(docs_text, embedding=embedding)
MODEL_ID = "TheBloke/Mistral-7B-OpenOrca-GGUF"
MODEL_BASENAME = "mistral-7b-openorca.Q4_K_M.gguf"
model_path = hf_hub_download(
repo_id=MODEL_ID,
filename=MODEL_BASENAME,
resume_download=True,
)
print("model_path : ", model_path)
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
CONTEXT_WINDOW_SIZE = 1500
MAX_NEW_TOKENS = 2000
N_BATCH = 512
n_gpu_layers = 40
kwargs = {
"model_path": model_path,
"n_ctx": CONTEXT_WINDOW_SIZE,
"max_tokens": MAX_NEW_TOKENS,
"n_batch": N_BATCH,
"n_gpu_layers": n_gpu_layers,
"callback_manager": callback_manager,
"verbose":True,
}
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.chains import LLMChain
from langchain.llms import LlamaCpp
# Callbacks support token-wise streaming
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
n_gpu_layers = 40 # Change this value based on your model and your GPU VRAM pool.
n_batch = 512 # Should be between 1 and n_ctx, consider the amount of VRAM in your GPU.
max_tokens = 2000
# Make sure the model path is correct for your system!
llm = LlamaCpp(
model_path=model_path,
n_gpu_layers=n_gpu_layers,
n_batch=n_batch,
max_tokens= max_tokens,
callback_manager=callback_manager,
verbose=True, # Verbose is required to pass to the callback manager
)
llm = LlamaCpp(**kwargs)
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True,
input_key='question',
output_key='answer'
)
# memory.clear()
qa = ConversationalRetrievalChain.from_llm(
llm,
chain_type="stuff",
retriever=VectorStore.as_retriever(search_kwargs={"k": 5}),
memory=memory,
return_source_documents=True,
verbose=False,
)
# start = time.time()
# res = qa(f"""
# I'm intressted in starting the buisness in Casa , what I should do next?""")
# end = time.time()
# execution_time = end - start
#---------------------------------------------------------
import streamlit as st
import time
# App title
st.set_page_config(page_title="🤖💼 🇲🇦 Financial advisor is Here")
# Replicate Credentials
with st.sidebar:
st.title('Mokawil.AI is Here 🤖💼 🇲🇦')
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.')
# Store LLM generated responses
if "messages" not in st.session_state.keys():
st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
# Display or clear chat messages
for message in st.session_state.messages:
if message["role"] == "user" :
with st.chat_message(message["role"], avatar="user.png"):
st.write(message["content"])
else :
with st.chat_message(message["role"], avatar="logo.png"):
st.write(message["content"])
def clear_chat_history():
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True,
input_key='question',
output_key='answer'
)
qa = ConversationalRetrievalChain.from_llm(
llm,
chain_type="stuff",
retriever=VectorStore.as_retriever(search_kwargs={"k": 5}),
memory=memory,
return_source_documents=True,
verbose=False,
)
st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
# Function for generating LLaMA2 response
def generate_llm_response(prompt_input):
res = qa(f'''{prompt_input}''')
return res['answer']
# User-provided prompt
if prompt := st.chat_input("What is up?"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user", avatar="user.png"):
st.write(prompt)
# Generate a new response if last message is not from assistant
if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant", avatar="logo.png"):
with st.spinner("Thinking..."):
response = generate_llm_response(st.session_state.messages[-1]["content"])
placeholder = st.empty()
full_response = ''
for item in response:
full_response += item
placeholder.markdown(full_response)
time.sleep(0.05)
placeholder.markdown(full_response)
message = {"role": "assistant", "content": full_response}
st.session_state.messages.append(message)
# Example prompt
with st.sidebar :
st.title('Examples :')
def promptExample1():
prompt = "how can I start my company in morocco?"
st.session_state.messages.append({"role": "user", "content": prompt})
# Example prompt
def promptExample2():
prompt = "What are some recommended cities for starting a business in finance"
st.session_state.messages.append({"role": "user", "content": prompt})
# Example prompt
def promptExample3():
prompt = "what is the estimate money I need for starting my company"
st.session_state.messages.append({"role": "user", "content": prompt})
st.sidebar.button('how can I start my company in morocco?', on_click=promptExample1)
st.sidebar.button('What are some recommended cities for starting a business in finance', on_click=promptExample2)
st.sidebar.button('what is the estimate money I need for starting my company', on_click=promptExample3)
|