Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,14 +6,18 @@ import fitz # PyMuPDF
|
|
| 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(
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Streamlit app
|
| 19 |
def main():
|
|
@@ -26,22 +30,23 @@ def main():
|
|
| 26 |
# Read the PDF file and extract text
|
| 27 |
pdf_text = extract_text_from_pdf(uploaded_file)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
if
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
| 47 |
main()
|
|
|
|
| 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(uploaded_file):
|
| 10 |
+
try:
|
| 11 |
+
doc = fitz.open(stream=uploaded_file, filetype="pdf")
|
| 12 |
+
text = ""
|
| 13 |
+
for page_num in range(doc.page_count):
|
| 14 |
+
page = doc[page_num]
|
| 15 |
+
text += page.get_text()
|
| 16 |
+
doc.close()
|
| 17 |
+
return text
|
| 18 |
+
except Exception as e:
|
| 19 |
+
st.error(f"Error extracting text from PDF: {str(e)}")
|
| 20 |
+
return None
|
| 21 |
|
| 22 |
# Streamlit app
|
| 23 |
def main():
|
|
|
|
| 30 |
# Read the PDF file and extract text
|
| 31 |
pdf_text = extract_text_from_pdf(uploaded_file)
|
| 32 |
|
| 33 |
+
if pdf_text is not None:
|
| 34 |
+
# Display the extracted text
|
| 35 |
+
st.subheader("Extracted Text from PDF")
|
| 36 |
+
st.text(pdf_text)
|
| 37 |
+
|
| 38 |
+
# Input for user question
|
| 39 |
+
question = st.text_input("Ask a question about the PDF:")
|
| 40 |
+
|
| 41 |
+
# Button to trigger question answering
|
| 42 |
+
if st.button("Get Answer"):
|
| 43 |
+
if question:
|
| 44 |
+
# Use the QA model to get the answer
|
| 45 |
+
answer = qa_model(question=question, context=pdf_text)
|
| 46 |
+
st.subheader("Answer:")
|
| 47 |
+
st.write(answer["answer"])
|
| 48 |
+
else:
|
| 49 |
+
st.warning("Please enter a question.")
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
| 52 |
main()
|