File size: 1,695 Bytes
6d9f20a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import os
import shutil
from chain import extract_pdf_with_user, get_final_aswer

UPLOAD_DIR = "uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)

st.title("PDF Extraction and Question Answering")

# --- Step 1: Upload PDF and extract ---

st.header("Upload PDF and extract text")

user_id = st.text_input("User ID")
name = st.text_input("Save PDF name (without extension)")

uploaded_file = st.file_uploader("Upload PDF file", type=["pdf"])

if st.button("Extract PDF"):

    if not user_id or not name or not uploaded_file:
        st.error("Please provide User ID, name, and upload a PDF file.")
    else:
        # Save uploaded PDF
        upload_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
        with open(upload_path, "wb") as f:
            shutil.copyfileobj(uploaded_file, f)

        # Call your extraction function
        documents = extract_pdf_with_user(user_id, upload_path, name)
        st.success("PDF processed!")
        st.write("Extracted documents:")
        st.write(documents)

# --- Step 2: Ask a question ---

st.header("Ask a question")

query = st.text_input("Enter your question")

if st.button("Get Answer"):

    if not user_id or not name or not query:
        st.error("Please provide User ID, name, and your question.")
    else:
        pdf_path = os.path.join(UPLOAD_DIR, f"{name}.pdf")
        if not os.path.exists(pdf_path):
            st.error(f"No PDF found with name '{name}.pdf'. Please upload and extract first.")
        else:
            answer = get_final_aswer(query, user_id, pdf_path)
            st.success("Answer generated!")
            st.write(answer)