Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,40 +2,46 @@ import gradio as gr
|
|
| 2 |
import fitz
|
| 3 |
import torch
|
| 4 |
from transformers import pipeline
|
| 5 |
-
import time
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def summarize_file(file_bytes):
|
| 13 |
start = time.time()
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
if not text.strip(): return "β No text found"
|
| 16 |
text = text[:300000]
|
| 17 |
-
chunks = [text[i:i+
|
| 18 |
if not chunks: return "β No chunks to summarize"
|
| 19 |
summaries = []
|
| 20 |
-
|
| 21 |
-
for i in range(0, len(chunks), batch_size):
|
| 22 |
if time.time() - start > 9:
|
| 23 |
summaries.append("β οΈ Stopped early")
|
| 24 |
break
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
return f"**Chars**: {len(text)}\n**Time**: {time.time()-start:.2f}s\n\n" + "\n\n".join(summaries)
|
| 31 |
|
| 32 |
-
demo = gr.Interface(
|
| 33 |
-
fn=summarize_file, inputs=gr.File(label="π PDF/TXT Notes"),
|
| 34 |
-
outputs=gr.Textbox(label="π Summary"),
|
| 35 |
-
title="Fast Summarizer", description="300,000+ chars in ~5β8s (GPU) or ~12β22s (CPU)"
|
| 36 |
-
)
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
|
|
|
|
| 2 |
import fitz
|
| 3 |
import torch
|
| 4 |
from transformers import pipeline
|
| 5 |
+
import time, logging
|
| 6 |
|
| 7 |
+
logging.basicConfig(level=logging.ERROR)
|
| 8 |
+
device = -1 # Force CPU (no GPU detected)
|
| 9 |
+
print("β οΈ CPU-only. Expect ~15β25s for 300,000 chars.")
|
| 10 |
|
| 11 |
+
try:
|
| 12 |
+
summarizer = pipeline("summarization", model="t5-small", device=device, torch_dtype=torch.float32)
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print(f"β Model loading failed: {str(e)}")
|
| 15 |
+
exit(1)
|
| 16 |
|
| 17 |
def summarize_file(file_bytes):
|
| 18 |
start = time.time()
|
| 19 |
+
try:
|
| 20 |
+
text = "".join(page.get_text("text", flags=16) for page in fitz.open(stream=file_bytes, filetype="pdf")) if file_bytes[:4].startswith(b'%PDF') else file_bytes.decode("utf-8", errors="ignore")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return f"β Text extraction failed: {str(e)}"
|
| 23 |
if not text.strip(): return "β No text found"
|
| 24 |
text = text[:300000]
|
| 25 |
+
chunks = [text[i:i+10000] for i in range(0, len(text), 10000)]
|
| 26 |
if not chunks: return "β No chunks to summarize"
|
| 27 |
summaries = []
|
| 28 |
+
for i, chunk in enumerate(chunks):
|
|
|
|
| 29 |
if time.time() - start > 9:
|
| 30 |
summaries.append("β οΈ Stopped early")
|
| 31 |
break
|
| 32 |
try:
|
| 33 |
+
summary = summarizer(chunk, max_length=40, min_length=10, do_sample=False)[0]['summary_text']
|
| 34 |
+
summaries.append(f"**Chunk {i+1}**:\n{summary}")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
summaries.append(f"**Chunk {i+1}**: β Error: {str(e)}")
|
| 37 |
return f"**Chars**: {len(text)}\n**Time**: {time.time()-start:.2f}s\n\n" + "\n\n".join(summaries)
|
| 38 |
|
| 39 |
+
demo = gr.Interface(fn=summarize_file, inputs=gr.File(label="π PDF/TXT Notes"), outputs=gr.Textbox(label="π Summary"), title="Fast Summarizer", description="300,000+ chars in ~15β25s (CPU)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
| 42 |
+
try:
|
| 43 |
+
demo.launch(share=False, server_port=7860)
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"β Gradio launch failed: {str(e)}")
|
| 46 |
|
| 47 |
|