ZunairaHawwar commited on
Commit
5622d5a
Β·
verified Β·
1 Parent(s): 61104b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -50,18 +50,18 @@ def ingest_docs_to_chroma():
50
  chunks = text_splitter.split_documents(all_docs)
51
  st.write(f"Total chunks created: {len(chunks)}")
52
 
 
 
 
53
  # Add Chunks to ChromaDB
54
  for chunk in chunks:
55
- # Flatten list content if necessary
56
  if isinstance(chunk.page_content, list):
57
  content = " ".join(str(item) for item in chunk.page_content).strip()
58
  else:
59
  content = str(chunk.page_content).strip()
60
 
61
- metadata = chunk.metadata
62
  doc_id = str(hash(content))
63
- collection.add(documents=[content], metadatas=[metadata], ids=[doc_id])
64
-
65
 
66
  st.success("βœ… Knowledge Base Updated Successfully!")
67
 
@@ -81,11 +81,15 @@ def ask_groq(context, question):
81
  {"role": "system", "content": "You are a helpful assistant. Always provide relevant video and website links if possible."},
82
  {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}\nAnswer (include links):"}
83
  ]
84
- response = groq_client.chat.completions.create(
85
- model=GROQ_MODEL,
86
- messages=messages
87
- )
88
- return response.choices[0].message.content.strip()
 
 
 
 
89
 
90
  # --- Streamlit UI ---
91
  def main():
@@ -93,10 +97,8 @@ def main():
93
  st.title("πŸŽ“ EduBot for @icodeguru0")
94
  st.markdown("Ask anything based on pre-loaded iCodeGuru knowledge.")
95
 
96
- # --- Auto Update Knowledge Base at App Start ---
97
- st.info("πŸ”„ Updating Knowledge Base from /docs/...")
98
- ingest_docs_to_chroma()
99
- st.success("βœ… Knowledge Base Loaded Successfully!")
100
 
101
  st.markdown("---")
102
 
@@ -105,7 +107,7 @@ def main():
105
  if user_question:
106
  vector_context = search_vector_data(user_question)
107
  if vector_context:
108
- with st.spinner("🧠 Answering from knowledge base..."):
109
  answer = ask_groq(vector_context, user_question)
110
  st.success(answer)
111
  else:
@@ -115,4 +117,4 @@ def main():
115
  st.caption("Powered by ChromaDB 🧠 and Groq ⚑")
116
 
117
  if __name__ == "__main__":
118
- main()
 
50
  chunks = text_splitter.split_documents(all_docs)
51
  st.write(f"Total chunks created: {len(chunks)}")
52
 
53
+ # Clear existing vectors to avoid duplication
54
+ collection.delete(where={})
55
+
56
  # Add Chunks to ChromaDB
57
  for chunk in chunks:
 
58
  if isinstance(chunk.page_content, list):
59
  content = " ".join(str(item) for item in chunk.page_content).strip()
60
  else:
61
  content = str(chunk.page_content).strip()
62
 
 
63
  doc_id = str(hash(content))
64
+ collection.add(documents=[content], ids=[doc_id])
 
65
 
66
  st.success("βœ… Knowledge Base Updated Successfully!")
67
 
 
81
  {"role": "system", "content": "You are a helpful assistant. Always provide relevant video and website links if possible."},
82
  {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}\nAnswer (include links):"}
83
  ]
84
+ try:
85
+ response = groq_client.chat.completions.create(
86
+ model=GROQ_MODEL,
87
+ messages=messages
88
+ )
89
+ return response.choices[0].message.content.strip()
90
+ except Exception as e:
91
+ st.error(f"Groq API error: {e}")
92
+ return "⚠️ Failed to get response from Groq API."
93
 
94
  # --- Streamlit UI ---
95
  def main():
 
97
  st.title("πŸŽ“ EduBot for @icodeguru0")
98
  st.markdown("Ask anything based on pre-loaded iCodeGuru knowledge.")
99
 
100
+ if st.button("πŸ”„ Refresh Knowledge Base"):
101
+ ingest_docs_to_chroma()
 
 
102
 
103
  st.markdown("---")
104
 
 
107
  if user_question:
108
  vector_context = search_vector_data(user_question)
109
  if vector_context:
110
+ with st.spinner("🧐 Answering from knowledge base..."):
111
  answer = ask_groq(vector_context, user_question)
112
  st.success(answer)
113
  else:
 
117
  st.caption("Powered by ChromaDB 🧠 and Groq ⚑")
118
 
119
  if __name__ == "__main__":
120
+ main()