Update app.py
Browse files
app.py
CHANGED
|
@@ -14,11 +14,27 @@ import time
|
|
| 14 |
huggingfacehub_api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 15 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Set environment variables for Hugging Face
|
| 18 |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = huggingfacehub_api_token
|
| 19 |
|
| 20 |
# Initialize the ChatGroq LLM with the retrieved API key
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
st.title("DataScience Chatgroq With Llama3")
|
| 24 |
|
|
@@ -41,23 +57,31 @@ def vector_embedding():
|
|
| 41 |
st.session_state.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) # Chunk Creation
|
| 42 |
st.session_state.final_documents = st.session_state.text_splitter.split_documents(st.session_state.docs[:20]) # Splitting
|
| 43 |
st.session_state.vectors = FAISS.from_documents(st.session_state.final_documents, st.session_state.embeddings) # Vector HuggingFace embeddings
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
prompt1 = st.text_input("Enter Your Question From Documents")
|
| 46 |
|
| 47 |
if st.button("Documents Embedding"):
|
| 48 |
vector_embedding()
|
| 49 |
-
st.write("Vector Store DB Is Ready")
|
| 50 |
|
| 51 |
if prompt1:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
st.write(
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
huggingfacehub_api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 15 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 16 |
|
| 17 |
+
# Debugging: Print the API keys to ensure they are being retrieved (remove these prints in production)
|
| 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()
|
| 25 |
+
if not groq_api_key:
|
| 26 |
+
st.error("GROQ_API_KEY environment variable is not set")
|
| 27 |
+
st.stop()
|
| 28 |
+
|
| 29 |
# Set environment variables for Hugging Face
|
| 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 |
|
|
|
|
| 57 |
st.session_state.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) # Chunk Creation
|
| 58 |
st.session_state.final_documents = st.session_state.text_splitter.split_documents(st.session_state.docs[:20]) # Splitting
|
| 59 |
st.session_state.vectors = FAISS.from_documents(st.session_state.final_documents, st.session_state.embeddings) # Vector HuggingFace embeddings
|
| 60 |
+
st.write("Vector Store DB Is Ready")
|
| 61 |
+
else:
|
| 62 |
+
st.write("Vectors already initialized.")
|
| 63 |
|
| 64 |
prompt1 = st.text_input("Enter Your Question From Documents")
|
| 65 |
|
| 66 |
if st.button("Documents Embedding"):
|
| 67 |
vector_embedding()
|
|
|
|
| 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 |
+
start = time.process_time()
|
| 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"Failed to retrieve the answer: {e}")
|