tbels89 commited on
Commit
026a6c2
·
1 Parent(s): 85cc6e0

updated db connection

Browse files
Files changed (1) hide show
  1. app.py +85 -85
app.py CHANGED
@@ -428,95 +428,95 @@ def send_query_to_llm(messages):
428
  try:
429
  init_db() # Ensure DB is ready
430
  logger.info("DB initialized successfully.")
431
- except Exception as e:
432
- logger.error(f"Failed to initialize DB: {e}")
433
- raise ValueError("Failed to initialize DB.")
434
 
435
- @st.cache_resource
436
- def load_retriever():
437
- try:
438
- retriever = Retriever()
439
- logger.info("Retriever initialized successfully.")
440
- return retriever
441
- except Exception as e:
442
- logger.error(f"Failed to initialize Retriever: {e}")
443
- return None
444
-
445
- retriever = load_retriever()
446
-
447
- st.title("Prometheus v0.2 - Personal Tutor")
448
-
449
- # Instructions
450
- with st.expander("📋 Instructions", expanded=True):
451
- st.write(
452
- """
453
- - Press **Enter** to submit your query.
454
- - Type **'quit'** to clear the conversation history.
455
- """
456
- )
457
-
458
- # Chat-like UI for conversation history
459
- st.subheader("💬 Conversation History")
460
- messages = load_messages()
461
- for m in messages:
462
- with st.chat_message(m['role']):
463
- st.markdown(m['content'])
464
-
465
- # Input area for user query
466
- if prompt := st.chat_input("Ask Prometheus... Type 'quit' to clear history."):
467
- # If user inputs 'quit', clear conversation history
468
- if prompt.strip().lower() == 'quit':
469
  try:
470
- conn = sqlite3.connect(DB_NAME)
471
- c = conn.cursor()
472
- c.execute("DELETE FROM conversations")
473
- conn.commit()
474
- logger.info("Conversation history cleared.")
475
- st.success("Conversation history cleared.")
476
- except sqlite3.Error as e:
477
- logger.error(f"Error clearing conversation history: {e}")
478
- st.error("Failed to clear conversation history.")
479
- finally:
480
- conn.close()
481
- else:
482
- # Immediately display user message in the UI
483
- messages.append({"role": "user", "content": prompt.strip()})
484
- with st.chat_message("user"):
485
- st.markdown(prompt.strip())
486
-
487
- # Add user message to DB
488
- add_message("user", prompt.strip())
489
-
490
- # Retrieve chunks
491
- if retriever:
492
- results = retriever.retrieve(prompt.strip(), k=TOP_K)
493
- logger.info(f"Retrieved {len(results)} chunks for query: '{prompt.strip()}'")
494
- else:
495
- results = []
496
- logger.warning("Retriever is not available.")
497
-
498
- # Load updated messages (including user query) - checking if nessessary
499
- #messages = load_messages()
500
-
501
- # Build prompt
502
- llm_prompt = build_prompt(prompt.strip(), results, messages)
503
 
504
- # Call LLM
505
- assistant_response = send_query_to_llm(llm_prompt)
506
 
507
- # Add assistant response to DB
508
- add_message("assistant", assistant_response)
509
 
510
- # Display assistant response
511
- with st.chat_message("assistant"):
512
- st.markdown(assistant_response)
 
 
 
 
 
513
 
514
- # Display retrieved sources in the sidebar
515
- with st.sidebar:
516
- st.header("📖 Sources")
517
- if results:
518
- for i, res in enumerate(results):
519
- st.markdown(f"- **Result {i+1}:** Page {res['page_num']}")
520
- st.code(res['content'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
521
  else:
522
- st.warning("No relevant sources found.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
  try:
429
  init_db() # Ensure DB is ready
430
  logger.info("DB initialized successfully.")
 
 
 
431
 
432
+ @st.cache_resource
433
+ def load_retriever():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
434
  try:
435
+ retriever = Retriever()
436
+ logger.info("Retriever initialized successfully.")
437
+ return retriever
438
+ except Exception as e:
439
+ logger.error(f"Failed to initialize Retriever: {e}")
440
+ return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
441
 
442
+ retriever = load_retriever()
 
443
 
444
+ st.title("Prometheus v0.2 - Personal Tutor")
 
445
 
446
+ # Instructions
447
+ with st.expander("📋 Instructions", expanded=True):
448
+ st.write(
449
+ """
450
+ - Press **Enter** to submit your query.
451
+ - Type **'quit'** to clear the conversation history.
452
+ """
453
+ )
454
 
455
+ # Chat-like UI for conversation history
456
+ st.subheader("💬 Conversation History")
457
+ messages = load_messages()
458
+ for m in messages:
459
+ with st.chat_message(m['role']):
460
+ st.markdown(m['content'])
461
+
462
+ # Input area for user query
463
+ if prompt := st.chat_input("Ask Prometheus... Type 'quit' to clear history."):
464
+ # If user inputs 'quit', clear conversation history
465
+ if prompt.strip().lower() == 'quit':
466
+ try:
467
+ conn = sqlite3.connect(DB_NAME)
468
+ c = conn.cursor()
469
+ c.execute("DELETE FROM conversations")
470
+ conn.commit()
471
+ logger.info("Conversation history cleared.")
472
+ st.success("Conversation history cleared.")
473
+ except sqlite3.Error as e:
474
+ logger.error(f"Error clearing conversation history: {e}")
475
+ st.error("Failed to clear conversation history.")
476
+ finally:
477
+ conn.close()
478
  else:
479
+ # Immediately display user message in the UI
480
+ messages.append({"role": "user", "content": prompt.strip()})
481
+ with st.chat_message("user"):
482
+ st.markdown(prompt.strip())
483
+
484
+ # Add user message to DB
485
+ add_message("user", prompt.strip())
486
+
487
+ # Retrieve chunks
488
+ if retriever:
489
+ results = retriever.retrieve(prompt.strip(), k=TOP_K)
490
+ logger.info(f"Retrieved {len(results)} chunks for query: '{prompt.strip()}'")
491
+ else:
492
+ results = []
493
+ logger.warning("Retriever is not available.")
494
+
495
+ # Load updated messages (including user query) - checking if nessessary
496
+ #messages = load_messages()
497
+
498
+ # Build prompt
499
+ llm_prompt = build_prompt(prompt.strip(), results, messages)
500
+
501
+ # Call LLM
502
+ assistant_response = send_query_to_llm(llm_prompt)
503
+
504
+ # Add assistant response to DB
505
+ add_message("assistant", assistant_response)
506
+
507
+ # Display assistant response
508
+ with st.chat_message("assistant"):
509
+ st.markdown(assistant_response)
510
+
511
+ # Display retrieved sources in the sidebar
512
+ with st.sidebar:
513
+ st.header("📖 Sources")
514
+ if results:
515
+ for i, res in enumerate(results):
516
+ st.markdown(f"- **Result {i+1}:** Page {res['page_num']}")
517
+ st.code(res['content'])
518
+ else:
519
+ st.warning("No relevant sources found.")
520
+ except Exception as e:
521
+ logger.error(f"Failed to initialize DB: {e}")
522
+ raise ValueError("Failed to initialize DB.")