ivyblossom commited on
Commit
ed5b1fa
1 Parent(s): 4b5d666

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -1,13 +1,11 @@
1
- !pip install gradio
2
-
3
  import os
4
  import streamlit as st
5
  from transformers import pipeline
6
  from PyPDF2 import PdfReader
7
  import tempfile
8
- import gradio as gr
9
 
10
  # Function to perform question-answering
 
11
  def question_answering(questions, pdf_text):
12
  # Perform question-answering using Hugging Face's Transformers
13
  question_answerer = pipeline("question-answering", model="distilbert-base-cased-distilled-squad", tokenizer="distilbert-base-cased-distilled-squad")
@@ -15,8 +13,15 @@ def question_answering(questions, pdf_text):
15
 
16
  return answers
17
 
18
- def main(uploaded_file, question):
19
- if uploaded_file is not None:
 
 
 
 
 
 
 
20
  pdf_path = os.path.join(tempfile.gettempdir(), uploaded_file.name)
21
  with open(pdf_path, "wb") as f:
22
  f.write(uploaded_file.read())
@@ -25,15 +30,14 @@ def main(uploaded_file, question):
25
  pdf_reader = PdfReader(pdf_path)
26
  pdf_text = "\n".join([pdf_page.extract_text() for pdf_page in pdf_reader.pages])
27
 
28
- # Perform question-answering
29
- answers = question_answering([question], pdf_text)
30
 
31
- result = f"Question: '{question}'\nAnswer: {answers[0]['answer']}\nScore: {answers[0]['score']:.2f}"
32
- return result
 
 
 
33
 
34
- iface = gr.Interface(fn=main,
35
- inputs=[gr.inputs.File(type="file", label="Upload a PDF file"),
36
- gr.inputs.Textbox(label="Enter a question:")],
37
- outputs=gr.outputs.Textbox(label="Question Answering Results"),
38
- live=True)
39
- iface.launch()
 
 
 
1
  import os
2
  import streamlit as st
3
  from transformers import pipeline
4
  from PyPDF2 import PdfReader
5
  import tempfile
 
6
 
7
  # Function to perform question-answering
8
+ @st.cache_data(show_spinner=False)
9
  def question_answering(questions, pdf_text):
10
  # Perform question-answering using Hugging Face's Transformers
11
  question_answerer = pipeline("question-answering", model="distilbert-base-cased-distilled-squad", tokenizer="distilbert-base-cased-distilled-squad")
 
13
 
14
  return answers
15
 
16
+ def main():
17
+ st.title("Question Answering on PDF Files")
18
+
19
+ uploaded_file = st.file_uploader("Upload a PDF file:", type=["pdf"])
20
+
21
+ st.write("Enter your question(s) below (separate multiple questions with new lines):")
22
+ questions = st.text_area("Questions").split('\n')
23
+
24
+ if st.button("Answer") and uploaded_file is not None:
25
  pdf_path = os.path.join(tempfile.gettempdir(), uploaded_file.name)
26
  with open(pdf_path, "wb") as f:
27
  f.write(uploaded_file.read())
 
30
  pdf_reader = PdfReader(pdf_path)
31
  pdf_text = "\n".join([pdf_page.extract_text() for pdf_page in pdf_reader.pages])
32
 
33
+ # Perform question-answering in batches
34
+ answers = question_answering(questions, pdf_text)
35
 
36
+ st.write("Questions and Answers:")
37
+ for i, (question, answer) in enumerate(zip(questions, answers)):
38
+ st.write(f"Question {i + 1}: '{question}'")
39
+ st.write("Answer:", answer['answer'])
40
+ st.write("Score:", answer['score'])
41
 
42
+ if __name__ == "__main__":
43
+ main()