Anupam251272 commited on
Commit
badae39
1 Parent(s): 31523b3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import PyPDF2
4
+ import torch
5
+
6
+ # Function to extract text from PDF
7
+ def extract_text_from_pdf(pdf_file):
8
+ text = ""
9
+ try:
10
+ pdf_reader = PyPDF2.PdfReader(pdf_file)
11
+ for page in pdf_reader.pages:
12
+ text += page.extract_text()
13
+ except Exception as e:
14
+ return f"Error parsing PDF: {str(e)}"
15
+ return text
16
+
17
+ # Load Summarization Model
18
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=0 if torch.cuda.is_available() else -1)
19
+
20
+ # Summarization Function
21
+ def summarize_text(text):
22
+ try:
23
+ summaries = summarizer(text[:1024], max_length=150, min_length=40, do_sample=False)
24
+ return summaries[0]['summary_text']
25
+ except Exception as e:
26
+ return f"Error during summarization: {str(e)}"
27
+
28
+ # Load Question-Answering Model
29
+ qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2", device=0 if torch.cuda.is_available() else -1)
30
+
31
+ # Key Findings Extraction Function
32
+ def extract_key_findings(text, query="What are the main findings?"):
33
+ try:
34
+ result = qa_pipeline({'question': query, 'context': text[:1024]})
35
+ return result['answer']
36
+ except Exception as e:
37
+ return f"Error extracting key findings: {str(e)}"
38
+
39
+ # Main Function to Process PDF
40
+ def process_paper(pdf_file):
41
+ extracted_text = extract_text_from_pdf(pdf_file.name)
42
+ if "Error" in extracted_text:
43
+ return extracted_text, None, None
44
+
45
+ summary = summarize_text(extracted_text)
46
+ findings = extract_key_findings(extracted_text)
47
+
48
+ return extracted_text, summary, findings
49
+
50
+ # Gradio Interface
51
+ with gr.Blocks() as app:
52
+ gr.Markdown("## AI-Assisted Literature Review Tool")
53
+
54
+ with gr.Row():
55
+ pdf_input = gr.File(label="Upload Research Paper (PDF)")
56
+ process_button = gr.Button("Process")
57
+
58
+ with gr.Row():
59
+ extracted_text_output = gr.Textbox(label="Extracted Text", lines=10, interactive=False)
60
+ summary_output = gr.Textbox(label="Summary", lines=5, interactive=False)
61
+ findings_output = gr.Textbox(label="Key Findings", lines=5, interactive=False)
62
+
63
+ process_button.click(process_paper, inputs=pdf_input, outputs=[extracted_text_output, summary_output, findings_output])
64
+
65
+ app.launch()