Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,12 +14,11 @@ import faiss
|
|
14 |
import uuid
|
15 |
from dotenv import load_dotenv
|
16 |
|
17 |
-
# Load environment variables
|
18 |
load_dotenv()
|
19 |
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
20 |
RAG_ACCESS_KEY = os.getenv("RAG_ACCESS_KEY")
|
21 |
|
22 |
-
#
|
23 |
if "vectorstore" not in st.session_state:
|
24 |
st.session_state.vectorstore = None
|
25 |
if "history" not in st.session_state:
|
@@ -27,7 +26,7 @@ if "history" not in st.session_state:
|
|
27 |
if "authenticated" not in st.session_state:
|
28 |
st.session_state.authenticated = False
|
29 |
|
30 |
-
# Sidebar with
|
31 |
with st.sidebar:
|
32 |
try:
|
33 |
st.image("bsnl_logo.png", width=200)
|
@@ -37,7 +36,6 @@ with st.sidebar:
|
|
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 {
|
@@ -83,7 +81,7 @@ with st.sidebar:
|
|
83 |
st.write(f"**A{i+1}:** {a}")
|
84 |
st.markdown("---")
|
85 |
|
86 |
-
# Main
|
87 |
def main():
|
88 |
st.markdown("""
|
89 |
<style>
|
@@ -116,10 +114,10 @@ def main():
|
|
116 |
except Exception as e:
|
117 |
st.error(f"Error generating answer: {str(e)}")
|
118 |
|
119 |
-
# PDF
|
120 |
def process_input(input_data):
|
121 |
os.makedirs("vectorstore", exist_ok=True)
|
122 |
-
os.chmod("vectorstore",
|
123 |
|
124 |
progress_bar = st.progress(0)
|
125 |
status = st.status("Processing PDF file...", expanded=True)
|
@@ -163,20 +161,20 @@ def process_input(input_data):
|
|
163 |
progress_bar.progress(1.0)
|
164 |
return vector_store
|
165 |
|
166 |
-
#
|
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, "
|
172 |
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN
|
173 |
)
|
174 |
except Exception as e:
|
175 |
-
raise RuntimeError("Failed to load LLM.
|
176 |
|
177 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
178 |
prompt_template = PromptTemplate(
|
179 |
-
template="Use the context to answer the question
|
180 |
input_variables=["context", "question"]
|
181 |
)
|
182 |
|
@@ -191,6 +189,5 @@ def answer_question(vectorstore, query):
|
|
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()
|
|
|
14 |
import uuid
|
15 |
from dotenv import load_dotenv
|
16 |
|
|
|
17 |
load_dotenv()
|
18 |
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
19 |
RAG_ACCESS_KEY = os.getenv("RAG_ACCESS_KEY")
|
20 |
|
21 |
+
# Session state init
|
22 |
if "vectorstore" not in st.session_state:
|
23 |
st.session_state.vectorstore = None
|
24 |
if "history" not in st.session_state:
|
|
|
26 |
if "authenticated" not in st.session_state:
|
27 |
st.session_state.authenticated = False
|
28 |
|
29 |
+
# Sidebar with login
|
30 |
with st.sidebar:
|
31 |
try:
|
32 |
st.image("bsnl_logo.png", width=200)
|
|
|
36 |
st.header("RAG Control Panel")
|
37 |
api_key_input = st.text_input("Enter RAG Access Key", type="password")
|
38 |
|
|
|
39 |
st.markdown("""
|
40 |
<style>
|
41 |
.auth-button button {
|
|
|
81 |
st.write(f"**A{i+1}:** {a}")
|
82 |
st.markdown("---")
|
83 |
|
84 |
+
# Main UI
|
85 |
def main():
|
86 |
st.markdown("""
|
87 |
<style>
|
|
|
114 |
except Exception as e:
|
115 |
st.error(f"Error generating answer: {str(e)}")
|
116 |
|
117 |
+
# Processing PDF
|
118 |
def process_input(input_data):
|
119 |
os.makedirs("vectorstore", exist_ok=True)
|
120 |
+
os.chmod("vectorstore", 0o700) # safer permissions
|
121 |
|
122 |
progress_bar = st.progress(0)
|
123 |
status = st.status("Processing PDF file...", expanded=True)
|
|
|
161 |
progress_bar.progress(1.0)
|
162 |
return vector_store
|
163 |
|
164 |
+
# Q&A logic
|
165 |
def answer_question(vectorstore, query):
|
166 |
try:
|
167 |
llm = HuggingFaceHub(
|
168 |
repo_id="mistralai/Mistral-7B-Instruct-v0.1",
|
169 |
+
model_kwargs={"temperature": 0.7, "max_new_tokens": 512},
|
170 |
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN
|
171 |
)
|
172 |
except Exception as e:
|
173 |
+
raise RuntimeError("Failed to load LLM. Ensure your Hugging Face API key is valid and the model is public or accessible.") from e
|
174 |
|
175 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
176 |
prompt_template = PromptTemplate(
|
177 |
+
template="Use the context below to answer the question.\n\nContext: {context}\n\nQuestion: {question}\n\nAnswer:",
|
178 |
input_variables=["context", "question"]
|
179 |
)
|
180 |
|
|
|
189 |
result = qa_chain({"query": query})
|
190 |
return result["result"].split("Answer:")[-1].strip()
|
191 |
|
|
|
192 |
if __name__ == "__main__":
|
193 |
main()
|