File size: 1,710 Bytes
c1ba64e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import pipeline
from PyPDF2 import PdfReader
import docx

# Initialize the NLP pipeline
nlp = pipeline(
    "document-question-answering",
    model="impira/layoutlm-document-qa",
)

# Set the title of the app
st.title("LayoutLM Example")

# Create a file uploader that accepts various document formats
uploaded_file = st.file_uploader("Drag and drop a document here", type=['txt', 'pdf', 'docx'])

# Create a text box for user input
question = st.text_area("What would you like to know?")

def extract_text_from_file(uploaded_file):
    if uploaded_file.type == "text/plain":
        return uploaded_file.read().decode("utf-8")
    elif uploaded_file.type == "application/pdf":
        reader = PdfReader(uploaded_file)
        text = ""
        for page in reader.pages:
            text += page.extract_text()
        return text
    elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
        doc = docx.Document(uploaded_file)
        text = "\n".join([paragraph.text for paragraph in doc.paragraphs])
        return text
    else:
        return None

if uploaded_file and question:
    # Extract text from the uploaded document
    document_text = extract_text_from_file(uploaded_file)

    if document_text:
        # Run the NLP model on the extracted text and the user's question
        answer = nlp(
            {
                "context": document_text,
                "question": question
            }
        )

        # Display the answer
        st.write("Answer:")
        st.write(answer['answer'])
    else:
        st.write("Unsupported file type or failed to extract text from the document.")