Azidan commited on
Commit
b512372
·
verified ·
1 Parent(s): e45c1f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -16
app.py CHANGED
@@ -4,6 +4,7 @@ import torch
4
  import pdfplumber
5
  from docx import Document
6
  import io
 
7
 
8
  # Load the summarization model once
9
  device = 0 if torch.cuda.is_available() else -1
@@ -11,32 +12,40 @@ print(f"Using device: {'GPU' if device == 0 else 'CPU'}")
11
 
12
  summarizer = pipeline(
13
  "summarization",
14
- model="sshleifer/distilbart-cnn-12-6", # Fast & good for CPU; change to "facebook/bart-large-cnn" if you get GPU
15
  device=device
16
  )
17
 
18
- def extract_text(file):
19
- if file is None:
20
  return ""
21
- filename = file.name.lower()
22
- content = file.read()
 
 
 
23
  try:
24
  if filename.endswith('.pdf'):
25
- with pdfplumber.open(io.BytesIO(content)) as pdf:
26
  return "\n".join(page.extract_text() or "" for page in pdf.pages)
 
27
  elif filename.endswith('.docx'):
28
- doc = Document(io.BytesIO(content))
29
  return "\n".join(para.text for para in doc.paragraphs if para.text.strip())
 
30
  elif filename.endswith('.txt'):
31
- return content.decode('utf-8', errors='replace')
 
 
32
  else:
33
  return "Unsupported file. Please use .pdf, .docx, or .txt"
 
34
  except Exception as e:
35
  return f"Error reading file: {str(e)}"
36
 
37
- def summarize(input_text, file, detail_level):
38
- if file is not None:
39
- text = extract_text(file)
40
  else:
41
  text = input_text.strip()
42
 
@@ -47,7 +56,7 @@ def summarize(input_text, file, detail_level):
47
  if words < 100:
48
  return text # Too short → return as-is
49
 
50
- # Convert slider (0.15 to 0.60) to target length ratio
51
  target_ratio = detail_level
52
  target_length = int(words * target_ratio)
53
  max_l = max(500, min(1400, target_length + 250))
@@ -92,9 +101,8 @@ interface = gr.Interface(
92
  outputs=gr.Textbox(label="Generated Summary"),
93
  title="Lecture Summarizer",
94
  description="Upload a lecture file (PDF/DOCX/TXT) or paste text. Adjust the slider for shorter or more detailed summaries.",
95
- flagging_mode="never", # This disables flagging correctly
96
- # NO theme here anymore
97
  )
98
 
99
- # Launch with theme here (Gradio 6.0+ requirement)
100
- interface.launch(theme="soft") # Or gr.themes.Soft() if you want to import gr.themes
 
4
  import pdfplumber
5
  from docx import Document
6
  import io
7
+ import os
8
 
9
  # Load the summarization model once
10
  device = 0 if torch.cuda.is_available() else -1
 
12
 
13
  summarizer = pipeline(
14
  "summarization",
15
+ model="sshleifer/distilbart-cnn-12-6", # Fast & good for CPU
16
  device=device
17
  )
18
 
19
+ def extract_text(file_path):
20
+ if file_path is None:
21
  return ""
22
+
23
+ # file_path is a string (temp path) or NamedString-like object; convert to str
24
+ file_path = str(file_path) # Ensure it's a plain string
25
+ filename = os.path.basename(file_path).lower()
26
+
27
  try:
28
  if filename.endswith('.pdf'):
29
+ with pdfplumber.open(file_path) as pdf:
30
  return "\n".join(page.extract_text() or "" for page in pdf.pages)
31
+
32
  elif filename.endswith('.docx'):
33
+ doc = Document(file_path)
34
  return "\n".join(para.text for para in doc.paragraphs if para.text.strip())
35
+
36
  elif filename.endswith('.txt'):
37
+ with open(file_path, "r", encoding="utf-8", errors="replace") as f:
38
+ return f.read()
39
+
40
  else:
41
  return "Unsupported file. Please use .pdf, .docx, or .txt"
42
+
43
  except Exception as e:
44
  return f"Error reading file: {str(e)}"
45
 
46
+ def summarize(input_text, file_path, detail_level):
47
+ if file_path is not None:
48
+ text = extract_text(file_path)
49
  else:
50
  text = input_text.strip()
51
 
 
56
  if words < 100:
57
  return text # Too short → return as-is
58
 
59
+ # Convert slider to target ratio
60
  target_ratio = detail_level
61
  target_length = int(words * target_ratio)
62
  max_l = max(500, min(1400, target_length + 250))
 
101
  outputs=gr.Textbox(label="Generated Summary"),
102
  title="Lecture Summarizer",
103
  description="Upload a lecture file (PDF/DOCX/TXT) or paste text. Adjust the slider for shorter or more detailed summaries.",
104
+ flagging_mode="never",
 
105
  )
106
 
107
+ # Launch with theme
108
+ interface.launch(theme="soft")