Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import fitz # PyMuPDF
|
4 |
+
|
5 |
+
# Load the QA model
|
6 |
+
qa_model = pipeline("question-answering", "timpal0l/mdeberta-v3-base-squad2")
|
7 |
+
|
8 |
+
# Function to extract text from a PDF file
|
9 |
+
def extract_text_from_pdf(pdf_file):
|
10 |
+
doc = fitz.open(pdf_file)
|
11 |
+
text = ""
|
12 |
+
for page_num in range(doc.page_count):
|
13 |
+
page = doc[page_num]
|
14 |
+
text += page.get_text()
|
15 |
+
doc.close()
|
16 |
+
return text
|
17 |
+
|
18 |
+
# Streamlit app
|
19 |
+
def main():
|
20 |
+
st.title("PDF Question Answering App")
|
21 |
+
|
22 |
+
# Upload PDF file through Streamlit
|
23 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
24 |
+
|
25 |
+
if uploaded_file is not None:
|
26 |
+
# Read the PDF file and extract text
|
27 |
+
pdf_text = extract_text_from_pdf(uploaded_file)
|
28 |
+
|
29 |
+
# Display the extracted text
|
30 |
+
st.subheader("Extracted Text from PDF")
|
31 |
+
st.text(pdf_text)
|
32 |
+
|
33 |
+
# Input for user question
|
34 |
+
question = st.text_input("Ask a question about the PDF:")
|
35 |
+
|
36 |
+
# Button to trigger question answering
|
37 |
+
if st.button("Get Answer"):
|
38 |
+
if question:
|
39 |
+
# Use the QA model to get the answer
|
40 |
+
answer = qa_model(question=question, context=pdf_text)
|
41 |
+
st.subheader("Answer:")
|
42 |
+
st.write(answer["answer"])
|
43 |
+
else:
|
44 |
+
st.warning("Please enter a question.")
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
main()
|