Spaces:
Sleeping
Sleeping
streamlit
Browse files- app.py +128 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from llama_index.llms.ollama import Ollama
|
3 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
4 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings, set_global_tokenizer
|
5 |
+
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
6 |
+
from transformers import AutoTokenizer
|
7 |
+
from datetime import datetime
|
8 |
+
from llama_index.core.memory import ChatMemoryBuffer
|
9 |
+
import time
|
10 |
+
|
11 |
+
# Define the data directory for loading documents
|
12 |
+
DATA_DIR = "docs"
|
13 |
+
|
14 |
+
# Mengecek apakah 'is_initialized' sudah ada di session state
|
15 |
+
if 'is_initialized' not in st.session_state:
|
16 |
+
st.session_state.is_initialized = False
|
17 |
+
|
18 |
+
# Inisialisasi yang hanya dilakukan sekali saat pertama kali load
|
19 |
+
if not st.session_state.is_initialized:
|
20 |
+
st.session_state.is_initialized = True
|
21 |
+
|
22 |
+
|
23 |
+
Settings.llm = HuggingFaceInferenceAPI(
|
24 |
+
model_name="HuggingFaceH4/zephyr-7b-beta",
|
25 |
+
tokenizer_name="HuggingFaceH4/zephyr-7b-beta",
|
26 |
+
context_window=3000,
|
27 |
+
max_new_tokens=512,
|
28 |
+
generate_kwargs={"temperature": 0.1},
|
29 |
+
# stream=True
|
30 |
+
)
|
31 |
+
|
32 |
+
Settings.embed_model = HuggingFaceEmbedding(
|
33 |
+
model_name="BAAI/bge-small-en-v1.5"
|
34 |
+
)
|
35 |
+
|
36 |
+
# Set the global tokenizer to use the tokenizer from HuggingFace for encoding inputs
|
37 |
+
set_global_tokenizer(
|
38 |
+
AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-chat-hf").encode
|
39 |
+
)
|
40 |
+
|
41 |
+
print("# load data", datetime.now())
|
42 |
+
# Load documents from the data directory into the Vector Store Index
|
43 |
+
documents = SimpleDirectoryReader(DATA_DIR).load_data()
|
44 |
+
|
45 |
+
# Create Vector Store Index with HuggingFace Embedding
|
46 |
+
index = VectorStoreIndex.from_documents(documents)
|
47 |
+
|
48 |
+
# Create Prompt Template for Text-based Q&A
|
49 |
+
chat_text_qa_msgs = [
|
50 |
+
(
|
51 |
+
"user",
|
52 |
+
"""You are a Q&A assistant. For all other inquiries, your main goal is to provide answers as accurately as possible, based on the instructions and context you have been given. If a question does not match the provided context or is outside the scope of the document, kindly advise the user to ask questions within the context of the document.
|
53 |
+
Context:
|
54 |
+
{context_str}
|
55 |
+
Question:
|
56 |
+
{query_str}
|
57 |
+
"""
|
58 |
+
)
|
59 |
+
]
|
60 |
+
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
61 |
+
|
62 |
+
# Initialize Chat Memory Buffer for Conversation Memory
|
63 |
+
memory = ChatMemoryBuffer.from_defaults(token_limit=3900)
|
64 |
+
|
65 |
+
# Create Query Engine with LLM and Template
|
66 |
+
query_engine = index.as_query_engine(
|
67 |
+
text_qa_template=text_qa_template,
|
68 |
+
# streaming=True,
|
69 |
+
memory=memory
|
70 |
+
)
|
71 |
+
|
72 |
+
if 'query_engine' not in st.session_state:
|
73 |
+
st.session_state.query_engine = query_engine
|
74 |
+
|
75 |
+
|
76 |
+
print("# loaded", datetime.now())
|
77 |
+
|
78 |
+
# Function to handle queries
|
79 |
+
def handle_query(query):
|
80 |
+
return st.session_state.query_engine.query(query)
|
81 |
+
# streaming_response = st.session_state.query_engine.query(query)
|
82 |
+
# for text in streaming_response.response_gen:
|
83 |
+
# yield text
|
84 |
+
|
85 |
+
print("-- check", datetime.now())
|
86 |
+
|
87 |
+
# ============== Streamlit App ===============
|
88 |
+
st.title("POC LLM RAG ✅")
|
89 |
+
st.markdown("Retrieval-Augmented Generation (RAG) with Large Language Model (LLM) using llama-index library and Ollama.")
|
90 |
+
st.markdown("start chat ...🚀")
|
91 |
+
|
92 |
+
if 'messages' not in st.session_state:
|
93 |
+
st.session_state.messages = [{'role': 'assistant', "content": 'Hello! Ask me anything about the documents.'}]
|
94 |
+
|
95 |
+
# Sidebar to list documents
|
96 |
+
with st.sidebar:
|
97 |
+
st.title("Documents:")
|
98 |
+
docs = SimpleDirectoryReader(DATA_DIR).list_resources()
|
99 |
+
for d in docs:
|
100 |
+
file_name = str(d).split('/')[-1]
|
101 |
+
st.info(file_name)
|
102 |
+
|
103 |
+
|
104 |
+
# for message in st.session_state.messages:
|
105 |
+
# with st.chat_message(message["role"]):
|
106 |
+
# st.markdown(message["content"])
|
107 |
+
|
108 |
+
|
109 |
+
# if prompt := st.chat_input("Ask me anything about the documents"):
|
110 |
+
# st.session_state.messages.append({"role": "user", "content": prompt})
|
111 |
+
# with st.chat_message("user"):
|
112 |
+
# st.markdown(prompt)
|
113 |
+
|
114 |
+
# with st.chat_message("assistant"):
|
115 |
+
# stream = handle_query(prompt)
|
116 |
+
# response = st.write_stream(stream)
|
117 |
+
# st.session_state.messages.append({"role": "assistant", "content": response})
|
118 |
+
|
119 |
+
user_prompt = st.chat_input("Ask me anything about the content of the PDF:")
|
120 |
+
if user_prompt:
|
121 |
+
st.session_state.messages.append({'role': 'user', "content": user_prompt})
|
122 |
+
response = handle_query(user_prompt)
|
123 |
+
st.session_state.messages.append({'role': 'assistant', "content": response})
|
124 |
+
|
125 |
+
for message in st.session_state.messages:
|
126 |
+
with st.chat_message(message['role']):
|
127 |
+
st.write(message['content'])
|
128 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
llama-index
|
3 |
+
llama-index-llms-ollama
|
4 |
+
llama-index-embeddings-huggingface
|
5 |
+
llama-index-llms-huggingface-api
|