File size: 3,310 Bytes
e152803
 
5630f6b
e152803
 
 
 
 
5630f6b
e152803
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# app/main.py
import os, json
import streamlit as st
from ingestion import extract_text_from_pdf, chunk_text
from embeddings import generate_embeddings
from vectorstore import build_faiss_index
from qa import retrieve_chunks, generate_answer
import faiss

# ---------------------------
# App Config
# ---------------------------
st.set_page_config(page_title="Enterprise Knowledge Assistant", layout="wide")
st.title("πŸ“„ Enterprise Knowledge Assistant")
st.caption("Select a document from the library or upload your own, then ask questions.")

# ---------------------------
# Sidebar (Settings + Docs + Credits)
# ---------------------------
with st.sidebar:
    st.image("app/logo.png", width=150)

    # 1. Document Library FIRST
    st.header("πŸ“š Document Library")
    doc_choice = st.radio(
        "Choose a document:",
        ["-- Select --", "Sample PDF", "Upload Custom PDF"],
        index=0
    )

    st.markdown("---")

    # 2. Settings SECOND
    st.header("βš™οΈ Settings")
    chunk_size = st.slider("Chunk Size", 200, 1000, 500, step=100)
    top_k = st.slider("Top K Results", 1, 5, 3)

    st.markdown("---")

    # 3. Branding / Credits LAST
    st.caption("πŸ‘¨β€πŸ’» Built by Shubham Sharma")
    st.markdown("[πŸ“‚ GitHub Repo](https://github.com/shubhamsharma170793-cpu/enterprise-knowledge-assistant)")

# ---------------------------
# Document Handling
# ---------------------------
text, chunks, index = None, None, None

if doc_choice == "-- Select --":
    st.info("⬅️ Please choose **Sample PDF** or **Upload Custom PDF** from the sidebar to continue.")

elif doc_choice == "Sample PDF":
    temp_path = os.path.join("app", "sample.pdf")
    st.success("πŸ“˜ Sample PDF selected")
    text = extract_text_from_pdf(temp_path)
    chunks = chunk_text(text, chunk_size=chunk_size)
    embeddings = generate_embeddings(chunks)
    index = build_faiss_index(embeddings)

elif doc_choice == "Upload Custom PDF":
    uploaded_file = st.file_uploader("πŸ“‚ Upload your PDF", type="pdf")
    if uploaded_file:
        temp_path = "temp.pdf"
        with open(temp_path, "wb") as f:
            f.write(uploaded_file.getbuffer())
        st.success("βœ… Document uploaded and processed!")

        text = extract_text_from_pdf(temp_path)
        chunks = chunk_text(text, chunk_size=chunk_size)
        embeddings = generate_embeddings(chunks)
        index = build_faiss_index(embeddings)

# ---------------------------
# Document Preview
# ---------------------------
if chunks:
    st.subheader("πŸ“‘ Document Preview")
    st.text_area("Extracted text (first 1000 chars)", text[:1000], height=150)
    st.caption(f"πŸ“¦ {len(chunks)} chunks created")

# ---------------------------
# Query Section
# ---------------------------
if index and chunks:
    st.markdown("---")
    st.subheader("πŸ€– Ask a Question")

    user_query = st.text_input("πŸ” Your question about the document:")
    if user_query:
        retrieved = retrieve_chunks(user_query, index, chunks, top_k=top_k)
        answer = generate_answer(user_query, retrieved)

        st.markdown("### βœ… Assistant’s Answer")
        st.write(answer)

        with st.expander("πŸ“„ Supporting Chunks"):
            for i, r in enumerate(retrieved, start=1):
                st.markdown(f"**Chunk {i}:** {r}")