pratikshahp commited on
Commit
e2e0f97
1 Parent(s): ae6a409

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -10,24 +10,27 @@ def process_pdf(uploaded_file, qa_model, tokenizer):
10
  file_contents = uploaded_file.read()
11
 
12
  # Process the PDF file
13
- doc = fitz.open(stream=uploaded_file.read(), filetype="pdf")
14
- text = ""
15
- for page in doc:
16
- text += page.get_text()
17
-
18
- # Tokenize the text
19
- inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
20
-
21
- # Perform question answering
22
- outputs = qa_model(**inputs)
23
- start_scores = outputs.start_logits
24
- end_scores = outputs.end_logits
25
-
26
- # Display the generated questions and answers
27
- for i, (start, end) in enumerate(zip(start_scores, end_scores)):
28
- answer = tokenizer.decode(inputs["input_ids"][i][start.argmax():end.argmax()+1])
29
- st.write("Answer:", answer)
30
- st.write("---")
 
 
 
31
 
32
  # Main function
33
  def main():
 
10
  file_contents = uploaded_file.read()
11
 
12
  # Process the PDF file
13
+ doc = fitz.open(file_contents, filetype="pdf")
14
+ if doc is not None:
15
+ text = ""
16
+ for page in doc:
17
+ text += page.get_text()
18
+
19
+ # Tokenize the text
20
+ inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
21
+
22
+ # Perform question answering
23
+ outputs = qa_model(**inputs)
24
+ start_scores = outputs.start_logits
25
+ end_scores = outputs.end_logits
26
+
27
+ # Display the generated questions and answers
28
+ for i, (start, end) in enumerate(zip(start_scores, end_scores)):
29
+ answer = tokenizer.decode(inputs["input_ids"][i][start.argmax():end.argmax()+1])
30
+ st.write("Answer:", answer)
31
+ st.write("---")
32
+ else:
33
+ st.error("Error occurred while opening the PDF file.")
34
 
35
  # Main function
36
  def main():