Spaces:
Sleeping
Sleeping
Download summarizer.py from tbels89/Prometheus: direct link, hf CLI and curl.
- Browser
- Download file 2.85 kB
-
https://huggingface.co/spaces/tbels89/Prometheus/resolve/main/summarizer.py
- Command line
-
hf download hf://spaces/tbels89/Prometheus/summarizer.py
-
curl -L -o summarizer.py https://huggingface.co/spaces/tbels89/Prometheus/resolve/main/summarizer.py
2.85 kB
| # summarizer.py | |
| import streamlit as st | |
| from transformers import pipeline | |
| from logging_config import logger | |
| def load_summarizer(): | |
| """ | |
| Load and cache the summarization pipeline. | |
| Designed for a CPU-only environment and uses Hugging Face-hosted models. | |
| """ | |
| try: | |
| logger.info("Loading summarizer model on CPU...") | |
| # Initialize summarization pipeline on CPU | |
| summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=-1) | |
| logger.info("Summarizer model loaded successfully.") | |
| return summarizer | |
| except Exception as e: | |
| logger.error(f"Failed to load summarizer pipeline: {e}") | |
| st.error("The summarization model could not be loaded. Please try again later.") | |
| return None | |
| def truncate_and_summarize(messages, max_length=2500, min_length=250, recent_messages=20): | |
| """ | |
| If conversation too long, summarize older messages. | |
| Args: | |
| messages (list): List of message dictionaries. | |
| max_length (int): Maximum length for the summary. | |
| min_length (int): Minimum length for the summary. | |
| recent_messages (int): Number of recent messages to keep intact. | |
| Returns: | |
| list: Updated list of messages with older ones summarized. | |
| """ | |
| logger.info(f"Checking if summarization is needed. Total messages: {len(messages)}") | |
| if len(messages) <= recent_messages: | |
| logger.info("No summarization needed; message count within limit.") | |
| return messages | |
| # Split older and recent messages | |
| older = messages[:-recent_messages] | |
| recent = messages[-recent_messages:] | |
| # Combine older messages | |
| conversation_text = "\n".join([f"{m['role']}: {m['content']}" for m in older]) | |
| summary_input = ("Summarize the following conversation concisely to act as assistant's memory:\n\n" | |
| + conversation_text) | |
| summarizer = load_summarizer() | |
| if summarizer: | |
| try: | |
| # Perform summarization | |
| logger.info("Performing summarization of older messages.") | |
| summary_result = summarizer(summary_input, max_length=max_length, min_length=min_length, do_sample=False) | |
| summary_text = summary_result[0]['summary_text'].strip() | |
| logger.info("Successfully summarized older conversation.") | |
| # Prepend the summarized message | |
| summarized = [{"role": "system", "content": "Summary of previous conversation: " + summary_text}] | |
| return summarized + recent | |
| except Exception as e: | |
| logger.error(f"Error during summarization: {e}") | |
| logger.warning("Returning messages without summarization.") | |
| logger.warning("Summarizer not available. Returning messages without summarization.") | |
| return messages |