| import os |
| import streamlit as st |
| from rag_utility import process_document_to_chroma_db, answer_question |
|
|
| |
| st.set_page_config( |
| page_title="AI Doubt Teacher - RAG", |
| page_icon="π", |
| layout="wide" |
| ) |
|
|
| |
| working_dir = "pdfs" |
|
|
| |
| os.makedirs(working_dir, exist_ok=True) |
|
|
| |
| if "processed_files" not in st.session_state: |
| st.session_state.processed_files = set() |
| if "db_ready" not in st.session_state: |
| st.session_state.db_ready = False |
|
|
| st.title("π AI Doubt Teacher - Document RAG") |
| st.caption("Upload your textbooks/notes and ask doubts in natural language") |
|
|
| |
| with st.sidebar: |
| st.header("π Document Manager") |
| |
| |
| uploaded_file = st.file_uploader( |
| "Upload a PDF file", |
| type=["pdf"], |
| accept_multiple_files=False |
| ) |
| |
| if uploaded_file is not None: |
| save_path = os.path.join(working_dir, uploaded_file.name) |
| |
| |
| if uploaded_file.name in st.session_state.processed_files: |
| st.info(f"π '{uploaded_file.name}' is already processed.") |
| else: |
| |
| try: |
| with open(save_path, "wb") as f: |
| f.write(uploaded_file.getbuffer()) |
| st.success(f"β
File '{uploaded_file.name}' uploaded!") |
| |
| |
| with st.spinner("π Processing document... This may take a minute."): |
| try: |
| result = process_document_to_chroma_db(working_dir) |
| st.session_state.processed_files.add(uploaded_file.name) |
| st.session_state.db_ready = True |
| st.success("β
Document processed! Ready for Q&A.") |
| except Exception as e: |
| st.error(f"β οΈ Error: {e}") |
| except Exception as e: |
| st.error(f"β οΈ Error saving file: {e}") |
| |
| |
| if st.session_state.processed_files: |
| st.divider() |
| st.subheader("π Processed Documents") |
| for file_name in st.session_state.processed_files: |
| st.markdown(f"- π {file_name}") |
| |
| |
| if st.session_state.processed_files: |
| if st.button("ποΈ Clear All Documents", type="secondary"): |
| |
| import shutil |
| db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "doc_vectorstore") |
| if os.path.exists(db_path): |
| shutil.rmtree(db_path) |
| |
| for file_name in os.listdir(working_dir): |
| os.remove(os.path.join(working_dir, file_name)) |
| st.session_state.processed_files.clear() |
| st.session_state.db_ready = False |
| st.rerun() |
|
|
| |
| st.divider() |
| st.subheader("π¬ Ask Your Doubt") |
|
|
| |
| user_question = st.text_area( |
| "Type your question about the uploaded documents:", |
| placeholder="e.g., What is Newton's First Law? Explain with examples.", |
| height=100 |
| ) |
|
|
| col1, col2, col3 = st.columns([1, 1, 3]) |
| with col1: |
| ask_button = st.button("π Get Answer", type="primary", use_container_width=True) |
| with col2: |
| clear_button = st.button("ποΈ Clear Question", use_container_width=True) |
|
|
| if clear_button: |
| st.session_state.user_question = "" |
| st.rerun() |
|
|
| if ask_button: |
| if not st.session_state.db_ready: |
| st.warning("β οΈ Please upload and process a document first.") |
| elif not user_question.strip(): |
| st.warning("β οΈ Please enter a question.") |
| else: |
| with st.spinner("π€ Thinking..."): |
| try: |
| answer = answer_question(user_question) |
| |
| |
| st.markdown("### π€ AI Tutor's Answer") |
| st.markdown( |
| f""" |
| <div style=" |
| background-color: #f0f2f6; |
| padding: 20px; |
| border-radius: 10px; |
| border-left: 5px solid #4CAF50; |
| margin-bottom: 20px; |
| "> |
| {answer} |
| </div> |
| """, |
| unsafe_allow_html=True |
| ) |
| |
| |
| if st.button("π Copy Answer"): |
| st.write("Answer copied to clipboard! (Use Ctrl+C)") |
| |
| except Exception as e: |
| st.error(f"β οΈ Error generating answer: {e}") |
|
|
| |
| st.divider() |
| st.caption("Built with β€οΈ using Groq, DeepSeek-R1, and ChromaDB") |