Spaces:
Sleeping
Sleeping
app.py checking in
Browse files
app.py
CHANGED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
import fitz # PyMuPDF
|
4 |
+
|
5 |
+
# Use a pipeline as a high-level helper
|
6 |
+
from transformers import pipeline
|
7 |
+
|
8 |
+
qna = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
9 |
+
|
10 |
+
|
11 |
+
def read_pdf(file_path):
|
12 |
+
try:
|
13 |
+
# Open the PDF file
|
14 |
+
pdf_document = fitz.open(file_path)
|
15 |
+
|
16 |
+
# Initialize a string to store the content
|
17 |
+
content = ""
|
18 |
+
|
19 |
+
# Iterate through each page
|
20 |
+
for page_num in range(len(pdf_document)):
|
21 |
+
page = pdf_document.load_page(page_num) # Load the page
|
22 |
+
content += page.get_text() # Extract text from the page
|
23 |
+
|
24 |
+
# Close the PDF file
|
25 |
+
pdf_document.close()
|
26 |
+
|
27 |
+
return content
|
28 |
+
except Exception as e:
|
29 |
+
return str(e)
|
30 |
+
|
31 |
+
def answer_question(file_path, question):
|
32 |
+
context = read_pdf(file_path)
|
33 |
+
answer = qna(question= question, context=context)
|
34 |
+
return answer['answer']
|
35 |
+
|
36 |
+
gr.close_all()
|
37 |
+
|
38 |
+
|
39 |
+
demo = gr.Interface(answer_question,
|
40 |
+
inputs=[gr.File(file_types=['pdf'], label='Upload a pdf file'), gr.Textbox(label='Please ask question?', lines=3)],
|
41 |
+
|
42 |
+
outputs=[gr.Textbox(label='Answer is:', lines=3)],
|
43 |
+
title="Gen AI Learning Project 5: Question and Answer with PDF file",
|
44 |
+
description="This Application uses 'deepset/roberta-base-squad2' to simple short answer from PDF content to questions asked.")
|
45 |
+
demo.launch()
|
46 |
+
|