sagar008 commited on
Commit
ac4f231
Β·
verified Β·
1 Parent(s): 93a0659

Update summarizer.py

Browse files
Files changed (1) hide show
  1. summarizer.py +29 -108
summarizer.py CHANGED
@@ -5,119 +5,37 @@ import os
5
 
6
  class DocumentSummarizer:
7
  def __init__(self):
8
- self.bart_pipeline = None
9
- self.legal_pipeline = None
10
  self.tokenizer = None
 
11
 
12
  async def initialize(self):
13
- """Initialize both summarization models"""
14
- print(" Loading BART summarizer...")
15
- start_time = time.time()
16
-
17
- # Initialize reliable BART model first
18
- self.bart_pipeline = pipeline("summarization", model="facebook/bart-large-cnn")
19
- self.tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
20
- print(f" BART model loaded in {time.time() - start_time:.2f}s")
21
-
22
- # Try to load legal model
23
- print(" Loading legal summarizer...")
24
- hf_token = os.getenv("HF_TOKEN")
25
- try:
26
- legal_start = time.time()
27
- self.legal_pipeline = pipeline(
28
- "summarization",
29
- model="VincentMuriuki/legal-summarizer",
30
- token=hf_token
31
- )
32
- print(f" Legal model loaded in {time.time() - legal_start:.2f}s")
33
- except Exception as e:
34
- print(f"⚠️ Legal model failed, using BART only: {e}")
35
- self.legal_pipeline = None
36
 
37
  async def batch_summarize(self, chunks: List[str]) -> Dict[str, Any]:
38
- """Choose strategy based on available models"""
39
- if self.legal_pipeline:
40
- return await self.hybrid_summarize(chunks)
41
- else:
42
- return await self.bart_only_summarize(chunks)
43
-
44
- async def hybrid_summarize(self, chunks: List[str]) -> Dict[str, Any]:
45
- """Two-stage summarization: BART β†’ Legal-specific"""
46
  if not chunks:
47
  return {"actual_summary": "", "short_summary": ""}
48
 
49
- print(f"Stage 1: Initial summarization with BART ({len(chunks)} chunks)...")
50
- stage1_start = time.time()
51
-
52
- # Stage 1: Facebook BART for clean, reliable summarization
53
- initial_summaries = self.bart_pipeline(
54
- chunks,
55
- max_length=150, # Slightly longer for more detail
56
- min_length=30,
57
- do_sample=False,
58
- num_beams=2,
59
- truncation=True
60
- )
61
-
62
- initial_summary = " ".join([s["summary_text"] for s in initial_summaries])
63
- stage1_time = time.time() - stage1_start
64
- print(f" Stage 1 completed in {stage1_time:.2f}s")
65
-
66
- # Stage 2: Vincent's legal model for domain refinement
67
- print(" Stage 2: Legal refinement with specialized model...")
68
- stage2_start = time.time()
69
-
70
- # Break the initial summary into smaller chunks if needed
71
- if len(initial_summary) > 3000:
72
- # Use simple chunking since we don't have chunker here
73
- words = initial_summary.split()
74
- refined_chunks = []
75
- chunk_size = 800 # words per chunk
76
- for i in range(0, len(words), chunk_size):
77
- chunk = " ".join(words[i:i + chunk_size])
78
- refined_chunks.append(chunk)
79
- else:
80
- refined_chunks = [initial_summary]
81
-
82
- final_summaries = self.legal_pipeline(
83
- refined_chunks,
84
- max_length=250,
85
- min_length=48,
86
- do_sample=False,
87
- num_beams=1,
88
- truncation=True
89
- )
90
-
91
- final_summary = " ".join([s["summary_text"] for s in final_summaries])
92
- stage2_time = time.time() - stage2_start
93
- print(f" Stage 2 completed in {stage2_time:.2f}s")
94
-
95
- total_time = stage1_time + stage2_time
96
-
97
- return {
98
- "actual_summary": final_summary,
99
- "short_summary": final_summary,
100
- "initial_bart_summary": initial_summary, # For comparison
101
- "processing_method": "hybrid_bart_to_legal",
102
- "time_taken": f"{total_time:.2f}s",
103
- "stage1_time": f"{stage1_time:.2f}s",
104
- "stage2_time": f"{stage2_time:.2f}s"
105
- }
106
-
107
- async def bart_only_summarize(self, chunks: List[str]) -> Dict[str, Any]:
108
- """Fallback to BART-only summarization"""
109
- if not chunks:
110
- return {"actual_summary": "", "short_summary": ""}
111
-
112
- print(f" BART-only summarization ({len(chunks)} chunks)...")
113
  start_time = time.time()
