Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,11 @@ 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
|
|
@@ -20,7 +23,7 @@ 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]
|
| 24 |
|
| 25 |
fig, ax = plt.subplots(figsize=(10, 2.5))
|
| 26 |
ax.barh(labels, times, color=colors)
|
|
@@ -31,7 +34,8 @@ def visualize_chunk_status(chunk_data):
|
|
| 31 |
buf = io.BytesIO()
|
| 32 |
plt.savefig(buf, format='png')
|
| 33 |
buf.seek(0)
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
def summarize_file(file_bytes):
|
| 37 |
start = time.time()
|
|
@@ -56,7 +60,7 @@ def summarize_file(file_bytes):
|
|
| 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")
|
|
@@ -78,15 +82,15 @@ def summarize_file(file_bytes):
|
|
| 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 |
-
|
| 82 |
-
return final_summary,
|
| 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."
|
|
@@ -97,3 +101,4 @@ if __name__ == "__main__":
|
|
| 97 |
demo.launch(share=False, server_port=7860)
|
| 98 |
except Exception as e:
|
| 99 |
print(f"β Gradio launch failed: {str(e)}")
|
|
|
|
|
|
| 3 |
import torch
|
| 4 |
from transformers import pipeline
|
| 5 |
import time, logging, re
|
| 6 |
+
import matplotlib
|
| 7 |
+
matplotlib.use('Agg') # Use non-interactive backend for headless environments
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
import io
|
| 10 |
+
from PIL import Image
|
| 11 |
|
| 12 |
logging.basicConfig(level=logging.ERROR)
|
| 13 |
device = -1 # CPU-only
|
|
|
|
| 23 |
status_colors = {'summarized': 'green', 'skipped': 'orange', 'error': 'red'}
|
| 24 |
labels = [f"C{i['chunk']}" for i in chunk_data]
|
| 25 |
colors = [status_colors.get(i['status'], 'gray') for i in chunk_data]
|
| 26 |
+
times = [i.get('time', 0.1) for i in chunk_data]
|
| 27 |
|
| 28 |
fig, ax = plt.subplots(figsize=(10, 2.5))
|
| 29 |
ax.barh(labels, times, color=colors)
|
|
|
|
| 34 |
buf = io.BytesIO()
|
| 35 |
plt.savefig(buf, format='png')
|
| 36 |
buf.seek(0)
|
| 37 |
+
plt.close(fig) # Release memory
|
| 38 |
+
return Image.open(buf)
|
| 39 |
|
| 40 |
def summarize_file(file_bytes):
|
| 41 |
start = time.time()
|
|
|
|
| 60 |
|
| 61 |
for i, chunk in enumerate(chunks):
|
| 62 |
chunk_start = time.time()
|
| 63 |
+
chunk_result = {'chunk': i + 1, 'status': '', 'time': 0}
|
| 64 |
|
| 65 |
if time.time() - start > 20:
|
| 66 |
summaries.append("β οΈ Stopped early")
|
|
|
|
| 82 |
chunk_info.append(chunk_result)
|
| 83 |
|
| 84 |
final_summary = f"**Chars**: {len(text)}\n**Time**: {time.time()-start:.2f}s\n\n" + "\n\n".join(summaries)
|
| 85 |
+
image = visualize_chunk_status(chunk_info)
|
| 86 |
+
return final_summary, image
|
| 87 |
|
| 88 |
demo = gr.Interface(
|
| 89 |
fn=summarize_file,
|
| 90 |
inputs=gr.File(label="π Upload PDF", type="binary"),
|
| 91 |
outputs=[
|
| 92 |
gr.Textbox(label="π Summarized Output"),
|
| 93 |
+
gr.Image(label="π Visual Process Flow", type="pil")
|
| 94 |
],
|
| 95 |
title="AI-Powered PDF Summarizer",
|
| 96 |
description="Summarizes long PDFs (up to 300,000 characters) and visualizes chunk-level automation status."
|
|
|
|
| 101 |
demo.launch(share=False, server_port=7860)
|
| 102 |
except Exception as e:
|
| 103 |
print(f"β Gradio launch failed: {str(e)}")
|
| 104 |
+
|