Ansar7865 commited on
Commit
6d62583
·
verified ·
1 Parent(s): a10b3b9

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pdfplumber
3
+ from transformers import pipeline
4
+
5
+ # Load HuggingFace free models
6
+ summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
7
+ qa_model = pipeline("question-answering", model="google/flan-t5-small")
8
+
9
+ def extract_text_from_pdf(pdf_file):
10
+ text = ""
11
+ with pdfplumber.open(pdf_file.name) as pdf:
12
+ for page in pdf.pages:
13
+ text += page.extract_text() + "\n"
14
+ return text
15
+
16
+ def summarize_pdf(pdf_file):
17
+ text = extract_text_from_pdf(pdf_file)
18
+ summary = summarizer(text, max_length=200, min_length=50, do_sample=False)
19
+ return summary[0]['summary_text']
20
+
21
+ def generate_qa(pdf_file, question):
22
+ text = extract_text_from_pdf(pdf_file)
23
+ answer = qa_model(question=question, context=text)
24
+ return answer['answer']
25
+
26
+ with gr.Blocks() as demo:
27
+ gr.Markdown("# PDF Summarizer + Q&A (Free)")
28
+
29
+ with gr.Tab("Summarize PDF"):
30
+ pdf_input = gr.File(label="Upload PDF")
31
+ summary_output = gr.Textbox(label="Summary", lines=10)
32
+ summarize_btn = gr.Button("Generate Summary")
33
+ summarize_btn.click(summarize_pdf, inputs=pdf_input, outputs=summary_output)
34
+
35
+ with gr.Tab("PDF Q&A"):
36
+ pdf_input_qa = gr.File(label="Upload PDF")
37
+ question_input = gr.Textbox(label="Ask a Question")
38
+ answer_output = gr.Textbox(label="Answer", lines=5)
39
+ qa_btn = gr.Button("Get Answer")
40
+ qa_btn.click(generate_qa, inputs=[pdf_input_qa, question_input], outputs=answer_output)
41
+
42
+ demo.launch()