masadonline commited on
Commit
33aebae
·
verified ·
1 Parent(s): 40f6e88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -53,6 +53,10 @@ def generate_answer_with_groq(question, context):
53
  """Generates an answer using the Groq API."""
54
  url = "https://api.groq.com/openai/v1/chat/completions"
55
  api_key = os.environ.get("GROQ_API_KEY")
 
 
 
 
56
  headers = {
57
  "Authorization": f"Bearer {api_key}",
58
  "Content-Type": "application/json",
@@ -92,7 +96,7 @@ def perform_rag_groq(vectorstore, query):
92
  relevant_docs = retriever.get_relevant_documents(query)
93
  context = "\n\n".join([doc.page_content for doc in relevant_docs])
94
  answer = generate_answer_with_groq(query, context)
95
- return {"answer": answer, "sources": [doc.metadata['source'] for doc in relevant_docs]} # You might need to adjust how sources are stored
96
 
97
  # --- 3. Streamlit UI ---
98
 
@@ -100,12 +104,6 @@ def main():
100
  st.title("PDF Q&A with Local Docs (Powered by Groq)")
101
  st.info("Make sure you have a 'docs' folder in the same directory as this script containing your PDF files.")
102
 
103
- groq_api_key = st.text_input("Enter your Groq API Key:", type="password")
104
- if not groq_api_key:
105
- st.warning("Please enter your Groq API key to ask questions.")
106
- return
107
- os.environ["GROQ_API_KEY"] = groq_api_key
108
-
109
  with st.spinner("Loading and processing PDF(s)..."):
110
  all_text, all_tables = load_and_process_pdfs_from_folder()
111
 
@@ -121,11 +119,14 @@ def main():
121
  if query:
122
  with st.spinner("Searching for answer..."):
123
  result = perform_rag_groq(vectorstore, query)
124
- st.subheader("Answer:")
125
- st.write(result["answer"])
126
- if "sources" in result:
127
- st.subheader("Source:")
128
- st.write(", ".join(result["sources"])) # Display sources
 
 
 
129
 
130
  if all_tables:
131
  st.subheader("Extracted Tables:")
 
53
  """Generates an answer using the Groq API."""
54
  url = "https://api.groq.com/openai/v1/chat/completions"
55
  api_key = os.environ.get("GROQ_API_KEY")
56
+ if not api_key:
57
+ st.error("GROQ_API_KEY environment variable not found. Please set it.")
58
+ return None # Indicate failure
59
+
60
  headers = {
61
  "Authorization": f"Bearer {api_key}",
62
  "Content-Type": "application/json",
 
96
  relevant_docs = retriever.get_relevant_documents(query)
97
  context = "\n\n".join([doc.page_content for doc in relevant_docs])
98
  answer = generate_answer_with_groq(query, context)
99
+ return {"answer": answer, "sources": [doc.metadata['source'] for doc in relevant_docs] if relevant_docs else []}
100
 
101
  # --- 3. Streamlit UI ---
102
 
 
104
  st.title("PDF Q&A with Local Docs (Powered by Groq)")
105
  st.info("Make sure you have a 'docs' folder in the same directory as this script containing your PDF files.")
106
 
 
 
 
 
 
 
107
  with st.spinner("Loading and processing PDF(s)..."):
108
  all_text, all_tables = load_and_process_pdfs_from_folder()
109
 
 
119
  if query:
120
  with st.spinner("Searching for answer..."):
121
  result = perform_rag_groq(vectorstore, query)
122
+ if result and result.get("answer"):
123
+ st.subheader("Answer:")
124
+ st.write(result["answer"])
125
+ if "sources" in result and result["sources"]:
126
+ st.subheader("Source:")
127
+ st.write(", ".join(result["sources"]))
128
+ else:
129
+ st.warning("Could not generate an answer.")
130
 
131
  if all_tables:
132
  st.subheader("Extracted Tables:")