Update app.py
Browse files
app.py
CHANGED
|
@@ -14,11 +14,23 @@ 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 |
|
|
@@ -52,12 +64,15 @@ if prompt1:
|
|
| 52 |
document_chain = create_stuff_documents_chain(llm, prompt)
|
| 53 |
retriever = st.session_state.vectors.as_retriever()
|
| 54 |
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
huggingfacehub_api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 15 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 16 |
|
| 17 |
+
# Check if the keys are retrieved correctly
|
| 18 |
+
if not huggingfacehub_api_token:
|
| 19 |
+
st.error("HUGGINGFACEHUB_API_TOKEN environment variable is not set")
|
| 20 |
+
st.stop()
|
| 21 |
+
if not groq_api_key:
|
| 22 |
+
st.error("GROQ_API_KEY environment variable is not set")
|
| 23 |
+
st.stop()
|
| 24 |
+
|
| 25 |
# Set environment variables for Hugging Face
|
| 26 |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = huggingfacehub_api_token
|
| 27 |
|
| 28 |
# Initialize the ChatGroq LLM with the retrieved API key
|
| 29 |
+
try:
|
| 30 |
+
llm = ChatGroq(api_key=groq_api_key, model_name="Llama3-8b-8192")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
st.error(f"Failed to initialize ChatGroq LLM: {e}")
|
| 33 |
+
st.stop()
|
| 34 |
|
| 35 |
st.title("DataScience Chatgroq With Llama3")
|
| 36 |
|
|
|
|
| 64 |
document_chain = create_stuff_documents_chain(llm, prompt)
|
| 65 |
retriever = st.session_state.vectors.as_retriever()
|
| 66 |
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
| 67 |
+
try:
|
| 68 |
+
start = time.process_time()
|
| 69 |
+
response = retrieval_chain.invoke({'input': prompt1})
|
| 70 |
+
st.write("Response time: ", time.process_time() - start)
|
| 71 |
+
st.write(response['answer'])
|
| 72 |
+
|
| 73 |
+
with st.expander("Document Similarity Search"):
|
| 74 |
+
for i, doc in enumerate(response["context"]):
|
| 75 |
+
st.write(doc.page_content)
|
| 76 |
+
st.write("--------------------------------")
|
| 77 |
+
except Exception as e:
|
| 78 |
+
st.error(f"Failed to retrieve the answer: {e}")
|