114
 
115
- outputs = self.bart_pipeline(
 
116
  chunks,
117
- max_length=128,
118
- min_length=24,
119
- do_sample=False,
120
- num_beams=2,
121
  truncation=True,
122
  )
123
 
@@ -127,17 +45,19 @@ class DocumentSummarizer:
127
  # Create short summary if combined is too long
128
  short_summary = combined_summary
129
  if len(combined_summary) > 2000:
130
- short_outputs = self.bart_pipeline(
 
131
  [combined_summary],
132
- max_length=96,
133
- min_length=16,
134
  do_sample=False,
135
- num_beams=1,
136
  truncation=True,
137
  )
138
  short_summary = short_outputs[0]["summary_text"]
139
 
140
  processing_time = time.time() - start_time
 
141
 
142
  return {
143
  "actual_summary": combined_summary,
@@ -150,12 +70,12 @@ class DocumentSummarizer:
150
  def summarize_texts_sync(self, texts: List[str], max_length: int, min_length: int) -> Dict[str, Any]:
151
  """Synchronous batch summarization for standalone endpoint"""
152
  start_time = time.time()
153
- outputs = self.bart_pipeline( # Use BART for reliability
154
  texts,
155
  max_length=max_length,
156
  min_length=min_length,
157
  do_sample=False,
158
- num_beams=1,
159
  truncation=True,
160
  )
161
  summaries = [output["summary_text"] for output in outputs]
@@ -165,3 +85,4 @@ class DocumentSummarizer:
165
  "time_taken": f"{time.time() - start_time:.2f}s"
166
  }
167
 
 
 
5
 
6
  class DocumentSummarizer:
7
  def __init__(self):
8
+ self.summarizer = None
 
9
  self.tokenizer = None
10
+ self.model_name = "facebook/bart-large-cnn"
11
 
12
  async def initialize(self):
13
+ """Initialize BART summarization pipeline only"""
14
+ if self.summarizer is None:
15
+ print(f"πŸ€– Loading BART summarization model: {self.model_name}")
16
+ start_time = time.time()
17
+
18
+ # No HF token needed for public BART model
19
+ self.summarizer = pipeline("summarization", model=self.model_name)
20
+ self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
21
+
22
+ print(f"βœ… BART model loaded in {time.time() - start_time:.2f}s")
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  async def batch_summarize(self, chunks: List[str]) -> Dict[str, Any]:
25
+ """BART-only batch summarization"""
 
 
 
 
 
 
 
26
  if not chunks:
27
  return {"actual_summary": "", "short_summary": ""}
28
 
29
+ print(f"πŸ“ BART summarizing {len(chunks)} chunks...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  start_time = time.time()
31
 
32
+ # Batch process all chunks at once
33
+ outputs = self.summarizer(
34
  chunks,
35
+ max_length=150, # Good detail level
36
+ min_length=30, # Avoid too short summaries
37
+ do_sample=False, # Deterministic output
38
+ num_beams=2, # Better quality than greedy
39
  truncation=True,
40
  )
41
 
 
45
  # Create short summary if combined is too long
46
  short_summary = combined_summary
47
  if len(combined_summary) > 2000:
48
+ print("πŸ“ Creating short summary...")
49
+ short_outputs = self.summarizer(
50
  [combined_summary],
51
+ max_length=128,
52
+ min_length=24,
53
  do_sample=False,
54
+ num_beams=2,
55
  truncation=True,
56
  )
57
  short_summary = short_outputs[0]["summary_text"]
58
 
59
  processing_time = time.time() - start_time
60
+ print(f"βœ… BART summarization completed in {processing_time:.2f}s")
61
 
62
  return {
63
  "actual_summary": combined_summary,
 
70
  def summarize_texts_sync(self, texts: List[str], max_length: int, min_length: int) -> Dict[str, Any]:
71
  """Synchronous batch summarization for standalone endpoint"""
72
  start_time = time.time()
73
+ outputs = self.summarizer(
74
  texts,
75
  max_length=max_length,
76
  min_length=min_length,
77
  do_sample=False,
78
+ num_beams=2,
79
  truncation=True,
80
  )
81
  summaries = [output["summary_text"] for output in outputs]
 
85
  "time_taken": f"{time.time() - start_time:.2f}s"
86
  }
87
 
88
+