aniket47 commited on
Commit
1193037
ยท
1 Parent(s): 8e53afa

Feature: Allow users to enter their own Serper API key in UI

Browse files
Files changed (1) hide show
  1. app.py +53 -9
app.py CHANGED
@@ -37,7 +37,7 @@ class DocumentChatbot:
37
  Main chatbot application class
38
  """
39
 
40
- def __init__(self):
41
  self.doc_processor = DocumentProcessor()
42
  self.vector_store = VectorStore()
43
  self.query_router = QueryRouter()
@@ -47,10 +47,16 @@ class DocumentChatbot:
47
  self.hf_client, self.model_loaded = get_hf_client()
48
 
49
  # Initialize web searcher if API key is available
 
 
 
 
50
  try:
51
- self.web_searcher = WebSearcher()
 
52
  except ValueError as e:
53
- st.warning(f"Web search disabled: {str(e)}")
 
54
 
55
  # Load existing index if available
56
  self.vector_store.load_index()
@@ -265,7 +271,9 @@ def main():
265
 
266
  # Initialize session state
267
  if 'chatbot' not in st.session_state:
268
- st.session_state.chatbot = DocumentChatbot()
 
 
269
 
270
  if 'chat_history' not in st.session_state:
271
  st.session_state.chat_history = []
@@ -319,12 +327,48 @@ def main():
319
  st.warning("โš ๏ธ AI model loading...")
320
  st.info("Models are being downloaded. This may take a few minutes on first run.")
321
 
322
- st.subheader("Web Search")
323
- if st.session_state.chatbot.web_searcher:
324
- st.success("Web search enabled")
 
 
 
 
 
 
 
 
325
  else:
326
- st.error("Web search disabled")
327
- st.info("Add SERPER_API_KEY to .env file to enable web search")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
 
329
  # Main chat interface
330
  st.header("Chat Interface")
 
37
  Main chatbot application class
38
  """
39
 
40
+ def __init__(self, serper_api_key: str = None):
41
  self.doc_processor = DocumentProcessor()
42
  self.vector_store = VectorStore()
43
  self.query_router = QueryRouter()
 
47
  self.hf_client, self.model_loaded = get_hf_client()
48
 
49
  # Initialize web searcher if API key is available
50
+ self.init_web_search(serper_api_key)
51
+
52
+ def init_web_search(self, api_key: str = None):
53
+ """Initialize or reinitialize web search with provided API key"""
54
  try:
55
+ self.web_searcher = WebSearcher(api_key=api_key)
56
+ return True
57
  except ValueError as e:
58
+ self.web_searcher = None
59
+ return False
60
 
61
  # Load existing index if available
62
  self.vector_store.load_index()
 
271
 
272
  # Initialize session state
273
  if 'chatbot' not in st.session_state:
274
+ # Try to get API key from environment variable first
275
+ env_api_key = os.getenv("SERPER_API_KEY")
276
+ st.session_state.chatbot = DocumentChatbot(serper_api_key=env_api_key)
277
 
278
  if 'chat_history' not in st.session_state:
279
  st.session_state.chat_history = []
 
327
  st.warning("โš ๏ธ AI model loading...")
328
  st.info("Models are being downloaded. This may take a few minutes on first run.")
329
 
330
+ # Web Search Configuration
331
+ st.subheader("๐ŸŒ Web Search")
332
+
333
+ # Check if web search is already enabled
334
+ web_search_enabled = st.session_state.chatbot.web_searcher is not None
335
+
336
+ if web_search_enabled:
337
+ st.success("โœ… Web search enabled")
338
+ if st.button("๐Ÿ”„ Change API Key"):
339
+ st.session_state.show_api_input = True
340
+ st.rerun()
341
  else:
342
+ st.warning("โš ๏ธ Web search disabled")
343
+
344
+ # Show API key input field
345
+ if not web_search_enabled or st.session_state.get('show_api_input', False):
346
+ st.markdown("---")
347
+ st.markdown("**Enter your Serper API Key:**")
348
+ st.caption("Get a free API key at [serper.dev](https://serper.dev/) (2,500 searches/month free)")
349
+
350
+ api_key = st.text_input(
351
+ "Serper API Key",
352
+ type="password",
353
+ placeholder="Enter your API key here",
354
+ help="Your API key is not stored and only used during this session",
355
+ key="serper_api_key_input"
356
+ )
357
+
358
+ if api_key:
359
+ if st.button("Enable Web Search", type="primary"):
360
+ success = st.session_state.chatbot.init_web_search(api_key)
361
+ if success:
362
+ st.success("โœ… Web search enabled!")
363
+ st.session_state.show_api_input = False
364
+ st.rerun()
365
+ else:
366
+ st.error("โŒ Invalid API key. Please check and try again.")
367
+
368
+ if not api_key:
369
+ st.info("๐Ÿ’ก Web search is optional. The chatbot works with documents only.")
370
+
371
+ st.markdown("---")
372
 
373
  # Main chat interface
374
  st.header("Chat Interface")