tejovanth commited on
Commit
0a3441b
Β·
verified Β·
1 Parent(s): a987bd6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -6
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] # Avoid zero-time bars
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
- return buf
 
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
- 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."
@@ -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
+