Update app.py
Browse files
app.py
CHANGED
|
@@ -3,16 +3,15 @@ from transformers import pipeline
|
|
| 3 |
import torch
|
| 4 |
import pdfplumber
|
| 5 |
from docx import Document
|
| 6 |
-
import io
|
| 7 |
import os
|
| 8 |
|
| 9 |
-
# Load model
|
| 10 |
device = 0 if torch.cuda.is_available() else -1
|
| 11 |
print(f"Using device: {'GPU' if device == 0 else 'CPU'}")
|
| 12 |
|
| 13 |
summarizer = pipeline(
|
| 14 |
"summarization",
|
| 15 |
-
model="
|
| 16 |
device=device
|
| 17 |
)
|
| 18 |
|
|
@@ -36,7 +35,9 @@ def extract_text(file_path):
|
|
| 36 |
except Exception as e:
|
| 37 |
return f"Error reading file: {str(e)}"
|
| 38 |
|
| 39 |
-
def summarize(input_text, file_path, detail_level):
|
|
|
|
|
|
|
| 40 |
if file_path is not None:
|
| 41 |
text = extract_text(file_path)
|
| 42 |
else:
|
|
@@ -52,16 +53,15 @@ def summarize(input_text, file_path, detail_level):
|
|
| 52 |
target_ratio = detail_level
|
| 53 |
target_length = int(words * target_ratio)
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
max_l = max(
|
| 57 |
-
min_l = max(
|
| 58 |
|
| 59 |
-
# Force min_l < max_l if overflow
|
| 60 |
if min_l >= max_l:
|
| 61 |
-
min_l = max_l
|
| 62 |
-
if min_l < 100:
|
| 63 |
-
min_l = 100
|
| 64 |
|
|
|
|
|
|
|
| 65 |
try:
|
| 66 |
result = summarizer(
|
| 67 |
text,
|
|
@@ -73,21 +73,22 @@ def summarize(input_text, file_path, detail_level):
|
|
| 73 |
do_sample=False,
|
| 74 |
truncation=True
|
| 75 |
)
|
|
|
|
| 76 |
return result[0]['summary_text']
|
| 77 |
except Exception as e:
|
| 78 |
-
return f"Error
|
| 79 |
|
| 80 |
-
# Interface
|
| 81 |
interface = gr.Interface(
|
| 82 |
fn=summarize,
|
| 83 |
inputs=[
|
| 84 |
-
gr.Textbox(lines=12, placeholder="Paste
|
| 85 |
gr.File(file_types=[".pdf", ".docx", ".txt"], label="Upload Lecture File"),
|
| 86 |
-
gr.Slider(0.15, 0.60, value=0.32, step=0.01, label="Detail Level (higher = longer
|
| 87 |
],
|
| 88 |
outputs=gr.Textbox(label="Generated Summary"),
|
| 89 |
title="Lecture Summarizer",
|
| 90 |
-
description="
|
| 91 |
flagging_mode="never",
|
| 92 |
)
|
| 93 |
|
|
|
|
| 3 |
import torch
|
| 4 |
import pdfplumber
|
| 5 |
from docx import Document
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# Load faster model for CPU
|
| 9 |
device = 0 if torch.cuda.is_available() else -1
|
| 10 |
print(f"Using device: {'GPU' if device == 0 else 'CPU'}")
|
| 11 |
|
| 12 |
summarizer = pipeline(
|
| 13 |
"summarization",
|
| 14 |
+
model="Falconsai/text_summarization", # Faster/smaller for quick tests
|
| 15 |
device=device
|
| 16 |
)
|
| 17 |
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
return f"Error reading file: {str(e)}"
|
| 37 |
|
| 38 |
+
def summarize(input_text, file_path, detail_level, progress=gr.Progress()):
|
| 39 |
+
progress(0, desc="Preparing text...")
|
| 40 |
+
|
| 41 |
if file_path is not None:
|
| 42 |
text = extract_text(file_path)
|
| 43 |
else:
|
|
|
|
| 53 |
target_ratio = detail_level
|
| 54 |
target_length = int(words * target_ratio)
|
| 55 |
|
| 56 |
+
# Lower caps for speed and to avoid warnings
|
| 57 |
+
max_l = max(200, min(512, target_length + 100))
|
| 58 |
+
min_l = max(50, int(target_length * 0.65))
|
| 59 |
|
|
|
|
| 60 |
if min_l >= max_l:
|
| 61 |
+
min_l = max_l // 2
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
progress(0.4, desc="Summarizing... (10–60 sec for long text)")
|
| 64 |
+
|
| 65 |
try:
|
| 66 |
result = summarizer(
|
| 67 |
text,
|
|
|
|
| 73 |
do_sample=False,
|
| 74 |
truncation=True
|
| 75 |
)
|
| 76 |
+
progress(1.0, desc="Done!")
|
| 77 |
return result[0]['summary_text']
|
| 78 |
except Exception as e:
|
| 79 |
+
return f"Error: {str(e)}\n(Try shorter input or lower detail.)"
|
| 80 |
|
| 81 |
+
# Interface with progress
|
| 82 |
interface = gr.Interface(
|
| 83 |
fn=summarize,
|
| 84 |
inputs=[
|
| 85 |
+
gr.Textbox(lines=12, placeholder="Paste lecture text...", label="Lecture Text (Paste)"),
|
| 86 |
gr.File(file_types=[".pdf", ".docx", ".txt"], label="Upload Lecture File"),
|
| 87 |
+
gr.Slider(0.15, 0.60, value=0.32, step=0.01, label="Detail Level (higher = longer)")
|
| 88 |
],
|
| 89 |
outputs=gr.Textbox(label="Generated Summary"),
|
| 90 |
title="Lecture Summarizer",
|
| 91 |
+
description="Paste or upload lecture. Progress shows during generation. For long files, lower detail or upgrade to GPU.",
|
| 92 |
flagging_mode="never",
|
| 93 |
)
|
| 94 |
|