Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.document_loaders import PyPDFLoader
|
2 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
3 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
4 |
+
from langchain.vectorstores import Pinecone
|
5 |
+
from sentence_transformers import SentenceTransformer
|
6 |
+
from langchain.chains.question_answering import load_qa_chain
|
7 |
+
import pinecone
|
8 |
+
import os
|
9 |
+
from langchain.llms import LlamaCpp
|
10 |
+
from langchain.callbacks.manager import CallbackManager
|
11 |
+
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
12 |
+
from huggingface_hub import hf_hub_download
|
13 |
+
import streamlit as st
|
14 |
+
from streamlit_chat import message
|
15 |
+
from dotenv import load_dotenv
|
16 |
+
|
17 |
+
load_dotenv()
|
18 |
+
# from langchain.chains.question_answering import load_qa_chain
|
19 |
+
# from langchain import Document
|
20 |
+
# from langchain.document_loaders import DirectoryLoader
|
21 |
+
|
22 |
+
# loader = OnlinePDFLoader("food.pdf")
|
23 |
+
loader = PyPDFLoader("Ramesh_kumar_Resume.pdf")
|
24 |
+
data = loader.load()
|
25 |
+
# data
|
26 |
+
text_splitter=RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=0)
|
27 |
+
docs=text_splitter.split_documents(data)
|
28 |
+
len(docs)
|
29 |
+
|
30 |
+
|
31 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = ""
|
32 |
+
PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY', '')
|
33 |
+
PINECONE_API_ENV = os.environ.get('PINECONE_API_ENV', 'gcp-starter')
|
34 |
+
os.environ['PINECONE_API_KEY'] = PINECONE_API_KEY
|
35 |
+
|
36 |
+
embeddings=HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
37 |
+
|
38 |
+
# initialize pinecone
|
39 |
+
pinecone.Pinecone(
|
40 |
+
api_key=PINECONE_API_KEY, # find at app.pinecone.io
|
41 |
+
environment=PINECONE_API_ENV # next to api key in console
|
42 |
+
)
|
43 |
+
index_name = "testindex" # put in the name of your pinecone index here
|
44 |
+
|
45 |
+
|
46 |
+
docsearch= Pinecone.from_texts([t.page_content for t in docs],embeddings, index_name=index_name)
|
47 |
+
|
48 |
+
query="what languages ramesh know?"
|
49 |
+
|
50 |
+
docs=docsearch.similarity_search(query,k=1)
|
51 |
+
|
52 |
+
# docs
|
53 |
+
|
54 |
+
# !CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python --force-reinstall --upgrade --no-cache-dir --verbose
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
# Callbacks support token-wise streaming
|
59 |
+
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
|
60 |
+
# Verbose is required to pass to the callback manager
|
61 |
+
|
62 |
+
model_name_or_path = "TheBloke/Llama-2-7B-Chat-GGUF"
|
63 |
+
filename = "llama-2-7b-chat.Q4_K_M.gguf" # the model is in bin format
|
64 |
+
model_path = hf_hub_download(repo_id=model_name_or_path, filename=filename)
|
65 |
+
|
66 |
+
n_gpu_layers = 40 # Change this value based on your model and your GPU VRAM pool.
|
67 |
+
n_batch = 256 # Should be between 1 and n_ctx, consider the amount of VRAM in your GPU.
|
68 |
+
|
69 |
+
# Loading model,
|
70 |
+
|
71 |
+
|
72 |
+
query=st.text_input("Ask questions:")
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
if query:
|
79 |
+
search=docsearch.similarity_search(query)
|
80 |
+
|
81 |
+
llm = LlamaCpp(
|
82 |
+
model_path=model_path,
|
83 |
+
max_tokens=256,
|
84 |
+
# n_gpu_layers=n_gpu_layers,
|
85 |
+
# n_batch=n_batch,
|
86 |
+
callback_manager=callback_manager,
|
87 |
+
n_ctx=1024,
|
88 |
+
verbose=True,
|
89 |
+
)
|
90 |
+
|
91 |
+
chain=load_qa_chain(llm, chain_type="stuff")
|
92 |
+
with get_openai_callback() as cb:
|
93 |
+
response = chain.run(input_documents=search, question=query)
|
94 |
+
print(cb)
|
95 |
+
# st.write(response)
|
96 |
+
|
97 |
+
st.session_state['messages'].append({"role": "user", "content": query})
|
98 |
+
st.session_state['messages'].append({"role": "assistant", "content": response})
|
99 |
+
|
100 |
+
response_container = st.container()
|
101 |
+
# container for text box
|
102 |
+
container = st.container()
|
103 |
+
|
104 |
+
with container:
|
105 |
+
if query:
|
106 |
+
output = response
|
107 |
+
st.session_state['past'].append(query)
|
108 |
+
st.session_state['generated'].append(output)
|
109 |
+
|
110 |
+
if st.session_state['generated']:
|
111 |
+
with response_container:
|
112 |
+
for i in range(len(st.session_state['generated'])):
|
113 |
+
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user')
|
114 |
+
message(st.session_state["generated"][i], key=str(i))
|