Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import fitz
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import time, logging, re
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import io
|
| 8 |
+
|
| 9 |
+
logging.basicConfig(level=logging.ERROR)
|
| 10 |
+
device = -1 # CPU-only
|
| 11 |
+
print("β οΈ CPU-only. Expect ~20β30s for 300,000 chars.")
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
summarizer = pipeline("summarization", model="t5-small", device=device, torch_dtype=torch.float32)
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"β Model loading failed: {str(e)}")
|
| 17 |
+
exit(1)
|
| 18 |
+
|
| 19 |
+
def visualize_chunk_status(chunk_data):
|
| 20 |
+
status_colors = {'summarized': 'green', 'skipped': 'orange', 'error': 'red'}
|
| 21 |
+
labels = [f"C{i['chunk']}" for i in chunk_data]
|
| 22 |
+
colors = [status_colors.get(i['status'], 'gray') for i in chunk_data]
|
| 23 |
+
times = [i.get('time', 0.1) for i in chunk_data] # Avoid zero-time bars
|
| 24 |
+
|
| 25 |
+
fig, ax = plt.subplots(figsize=(10, 2.5))
|
| 26 |
+
ax.barh(labels, times, color=colors)
|
| 27 |
+
ax.set_xlabel("Time (s)")
|
| 28 |
+
ax.set_title("π Chunk Processing Status")
|
| 29 |
+
plt.tight_layout()
|
| 30 |
+
|
| 31 |
+
buf = io.BytesIO()
|
| 32 |
+
plt.savefig(buf, format='png')
|
| 33 |
+
buf.seek(0)
|
| 34 |
+
return buf
|
| 35 |
+
|
| 36 |
+
def summarize_file(file_bytes):
|
| 37 |
+
start = time.time()
|
| 38 |
+
chunk_info = []
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
doc = fitz.open(stream=file_bytes, filetype="pdf")
|
| 42 |
+
text = "".join(page.get_text("text") for page in doc)
|
| 43 |
+
text = re.sub(r"\$\s*([^$]+)\s*\$", r"\1", text)
|
| 44 |
+
text = re.sub(r"\\cap", "intersection", text)
|
| 45 |
+
text = re.sub(r"\s+", " ", text).strip()
|
| 46 |
+
text = "".join(c for c in text if ord(c) < 128)
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"β Text extraction failed: {str(e)}", None
|
| 49 |
+
|
| 50 |
+
if not text.strip():
|
| 51 |
+
return "β No text found", None
|
| 52 |
+
|
| 53 |
+
text = text[:300000]
|
| 54 |
+
chunks = [text[i:i+2000] for i in range(0, len(text), 2000)]
|
| 55 |
+
summaries = []
|
| 56 |
+
|
| 57 |
+
for i, chunk in enumerate(chunks):
|
| 58 |
+
chunk_start = time.time()
|
| 59 |
+
chunk_result = {'chunk': i+1, 'status': '', 'time': 0}
|
| 60 |
+
|
| 61 |
+
if time.time() - start > 20:
|
| 62 |
+
summaries.append("β οΈ Stopped early")
|
| 63 |
+
break
|
| 64 |
+
|
| 65 |
+
if sum(1 for c in chunk if not c.isalnum()) / len(chunk) > 0.5:
|
| 66 |
+
summaries.append(f"**Chunk {i+1}**: Skipped (equation-heavy)")
|
| 67 |
+
chunk_result['status'] = 'skipped'
|
| 68 |
+
else:
|
| 69 |
+
try:
|
| 70 |
+
summary = summarizer(chunk, max_length=60, min_length=10, do_sample=False)[0]['summary_text']
|
| 71 |
+
summaries.append(f"**Chunk {i+1}**:\n{summary}")
|
| 72 |
+
chunk_result['status'] = 'summarized'
|
| 73 |
+
except Exception as e:
|
| 74 |
+
summaries.append(f"**Chunk {i+1}**: β Error: {str(e)}")
|
| 75 |
+
chunk_result['status'] = 'error'
|
| 76 |
+
|
| 77 |
+
chunk_result['time'] = time.time() - chunk_start
|
| 78 |
+
chunk_info.append(chunk_result)
|
| 79 |
+
|
| 80 |
+
final_summary = f"**Chars**: {len(text)}\n**Time**: {time.time()-start:.2f}s\n\n" + "\n\n".join(summaries)
|
| 81 |
+
image_buf = visualize_chunk_status(chunk_info)
|
| 82 |
+
return final_summary, image_buf
|
| 83 |
+
|
| 84 |
+
demo = gr.Interface(
|
| 85 |
+
fn=summarize_file,
|
| 86 |
+
inputs=gr.File(label="π Upload PDF", type="binary"),
|
| 87 |
+
outputs=[
|
| 88 |
+
gr.Textbox(label="π Summarized Output"),
|
| 89 |
+
gr.Image(label="π Visual Process Flow")
|
| 90 |
+
],
|
| 91 |
+
title="AI-Powered PDF Summarizer",
|
| 92 |
+
description="Summarizes long PDFs (up to 300,000 characters) and visualizes chunk-level automation status."
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
try:
|
| 97 |
+
demo.launch(share=False, server_port=7860)
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(f"β Gradio launch failed: {str(e)}")
|