Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from llama_index import ServiceContext, StorageContext, set_global_service_context, VectorStoreIndex, Document
|
| 4 |
+
from llama_index.prompts import PromptTemplate
|
| 5 |
+
from llama_index.embeddings import LangchainEmbedding
|
| 6 |
+
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
| 7 |
+
from llama_index.chat_engine.condense_question import CondenseQuestionChatEngine
|
| 8 |
+
from llama_index.llms import LlamaCPP
|
| 9 |
+
from llama_index.llms.llama_utils import messages_to_prompt, completion_to_prompt
|
| 10 |
+
from PyPDF2 import PdfReader
|
| 11 |
+
|
| 12 |
+
def modelspecific_prompt(promptmessage):
|
| 13 |
+
return f"Instruct: {promptmessage}\nOutput:"
|
| 14 |
+
|
| 15 |
+
def extract_text_from_pdf(pdf):
|
| 16 |
+
pdf_reader = PdfReader(pdf)
|
| 17 |
+
data = ''.join(page.extract_text() for page in pdf_reader.pages)
|
| 18 |
+
return data.split('\n')
|
| 19 |
+
|
| 20 |
+
def main():
|
| 21 |
+
llm = LlamaCPP(
|
| 22 |
+
model_url=None,
|
| 23 |
+
model_path='models/phi-2.Q4_K_M.gguf',
|
| 24 |
+
temperature=0.1,
|
| 25 |
+
max_new_tokens=512,
|
| 26 |
+
context_window=2048,
|
| 27 |
+
generate_kwargs={},
|
| 28 |
+
messages_to_prompt=messages_to_prompt,
|
| 29 |
+
completion_to_prompt=completion_to_prompt,
|
| 30 |
+
verbose=True
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
embed_model = LangchainEmbedding(
|
| 34 |
+
HuggingFaceEmbeddings(model_name="BAAI/bge-small-en-v1.5")
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
service_context = ServiceContext.from_defaults(
|
| 38 |
+
chunk_size=128,
|
| 39 |
+
chunk_overlap=20,
|
| 40 |
+
context_window=2048,
|
| 41 |
+
num_output=768,
|
| 42 |
+
llm=llm,
|
| 43 |
+
embed_model=embed_model
|
| 44 |
+
)
|
| 45 |
+
set_global_service_context(service_context)
|
| 46 |
+
|
| 47 |
+
storage_context = StorageContext.from_defaults()
|
| 48 |
+
st.title("Llama-CPP Local LLM with RAG (Phi-2 RAG)")
|
| 49 |
+
|
| 50 |
+
pdf = st.file_uploader("Upload a PDF file", type=["pdf"])
|
| 51 |
+
|
| 52 |
+
if pdf is not None:
|
| 53 |
+
text_list = extract_text_from_pdf(pdf)
|
| 54 |
+
documents = [Document(text=t) for t in text_list]
|
| 55 |
+
nodes = (service_context.node_parser.get_nodes_from_documents(documents))
|
| 56 |
+
storage_context.docstore.add_documents(nodes)
|
| 57 |
+
index = (VectorStoreIndex.from_documents(
|
| 58 |
+
documents, service_context=service_context, storage_context=storage_context, llm=llm))
|
| 59 |
+
custom_prompt = PromptTemplate("Given the following context, answer the question:")
|
| 60 |
+
query_engine = index.as_query_engine()
|
| 61 |
+
chat_engine = CondenseQuestionChatEngine.from_defaults(
|
| 62 |
+
query_engine=query_engine,
|
| 63 |
+
condense_question_prompt=custom_prompt,
|
| 64 |
+
verbose=True,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
if "messages" not in st.session_state:
|
| 68 |
+
st.session_state.messages = []
|
| 69 |
+
|
| 70 |
+
for message in st.session_state.messages:
|
| 71 |
+
with st.chat_message(message["role"]):
|
| 72 |
+
st.markdown(message["content"])
|
| 73 |
+
|
| 74 |
+
if prompt := st.chat_input("What is up?"):
|
| 75 |
+
st.session_state.messages.append(
|
| 76 |
+
{"role": "user", "content": prompt})
|
| 77 |
+
|
| 78 |
+
with st.chat_message("user"):
|
| 79 |
+
st.markdown(prompt)
|
| 80 |
+
|
| 81 |
+
with st.chat_message("assistant"):
|
| 82 |
+
message_placeholder = st.empty()
|
| 83 |
+
full_response = ""
|
| 84 |
+
assistant_response = chat_engine.chat(
|
| 85 |
+
modelspecific_prompt(str(prompt)))
|
| 86 |
+
assistant_response = str(assistant_response)
|
| 87 |
+
for chunk in assistant_response.split():
|
| 88 |
+
full_response += chunk + " "
|
| 89 |
+
time.sleep(0.05)
|
| 90 |
+
message_placeholder.markdown(full_response + "▌")
|
| 91 |
+
message_placeholder.markdown(full_response)
|
| 92 |
+
|
| 93 |
+
st.session_state.messages.append(
|
| 94 |
+
{"role": "assistant", "content": full_response})
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
if __name__ == "__main__":
|
| 98 |
+
main()
|