Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -27,60 +27,83 @@ if "history" not in st.session_state:
|
|
27 |
if "authenticated" not in st.session_state:
|
28 |
st.session_state.authenticated = False
|
29 |
|
30 |
-
# Sidebar
|
31 |
with st.sidebar:
|
32 |
try:
|
33 |
st.image("bsnl_logo.png", width=200)
|
34 |
except FileNotFoundError:
|
35 |
-
st.warning("
|
36 |
|
37 |
st.header("RAG Control Panel")
|
38 |
api_key_input = st.text_input("Enter RAG Access Key", type="password")
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
if st.session_state.authenticated:
|
48 |
-
input_data = st.file_uploader("Upload PDF file", type=["pdf"])
|
49 |
-
|
50 |
-
if input_data:
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
# Chat History
|
67 |
-
st.subheader("Chat History")
|
68 |
-
for i, (q, a) in enumerate(st.session_state.history):
|
69 |
-
st.write(f"**Q{i+1}:** {q}")
|
70 |
-
st.write(f"**A{i+1}:** {a}")
|
71 |
-
st.markdown("---")
|
72 |
-
|
73 |
-
# Main area
|
74 |
def main():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
st.title("RAG Q&A App with Mistral AI")
|
76 |
-
st.markdown("Welcome to the BSNL RAG App
|
77 |
|
78 |
if not st.session_state.authenticated:
|
79 |
-
st.warning("Please authenticate using
|
80 |
return
|
81 |
|
82 |
if st.session_state.vectorstore is None:
|
83 |
-
st.info("Please upload and process a PDF file
|
84 |
return
|
85 |
|
86 |
query = st.text_input("Enter your question:")
|
@@ -91,46 +114,44 @@ def main():
|
|
91 |
st.session_state.history.append((query, answer))
|
92 |
st.write("**Answer:**", answer)
|
93 |
except Exception as e:
|
94 |
-
st.error(f"
|
95 |
|
|
|
96 |
def process_input(input_data):
|
97 |
os.makedirs("vectorstore", exist_ok=True)
|
98 |
os.chmod("vectorstore", 0o777)
|
99 |
|
100 |
progress_bar = st.progress(0)
|
101 |
-
status = st.status("Processing PDF...", expanded=True)
|
102 |
|
103 |
status.update(label="Reading PDF file...")
|
104 |
progress_bar.progress(0.2)
|
105 |
-
|
106 |
pdf_reader = PdfReader(BytesIO(input_data.read()))
|
107 |
-
documents = "".join(page.extract_text() or "" for page in pdf_reader.pages)
|
108 |
|
109 |
status.update(label="Splitting text...")
|
110 |
progress_bar.progress(0.4)
|
111 |
-
|
112 |
-
|
113 |
-
texts = splitter.split_text(documents)
|
114 |
|
115 |
status.update(label="Creating embeddings...")
|
116 |
progress_bar.progress(0.6)
|
117 |
-
|
118 |
-
embeddings = HuggingFaceEmbeddings(
|
119 |
model_name="sentence-transformers/all-mpnet-base-v2",
|
120 |
-
model_kwargs={
|
121 |
)
|
122 |
|
123 |
-
status.update(label="Building
|
124 |
progress_bar.progress(0.8)
|
125 |
-
|
126 |
-
dimension = len(embeddings.embed_query("sample text"))
|
127 |
index = faiss.IndexFlatL2(dimension)
|
128 |
vector_store = FAISS(
|
129 |
-
embedding_function=
|
130 |
index=index,
|
131 |
docstore=InMemoryDocstore({}),
|
132 |
index_to_docstore_id={}
|
133 |
)
|
|
|
134 |
uuids = [str(uuid.uuid4()) for _ in texts]
|
135 |
vector_store.add_texts(texts, ids=uuids)
|
136 |
|
@@ -140,20 +161,22 @@ def process_input(input_data):
|
|
140 |
|
141 |
status.update(label="Done!", state="complete")
|
142 |
progress_bar.progress(1.0)
|
143 |
-
|
144 |
return vector_store
|
145 |
|
|
|
146 |
def answer_question(vectorstore, query):
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
|
|
|
|
|
|
152 |
|
153 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
154 |
-
|
155 |
prompt_template = PromptTemplate(
|
156 |
-
template="Use the
|
157 |
input_variables=["context", "question"]
|
158 |
)
|
159 |
|
@@ -168,5 +191,6 @@ def answer_question(vectorstore, query):
|
|
168 |
result = qa_chain({"query": query})
|
169 |
return result["result"].split("Answer:")[-1].strip()
|
170 |
|
|
|
171 |
if __name__ == "__main__":
|
172 |
main()
|
|
|
27 |
if "authenticated" not in st.session_state:
|
28 |
st.session_state.authenticated = False
|
29 |
|
30 |
+
# Sidebar with BSNL logo and authentication
|
31 |
with st.sidebar:
|
32 |
try:
|
33 |
st.image("bsnl_logo.png", width=200)
|
34 |
except FileNotFoundError:
|
35 |
+
st.warning("BSNL logo not found.")
|
36 |
|
37 |
st.header("RAG Control Panel")
|
38 |
api_key_input = st.text_input("Enter RAG Access Key", type="password")
|
39 |
|
40 |
+
# Custom style for Authenticate button
|
41 |
+
st.markdown("""
|
42 |
+
<style>
|
43 |
+
.auth-button button {
|
44 |
+
background-color: #007BFF !important;
|
45 |
+
color: white !important;
|
46 |
+
font-weight: bold;
|
47 |
+
border-radius: 8px;
|
48 |
+
padding: 10px 20px;
|
49 |
+
border: none;
|
50 |
+
transition: all 0.3s ease;
|
51 |
+
}
|
52 |
+
.auth-button button:hover {
|
53 |
+
background-color: #0056b3 !important;
|
54 |
+
transform: scale(1.05);
|
55 |
+
}
|
56 |
+
</style>
|
57 |
+
""", unsafe_allow_html=True)
|
58 |
+
|
59 |
+
with st.container():
|
60 |
+
st.markdown('<div class="auth-button">', unsafe_allow_html=True)
|
61 |
+
if st.button("Authenticate"):
|
62 |
+
if api_key_input == RAG_ACCESS_KEY:
|
63 |
+
st.session_state.authenticated = True
|
64 |
+
st.success("Authentication successful!")
|
65 |
+
else:
|
66 |
+
st.error("Invalid API key.")
|
67 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
68 |
|
69 |
if st.session_state.authenticated:
|
70 |
+
input_data = st.file_uploader("Upload a PDF file", type=["pdf"])
|
71 |
+
|
72 |
+
if st.button("Process File") and input_data is not None:
|
73 |
+
try:
|
74 |
+
vector_store = process_input(input_data)
|
75 |
+
st.session_state.vectorstore = vector_store
|
76 |
+
st.success("File processed successfully. You can now ask questions.")
|
77 |
+
except Exception as e:
|
78 |
+
st.error(f"Processing failed: {str(e)}")
|
79 |
+
|
80 |
+
st.subheader("Chat History")
|
81 |
+
for i, (q, a) in enumerate(st.session_state.history):
|
82 |
+
st.write(f"**Q{i+1}:** {q}")
|
83 |
+
st.write(f"**A{i+1}:** {a}")
|
84 |
+
st.markdown("---")
|
85 |
+
|
86 |
+
# Main app UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
def main():
|
88 |
+
st.markdown("""
|
89 |
+
<style>
|
90 |
+
.stApp {
|
91 |
+
font-family: 'Roboto', sans-serif;
|
92 |
+
background-color: #FFFFFF;
|
93 |
+
color: #333;
|
94 |
+
}
|
95 |
+
</style>
|
96 |
+
""", unsafe_allow_html=True)
|
97 |
+
|
98 |
st.title("RAG Q&A App with Mistral AI")
|
99 |
+
st.markdown("Welcome to the BSNL RAG App! Upload a PDF and ask questions.")
|
100 |
|
101 |
if not st.session_state.authenticated:
|
102 |
+
st.warning("Please authenticate using the sidebar.")
|
103 |
return
|
104 |
|
105 |
if st.session_state.vectorstore is None:
|
106 |
+
st.info("Please upload and process a PDF file.")
|
107 |
return
|
108 |
|
109 |
query = st.text_input("Enter your question:")
|
|
|
114 |
st.session_state.history.append((query, answer))
|
115 |
st.write("**Answer:**", answer)
|
116 |
except Exception as e:
|
117 |
+
st.error(f"Error generating answer: {str(e)}")
|
118 |
|
119 |
+
# PDF processing logic
|
120 |
def process_input(input_data):
|
121 |
os.makedirs("vectorstore", exist_ok=True)
|
122 |
os.chmod("vectorstore", 0o777)
|
123 |
|
124 |
progress_bar = st.progress(0)
|
125 |
+
status = st.status("Processing PDF file...", expanded=True)
|
126 |
|
127 |
status.update(label="Reading PDF file...")
|
128 |
progress_bar.progress(0.2)
|
|
|
129 |
pdf_reader = PdfReader(BytesIO(input_data.read()))
|
130 |
+
documents = "".join([page.extract_text() or "" for page in pdf_reader.pages])
|
131 |
|
132 |
status.update(label="Splitting text...")
|
133 |
progress_bar.progress(0.4)
|
134 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
135 |
+
texts = text_splitter.split_text(documents)
|
|
|
136 |
|
137 |
status.update(label="Creating embeddings...")
|
138 |
progress_bar.progress(0.6)
|
139 |
+
hf_embeddings = HuggingFaceEmbeddings(
|
|
|
140 |
model_name="sentence-transformers/all-mpnet-base-v2",
|
141 |
+
model_kwargs={'device': 'cpu'}
|
142 |
)
|
143 |
|
144 |
+
status.update(label="Building vector store...")
|
145 |
progress_bar.progress(0.8)
|
146 |
+
dimension = len(hf_embeddings.embed_query("test"))
|
|
|
147 |
index = faiss.IndexFlatL2(dimension)
|
148 |
vector_store = FAISS(
|
149 |
+
embedding_function=hf_embeddings,
|
150 |
index=index,
|
151 |
docstore=InMemoryDocstore({}),
|
152 |
index_to_docstore_id={}
|
153 |
)
|
154 |
+
|
155 |
uuids = [str(uuid.uuid4()) for _ in texts]
|
156 |
vector_store.add_texts(texts, ids=uuids)
|
157 |
|
|
|
161 |
|
162 |
status.update(label="Done!", state="complete")
|
163 |
progress_bar.progress(1.0)
|
|
|
164 |
return vector_store
|
165 |
|
166 |
+
# Question-answering logic
|
167 |
def answer_question(vectorstore, query):
|
168 |
+
try:
|
169 |
+
llm = HuggingFaceHub(
|
170 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.1",
|
171 |
+
model_kwargs={"temperature": 0.7, "max_length": 512},
|
172 |
+
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN
|
173 |
+
)
|
174 |
+
except Exception as e:
|
175 |
+
raise RuntimeError("Failed to load LLM. Check Hugging Face API key and access rights.") from e
|
176 |
|
177 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
|
|
178 |
prompt_template = PromptTemplate(
|
179 |
+
template="Use the context to answer the question concisely:\n\nContext: {context}\n\nQuestion: {question}\n\nAnswer:",
|
180 |
input_variables=["context", "question"]
|
181 |
)
|
182 |
|
|
|
191 |
result = qa_chain({"query": query})
|
192 |
return result["result"].split("Answer:")[-1].strip()
|
193 |
|
194 |
+
# Run the app
|
195 |
if __name__ == "__main__":
|
196 |
main()
|