Spaces:
Sleeping
Sleeping
Update models/summary_models.py
Browse files- models/summary_models.py +270 -249
models/summary_models.py
CHANGED
|
@@ -1,249 +1,270 @@
|
|
| 1 |
-
# models/summary_models.py
|
| 2 |
-
import logging
|
| 3 |
-
from typing import Dict, List, Optional, Tuple, Union, Any
|
| 4 |
-
import torch
|
| 5 |
-
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 6 |
-
|
| 7 |
-
class SummaryModelManager:
|
| 8 |
-
def __init__(self, token_manager=None, cache_manager=None, metrics_calculator=None):
|
| 9 |
-
"""Initialize the SummaryModelManager with optional utilities."""
|
| 10 |
-
self.logger = logging.getLogger(__name__)
|
| 11 |
-
self.token_manager = token_manager
|
| 12 |
-
self.cache_manager = cache_manager
|
| 13 |
-
self.metrics_calculator = metrics_calculator
|
| 14 |
-
|
| 15 |
-
# Model instance
|
| 16 |
-
self.model = None
|
| 17 |
-
self.tokenizer = None
|
| 18 |
-
|
| 19 |
-
# Model name
|
| 20 |
-
self.model_name = "t5-small"
|
| 21 |
-
|
| 22 |
-
# Track initialization state
|
| 23 |
-
self.initialized = False
|
| 24 |
-
|
| 25 |
-
# Default generation parameters
|
| 26 |
-
self.default_params = {
|
| 27 |
-
"max_length": 150,
|
| 28 |
-
"min_length": 40,
|
| 29 |
-
"length_penalty": 2.0,
|
| 30 |
-
"num_beams": 4,
|
| 31 |
-
"early_stopping": True
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
def initialize_model(self):
|
| 35 |
-
"""Initialize the summarization model."""
|
| 36 |
-
if self.initialized:
|
| 37 |
-
return
|
| 38 |
-
|
| 39 |
-
try:
|
| 40 |
-
# Register with token manager if available
|
| 41 |
-
if self.token_manager:
|
| 42 |
-
self.token_manager.register_model(
|
| 43 |
-
self.model_name, "summarization")
|
| 44 |
-
|
| 45 |
-
# Load model and tokenizer
|
| 46 |
-
self.logger.info(f"Loading summary model: {self.model_name}")
|
| 47 |
-
self.tokenizer = T5Tokenizer.from_pretrained(self.model_name)
|
| 48 |
-
self.model = T5ForConditionalGeneration.from_pretrained(self.model_name)
|
| 49 |
-
|
| 50 |
-
self.initialized = True
|
| 51 |
-
self.logger.info("Summary model initialized successfully")
|
| 52 |
-
|
| 53 |
-
except Exception as e:
|
| 54 |
-
self.logger.error(f"Failed to initialize summary model: {e}")
|
| 55 |
-
raise
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
#
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
#
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
"
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# models/summary_models.py
|
| 2 |
+
import logging
|
| 3 |
+
from typing import Dict, List, Optional, Tuple, Union, Any
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 6 |
+
|
| 7 |
+
class SummaryModelManager:
|
| 8 |
+
def __init__(self, token_manager=None, cache_manager=None, metrics_calculator=None):
|
| 9 |
+
"""Initialize the SummaryModelManager with optional utilities."""
|
| 10 |
+
self.logger = logging.getLogger(__name__)
|
| 11 |
+
self.token_manager = token_manager
|
| 12 |
+
self.cache_manager = cache_manager
|
| 13 |
+
self.metrics_calculator = metrics_calculator
|
| 14 |
+
|
| 15 |
+
# Model instance
|
| 16 |
+
self.model = None
|
| 17 |
+
self.tokenizer = None
|
| 18 |
+
|
| 19 |
+
# Model name
|
| 20 |
+
self.model_name = "t5-small"
|
| 21 |
+
|
| 22 |
+
# Track initialization state
|
| 23 |
+
self.initialized = False
|
| 24 |
+
|
| 25 |
+
# Default generation parameters
|
| 26 |
+
self.default_params = {
|
| 27 |
+
"max_length": 150,
|
| 28 |
+
"min_length": 40,
|
| 29 |
+
"length_penalty": 2.0,
|
| 30 |
+
"num_beams": 4,
|
| 31 |
+
"early_stopping": True
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
def initialize_model(self):
|
| 35 |
+
"""Initialize the summarization model."""
|
| 36 |
+
if self.initialized:
|
| 37 |
+
return
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
# Register with token manager if available
|
| 41 |
+
if self.token_manager:
|
| 42 |
+
self.token_manager.register_model(
|
| 43 |
+
self.model_name, "summarization")
|
| 44 |
+
|
| 45 |
+
# Load model and tokenizer
|
| 46 |
+
self.logger.info(f"Loading summary model: {self.model_name}")
|
| 47 |
+
self.tokenizer = T5Tokenizer.from_pretrained(self.model_name)
|
| 48 |
+
self.model = T5ForConditionalGeneration.from_pretrained(self.model_name)
|
| 49 |
+
|
| 50 |
+
self.initialized = True
|
| 51 |
+
self.logger.info("Summary model initialized successfully")
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
#self.logger.error(f"Failed to initialize summary model: {e}")
|
| 55 |
+
#raise
|
| 56 |
+
# Try a fallback model that doesn't require SentencePiece
|
| 57 |
+
try:
|
| 58 |
+
fallback_model = "facebook/bart-base"
|
| 59 |
+
self.logger.info(f"Trying fallback model: {fallback_model}")
|
| 60 |
+
|
| 61 |
+
from transformers import BartTokenizer, BartForConditionalGeneration
|
| 62 |
+
|
| 63 |
+
self.tokenizer = BartTokenizer.from_pretrained(fallback_model)
|
| 64 |
+
self.model = BartForConditionalGeneration.from_pretrained(fallback_model)
|
| 65 |
+
self.model_name = fallback_model
|
| 66 |
+
|
| 67 |
+
# Register fallback with token manager
|
| 68 |
+
if self.token_manager:
|
| 69 |
+
self.token_manager.register_model(
|
| 70 |
+
self.model_name, "summarization")
|
| 71 |
+
|
| 72 |
+
self.initialized = True
|
| 73 |
+
self.logger.info("Fallback summary model initialized successfully")
|
| 74 |
+
except Exception as fallback_error:
|
| 75 |
+
self.logger.error(f"Failed to initialize fallback model: {fallback_error}")
|
| 76 |
+
raise
|
| 77 |
+
|
| 78 |
+
def generate_summary(self, text: str, prefix: str = "summarize: ",
|
| 79 |
+
agent_name: str = "report_generation",
|
| 80 |
+
params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
| 81 |
+
"""
|
| 82 |
+
Generate a summary of the given text.
|
| 83 |
+
Returns the summary and metadata.
|
| 84 |
+
"""
|
| 85 |
+
# Initialize model if needed
|
| 86 |
+
if not self.initialized:
|
| 87 |
+
self.initialize_model()
|
| 88 |
+
|
| 89 |
+
# Prepare input text
|
| 90 |
+
input_text = f"{prefix}{text}"
|
| 91 |
+
|
| 92 |
+
# Check cache if available
|
| 93 |
+
if self.cache_manager:
|
| 94 |
+
cache_key = input_text[:100] + str(hash(input_text)) # Use prefix of text + hash as key
|
| 95 |
+
cache_hit, cached_result = self.cache_manager.get(
|
| 96 |
+
cache_key, namespace="summaries")
|
| 97 |
+
|
| 98 |
+
if cache_hit:
|
| 99 |
+
# Update metrics if available
|
| 100 |
+
if self.metrics_calculator:
|
| 101 |
+
self.metrics_calculator.update_cache_metrics(1, 0, 0.005) # Estimated energy saving
|
| 102 |
+
return cached_result
|
| 103 |
+
|
| 104 |
+
# Request token budget if available
|
| 105 |
+
if self.token_manager:
|
| 106 |
+
approved, reason = self.token_manager.request_tokens(
|
| 107 |
+
agent_name, "summarization", input_text, self.model_name)
|
| 108 |
+
|
| 109 |
+
if not approved:
|
| 110 |
+
self.logger.warning(f"Token budget exceeded: {reason}")
|
| 111 |
+
return {"summary": "Token budget exceeded", "error": reason}
|
| 112 |
+
|
| 113 |
+
# Tokenize
|
| 114 |
+
inputs = self.tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
|
| 115 |
+
|
| 116 |
+
# Merge default and custom parameters
|
| 117 |
+
generation_params = self.default_params.copy()
|
| 118 |
+
if params:
|
| 119 |
+
generation_params.update(params)
|
| 120 |
+
|
| 121 |
+
# Generate summary
|
| 122 |
+
with torch.no_grad():
|
| 123 |
+
output_ids = self.model.generate(
|
| 124 |
+
inputs.input_ids,
|
| 125 |
+
**generation_params
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Decode summary
|
| 129 |
+
summary = self.tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 130 |
+
|
| 131 |
+
# Calculate compression ratio
|
| 132 |
+
input_length = len(text.split())
|
| 133 |
+
summary_length = len(summary.split())
|
| 134 |
+
compression_ratio = input_length / max(summary_length, 1)
|
| 135 |
+
|
| 136 |
+
# Prepare result
|
| 137 |
+
result = {
|
| 138 |
+
"summary": summary,
|
| 139 |
+
"input_length": input_length,
|
| 140 |
+
"summary_length": summary_length,
|
| 141 |
+
"compression_ratio": compression_ratio
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
# Log token usage if available
|
| 145 |
+
if self.token_manager:
|
| 146 |
+
input_tokens = len(inputs.input_ids[0])
|
| 147 |
+
output_tokens = len(output_ids[0])
|
| 148 |
+
total_tokens = input_tokens + output_tokens
|
| 149 |
+
|
| 150 |
+
self.token_manager.log_usage(
|
| 151 |
+
agent_name, "summarization", total_tokens, self.model_name)
|
| 152 |
+
|
| 153 |
+
# Log energy usage if metrics calculator is available
|
| 154 |
+
if self.metrics_calculator:
|
| 155 |
+
energy_usage = self.token_manager.calculate_energy_usage(
|
| 156 |
+
total_tokens, self.model_name)
|
| 157 |
+
self.metrics_calculator.log_energy_usage(
|
| 158 |
+
energy_usage, self.model_name, agent_name, "summarization")
|
| 159 |
+
|
| 160 |
+
# Store in cache if available
|
| 161 |
+
if self.cache_manager:
|
| 162 |
+
self.cache_manager.put(cache_key, result, namespace="summaries")
|
| 163 |
+
|
| 164 |
+
return result
|
| 165 |
+
|
| 166 |
+
def generate_executive_summary(self, detailed_content: str, confidence_level: float,
|
| 167 |
+
agent_name: str = "report_generation") -> Dict[str, Any]:
|
| 168 |
+
"""
|
| 169 |
+
Generate an executive summary with confidence indication.
|
| 170 |
+
Adjusts detail level based on confidence.
|
| 171 |
+
"""
|
| 172 |
+
# Prepare prompt based on confidence
|
| 173 |
+
if confidence_level >= 0.7:
|
| 174 |
+
prefix = "summarize with high confidence: "
|
| 175 |
+
params = {"min_length": 30, "max_length": 100}
|
| 176 |
+
elif confidence_level >= 0.4:
|
| 177 |
+
prefix = "summarize with moderate confidence: "
|
| 178 |
+
params = {"min_length": 20, "max_length": 80}
|
| 179 |
+
else:
|
| 180 |
+
prefix = "summarize with low confidence: "
|
| 181 |
+
params = {"min_length": 15, "max_length": 60}
|
| 182 |
+
|
| 183 |
+
# Generate summary
|
| 184 |
+
result = self.generate_summary(detailed_content, prefix=prefix,
|
| 185 |
+
agent_name=agent_name, params=params)
|
| 186 |
+
|
| 187 |
+
# Add confidence level to result
|
| 188 |
+
result["confidence_level"] = confidence_level
|
| 189 |
+
|
| 190 |
+
# Add confidence statement
|
| 191 |
+
confidence_statement = self._generate_confidence_statement(confidence_level)
|
| 192 |
+
result["confidence_statement"] = confidence_statement
|
| 193 |
+
|
| 194 |
+
return result
|
| 195 |
+
|
| 196 |
+
def _generate_confidence_statement(self, confidence_level: float) -> str:
|
| 197 |
+
"""Generate an appropriate confidence statement based on the level."""
|
| 198 |
+
if confidence_level >= 0.8:
|
| 199 |
+
return "This analysis is provided with high confidence based on strong evidence in the provided materials."
|
| 200 |
+
elif confidence_level >= 0.6:
|
| 201 |
+
return "This analysis is provided with good confidence based on substantial evidence in the provided materials."
|
| 202 |
+
elif confidence_level >= 0.4:
|
| 203 |
+
return "This analysis is provided with moderate confidence. Some aspects may require additional verification."
|
| 204 |
+
elif confidence_level >= 0.2:
|
| 205 |
+
return "This analysis is provided with limited confidence due to sparse relevant information in the provided materials."
|
| 206 |
+
else:
|
| 207 |
+
return "This analysis is provided with very low confidence due to insufficient relevant information in the provided materials."
|
| 208 |
+
|
| 209 |
+
def combine_analyses(self, text_analyses: List[Dict[str, Any]],
|
| 210 |
+
image_analyses: List[Dict[str, Any]],
|
| 211 |
+
topic: str, agent_name: str = "report_generation") -> Dict[str, Any]:
|
| 212 |
+
"""
|
| 213 |
+
Combine text and image analyses into a coherent report.
|
| 214 |
+
Returns the combined report with metadata.
|
| 215 |
+
"""
|
| 216 |
+
# Build combined content
|
| 217 |
+
combined_content = f"Topic: {topic}\n\n"
|
| 218 |
+
|
| 219 |
+
# Add text analyses
|
| 220 |
+
combined_content += "Text Analysis:\n"
|
| 221 |
+
for i, analysis in enumerate(text_analyses):
|
| 222 |
+
if "error" in analysis:
|
| 223 |
+
continue
|
| 224 |
+
combined_content += f"- Document {i+1}: {analysis.get('summary', 'No summary available')}\n"
|
| 225 |
+
|
| 226 |
+
# Add image analyses
|
| 227 |
+
combined_content += "\nImage Analysis:\n"
|
| 228 |
+
for i, analysis in enumerate(image_analyses):
|
| 229 |
+
if "error" in analysis:
|
| 230 |
+
continue
|
| 231 |
+
combined_content += f"- Image {i+1}: {analysis.get('caption', 'No caption available')}\n"
|
| 232 |
+
|
| 233 |
+
# Calculate overall confidence based on analyses
|
| 234 |
+
text_confidence = sum(a.get("confidence", 0) for a in text_analyses) / max(len(text_analyses), 1)
|
| 235 |
+
image_confidence = sum(a.get("confidence", 0) for a in image_analyses) / max(len(image_analyses), 1)
|
| 236 |
+
|
| 237 |
+
# Weight confidence (text analyses typically more important for deep dives)
|
| 238 |
+
overall_confidence = 0.7 * text_confidence + 0.3 * image_confidence
|
| 239 |
+
|
| 240 |
+
# Generate detailed report
|
| 241 |
+
detailed_report = self.generate_summary(
|
| 242 |
+
combined_content,
|
| 243 |
+
prefix=f"generate detailed report about {topic}: ",
|
| 244 |
+
agent_name=agent_name,
|
| 245 |
+
params={"max_length": 300, "min_length": 100}
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
# Generate executive summary
|
| 249 |
+
executive_summary = self.generate_executive_summary(
|
| 250 |
+
detailed_report["summary"],
|
| 251 |
+
overall_confidence,
|
| 252 |
+
agent_name
|
| 253 |
+
)
|
| 254 |
+
|
| 255 |
+
# Combine results
|
| 256 |
+
result = {
|
| 257 |
+
"topic": topic,
|
| 258 |
+
"executive_summary": executive_summary["summary"],
|
| 259 |
+
"confidence_statement": executive_summary["confidence_statement"],
|
| 260 |
+
"detailed_report": detailed_report["summary"],
|
| 261 |
+
"confidence_level": overall_confidence,
|
| 262 |
+
"text_confidence": text_confidence,
|
| 263 |
+
"image_confidence": image_confidence,
|
| 264 |
+
"source_count": {
|
| 265 |
+
"text": len(text_analyses),
|
| 266 |
+
"images": len(image_analyses)
|
| 267 |
+
}
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
return result
|