| from transformers import BartTokenizer | |
| import torch | |
| import streamlit as st | |
| import pickle | |
| class Summarizer: | |
| def __init__(self): | |
| self.model = None | |
| self.tokenizer = None | |
| def load_model(self): | |
| try: | |
| self.tokenizer = BartTokenizer.from_pretrained('facebook/bart-base') | |
| with open('bart_ami_finetuned.pkl', 'rb') as f: | |
| self.model = pickle.load(f) | |
| return self.model | |
| except Exception as e: | |
| st.error(f"Error loading summarization model: {str(e)}") | |
| return None | |
| def process(self, text: str, max_length: int = 130, min_length: int = 30): | |
| try: | |
| inputs = self.tokenizer(text, return_tensors="pt", max_length=1024, truncation=True) | |
| summary_ids = self.model.generate(inputs["input_ids"], max_length=max_length, min_length=min_length) | |
| return self.tokenizer.decode(summary_ids[0], skip_special_tokens=True) | |
| except Exception as e: | |
| st.error(f"Error in summarization: {str(e)}") | |
| return None |