|
import os |
|
import time |
|
|
|
import streamlit as st |
|
|
|
from extract import extract_text_from_pdfs |
|
from generate import generate_response |
|
from preprocess import preprocess_text |
|
from retrieve import create_vectorizer, retrieve |
|
|
|
|
|
st.title("RAG-based PDF Query System") |
|
|
|
uploaded_files = st.file_uploader("Upload PDFs", type=["pdf"], accept_multiple_files=True) |
|
|
|
if uploaded_files: |
|
st.write("Processing the uploaded PDFs...") |
|
|
|
|
|
progress_bar = st.progress(0) |
|
status_text = st.empty() |
|
|
|
|
|
pdf_files = [] |
|
for uploaded_file in uploaded_files: |
|
with open(uploaded_file.name, "wb") as f: |
|
f.write(uploaded_file.getbuffer()) |
|
pdf_files.append(uploaded_file.name) |
|
|
|
|
|
num_files = len(pdf_files) |
|
texts = [] |
|
for i, pdf_file in enumerate(pdf_files): |
|
status_text.text(f"Extracting text from file {i + 1} of {num_files}...") |
|
text = extract_text_from_pdfs([pdf_file]) |
|
texts.extend(text) |
|
progress_bar.progress((i + 1) / num_files) |
|
time.sleep(0.1) |
|
|
|
|
|
status_text.text("Preprocessing text...") |
|
progress_bar.progress(0.5) |
|
processed_texts = preprocess_text(texts) |
|
time.sleep(0.1) |
|
|
|
|
|
status_text.text("Creating vectorizer and transforming texts...") |
|
progress_bar.progress(0.75) |
|
vectorizer, X = create_vectorizer(processed_texts) |
|
time.sleep(0.1) |
|
|
|
|
|
progress_bar.progress(1.0) |
|
status_text.text("Processing complete!") |
|
|
|
query = st.text_input("Enter your query:") |
|
|
|
if query: |
|
|
|
top_indices = retrieve(query, X, vectorizer) |
|
retrieved_texts = [texts[i] for i in top_indices] |
|
|
|
|
|
response = generate_response(retrieved_texts, query) |
|
|
|
st.write("Response:") |
|
st.write(response) |
|
|
|
|
|
for pdf_file in pdf_files: |
|
os.remove(pdf_file) |
|
|