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