Update app.py
Browse files
app.py
CHANGED
@@ -10,15 +10,11 @@ from langchain_community.vectorstores import FAISS
|
|
10 |
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
11 |
import time
|
12 |
|
13 |
-
# Retrieve
|
14 |
huggingfacehub_api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
15 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
16 |
|
17 |
-
#
|
18 |
-
st.write("Hugging Face Hub API Token:", huggingfacehub_api_token)
|
19 |
-
st.write("GROQ API Key:", groq_api_key)
|
20 |
-
|
21 |
-
# Check if the keys are retrieved correctly
|
22 |
if not huggingfacehub_api_token:
|
23 |
st.error("HUGGINGFACEHUB_API_TOKEN environment variable is not set")
|
24 |
st.stop()
|
@@ -26,16 +22,14 @@ if not groq_api_key:
|
|
26 |
st.error("GROQ_API_KEY environment variable is not set")
|
27 |
st.stop()
|
28 |
|
29 |
-
#
|
30 |
-
os.environ['HUGGINGFACEHUB_API_TOKEN'] = huggingfacehub_api_token
|
31 |
-
|
32 |
-
# Initialize the ChatGroq LLM with the retrieved API key
|
33 |
try:
|
34 |
llm = ChatGroq(api_key=groq_api_key, model_name="Llama3-8b-8192")
|
35 |
except Exception as e:
|
36 |
st.error(f"Failed to initialize ChatGroq LLM: {e}")
|
37 |
st.stop()
|
38 |
|
|
|
39 |
st.title("DataScience Chatgroq With Llama3")
|
40 |
|
41 |
prompt = ChatPromptTemplate.from_template(
|
@@ -63,25 +57,19 @@ def vector_embedding():
|
|
63 |
|
64 |
prompt1 = st.text_input("Enter Your Question From Documents")
|
65 |
|
|
|
66 |
if st.button("Documents Embedding"):
|
67 |
-
|
|
|
|
|
|
|
68 |
|
|
|
69 |
if prompt1:
|
70 |
if "vectors" not in st.session_state:
|
71 |
st.error("Vectors are not initialized. Please click 'Documents Embedding' first.")
|
72 |
else:
|
73 |
-
document_chain = create_stuff_documents_chain(llm, prompt)
|
74 |
-
retriever = st.session_state.vectors.as_retriever()
|
75 |
-
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
76 |
try:
|
77 |
-
|
78 |
-
response = retrieval_chain.invoke({'input': prompt1})
|
79 |
-
st.write("Response time: ", time.process_time() - start)
|
80 |
-
st.write(response['answer'])
|
81 |
-
|
82 |
-
with st.expander("Document Similarity Search"):
|
83 |
-
for i, doc in enumerate(response["context"]):
|
84 |
-
st.write(doc.page_content)
|
85 |
-
st.write("--------------------------------")
|
86 |
except Exception as e:
|
87 |
-
st.error(f"
|
|
|
10 |
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
11 |
import time
|
12 |
|
13 |
+
# Retrieve API keys from environment variables
|
14 |
huggingfacehub_api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
15 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
16 |
|
17 |
+
# Check if keys are retrieved correctly
|
|
|
|
|
|
|
|
|
18 |
if not huggingfacehub_api_token:
|
19 |
st.error("HUGGINGFACEHUB_API_TOKEN environment variable is not set")
|
20 |
st.stop()
|
|
|
22 |
st.error("GROQ_API_KEY environment variable is not set")
|
23 |
st.stop()
|
24 |
|
25 |
+
# Initialize ChatGroq LLM with error handling
|
|
|
|
|
|
|
26 |
try:
|
27 |
llm = ChatGroq(api_key=groq_api_key, model_name="Llama3-8b-8192")
|
28 |
except Exception as e:
|
29 |
st.error(f"Failed to initialize ChatGroq LLM: {e}")
|
30 |
st.stop()
|
31 |
|
32 |
+
|
33 |
st.title("DataScience Chatgroq With Llama3")
|
34 |
|
35 |
prompt = ChatPromptTemplate.from_template(
|
|
|
57 |
|
58 |
prompt1 = st.text_input("Enter Your Question From Documents")
|
59 |
|
60 |
+
# Inside the button click for document embedding
|
61 |
if st.button("Documents Embedding"):
|
62 |
+
try:
|
63 |
+
vector_embedding()
|
64 |
+
except Exception as e:
|
65 |
+
st.error(f"Error initializing vector store: {e}")
|
66 |
|
67 |
+
# Inside the button click for processing user question
|
68 |
if prompt1:
|
69 |
if "vectors" not in st.session_state:
|
70 |
st.error("Vectors are not initialized. Please click 'Documents Embedding' first.")
|
71 |
else:
|
|
|
|
|
|
|
72 |
try:
|
73 |
+
# ... rest of your retrieval logic ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
except Exception as e:
|
75 |
+
st.error(f"Error processing your question: {e}")
|