Create app.py (Streamlit interface)
Browse files- app.py (Streamlit interface) +42 -0
app.py (Streamlit interface)
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from pipeline import process_pdf, enrich_metadata, semantic_split, build_vector_db, load_vector_db, get_qa_chain
|
| 3 |
+
import tempfile, os
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="Bangladesh Law RAG", layout="wide")
|
| 6 |
+
|
| 7 |
+
st.title("π Bangladesh Law Assistant")
|
| 8 |
+
st.markdown("Ask questions about Bangladesh ICT, Labour, Penal Code, and Constitution documents.")
|
| 9 |
+
|
| 10 |
+
# Upload and process PDF
|
| 11 |
+
with st.sidebar:
|
| 12 |
+
st.header("π Upload Legal PDF")
|
| 13 |
+
uploaded_file = st.file_uploader("Choose a PDF", type="pdf")
|
| 14 |
+
if uploaded_file:
|
| 15 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 16 |
+
tmp.write(uploaded_file.read())
|
| 17 |
+
path = tmp.name
|
| 18 |
+
st.success("PDF uploaded. Processing...")
|
| 19 |
+
pages = process_pdf(path)
|
| 20 |
+
docs = enrich_metadata(pages)
|
| 21 |
+
chunks = semantic_split(docs)
|
| 22 |
+
build_vector_db(chunks)
|
| 23 |
+
st.success("Vector DB updated successfully.")
|
| 24 |
+
|
| 25 |
+
# Load vector DB
|
| 26 |
+
vectorstore = load_vector_db()
|
| 27 |
+
qa_chain = get_qa_chain(vectorstore)
|
| 28 |
+
|
| 29 |
+
# Query box
|
| 30 |
+
query = st.text_input("π Enter your legal query:")
|
| 31 |
+
if query:
|
| 32 |
+
result = qa_chain({"query": query})
|
| 33 |
+
answer = result["result"]
|
| 34 |
+
sources = result["source_documents"]
|
| 35 |
+
|
| 36 |
+
st.markdown("### π§ Answer")
|
| 37 |
+
st.write(answer)
|
| 38 |
+
|
| 39 |
+
st.markdown("### π Sources")
|
| 40 |
+
for doc in sources:
|
| 41 |
+
meta = doc.metadata
|
| 42 |
+
st.markdown(f"- `{meta.get('law_name', 'Unknown')} - {meta.get('section_heading', 'Unknown')}` ({meta.get('source')})")
|