Update src/models/summarization.py
Browse files- src/models/summarization.py +26 -17
src/models/summarization.py
CHANGED
@@ -1,35 +1,44 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
2 |
import torch
|
3 |
import streamlit as st
|
4 |
|
5 |
class Summarizer:
|
6 |
def __init__(self):
|
|
|
7 |
self.model = None
|
8 |
-
self.tokenizer = None
|
9 |
|
10 |
def load_model(self):
|
|
|
11 |
try:
|
12 |
-
self.
|
13 |
-
|
14 |
-
|
|
|
|
|
15 |
return self.model
|
16 |
except Exception as e:
|
17 |
st.error(f"Error loading summarization model: {str(e)}")
|
18 |
return None
|
19 |
|
20 |
-
def process(self, text: str, max_length: int =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
try:
|
22 |
-
|
23 |
-
inputs = {key: value.to(self.model.device) for key, value in inputs.items()}
|
24 |
-
summary_ids = self.model.generate(
|
25 |
-
inputs["input_ids"],
|
26 |
-
max_length=max_length,
|
27 |
-
min_length=min_length,
|
28 |
-
num_beams=4,
|
29 |
-
length_penalty=2.0
|
30 |
-
)
|
31 |
-
summary = self.tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
32 |
return summary
|
33 |
except Exception as e:
|
34 |
st.error(f"Error in summarization: {str(e)}")
|
35 |
-
return None
|
|
|
1 |
+
"""
|
2 |
+
Summarization Model Handler
|
3 |
+
Manages the BART model for text summarization.
|
4 |
+
"""
|
5 |
+
|
6 |
+
from transformers import pipeline
|
7 |
import torch
|
8 |
import streamlit as st
|
9 |
|
10 |
class Summarizer:
|
11 |
def __init__(self):
|
12 |
+
"""Initialize the summarization model."""
|
13 |
self.model = None
|
|
|
14 |
|
15 |
def load_model(self):
|
16 |
+
"""Load the BART summarization model."""
|
17 |
try:
|
18 |
+
self.model = pipeline(
|
19 |
+
"summarization",
|
20 |
+
model="facebook/bart-large-cnn",
|
21 |
+
device=0 if torch.cuda.is_available() else -1
|
22 |
+
)
|
23 |
return self.model
|
24 |
except Exception as e:
|
25 |
st.error(f"Error loading summarization model: {str(e)}")
|
26 |
return None
|
27 |
|
28 |
+
def process(self, text: str, max_length: int = 130, min_length: int = 30):
|
29 |
+
"""Process text for summarization.
|
30 |
+
|
31 |
+
Args:
|
32 |
+
text (str): Text to summarize
|
33 |
+
max_length (int): Maximum length of summary
|
34 |
+
min_length (int): Minimum length of summary
|
35 |
+
|
36 |
+
Returns:
|
37 |
+
str: Summarized text
|
38 |
+
"""
|
39 |
try:
|
40 |
+
summary = self.model(text, max_length=max_length, min_length=min_length)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
return summary
|
42 |
except Exception as e:
|
43 |
st.error(f"Error in summarization: {str(e)}")
|
44 |
+
return None
|