tejovanth commited on
Commit
bfd9f8e
·
verified ·
1 Parent(s): d838df2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -12
app.py CHANGED
@@ -1,24 +1,39 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
  # Load summarization pipeline
5
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
6
 
7
- # Define function to summarize input text
8
- def summarize_notes(text):
9
- if len(text.strip()) == 0:
10
- return "Please paste your academic notes."
11
- summary = summarizer(text, max_length=150, min_length=40, do_sample=False)
12
- return summary[0]['summary_text']
 
13
 
14
- # Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  demo = gr.Interface(
16
- fn=summarize_notes,
17
- inputs=gr.Textbox(lines=15, placeholder="Paste your academic notes here...", label="Academic Notes"),
18
  outputs=gr.Textbox(label="Summarized Notes"),
19
- title="📚 Academic Note Summarizer",
20
- description="Paste your long academic notes and get a short summary using a Hugging Face Transformer model."
21
  )
22
 
23
- # Launch the app
24
  demo.launch()
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import fitz # PyMuPDF
4
 
5
  # Load summarization pipeline
6
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
7
 
8
+ # Function to extract text from PDF
9
+ def extract_text_from_pdf(pdf_file):
10
+ doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
11
+ text = ""
12
+ for page in doc:
13
+ text += page.get_text()
14
+ return text
15
 
16
+ # Combine everything into one function
17
+ def summarize_pdf(pdf_file):
18
+ try:
19
+ text = extract_text_from_pdf(pdf_file)
20
+ if len(text.strip()) == 0:
21
+ return "The PDF seems empty or text is not extractable."
22
+ # Truncate long text (BART model has ~1024 token limit)
23
+ text = text[:3000]
24
+ summary = summarizer(text, max_length=150, min_length=40, do_sample=False)
25
+ return summary[0]['summary_text']
26
+ except Exception as e:
27
+ return f"Error: {str(e)}"
28
+
29
+ # Gradio Interface
30
  demo = gr.Interface(
31
+ fn=summarize_pdf,
32
+ inputs=gr.File(label="Upload PDF of Academic Notes"),
33
  outputs=gr.Textbox(label="Summarized Notes"),
34
+ title="📄 Academic Note Summarizer (PDF)",
35
+ description="Upload your academic notes in PDF format. The app will extract and summarize the content using a Hugging Face model."
36
  )
37
 
 
38
  demo.launch()
39
+