Instructions to use Newvel/story_summarization_model_cnn with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Newvel/story_summarization_model_cnn with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSeq2SeqLM tokenizer = AutoTokenizer.from_pretrained("Newvel/story_summarization_model_cnn") model = AutoModelForSeq2SeqLM.from_pretrained("Newvel/story_summarization_model_cnn", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from Newvel/story_summarization_model_cnn: direct link, hf CLI and curl.
- Browser
- Download file 1.44 kB
-
https://huggingface.co/Newvel/story_summarization_model_cnn/resolve/main/handler.py
- Command line
-
hf download hf://Newvel/story_summarization_model_cnn/handler.py
-
curl -L -o handler.py https://huggingface.co/Newvel/story_summarization_model_cnn/resolve/main/handler.py
1.44 kB
| import json | |
| from transformers import pipeline | |
| class EndpointHandler: | |
| def __init__(self, model_dir): | |
| """ | |
| Initialize the handler with optimized settings for low-resource environments. | |
| Args: | |
| model_dir (str): The directory where the fine-tuned model is stored. | |
| """ | |
| print("Loading summarization pipeline...") | |
| self.summarizer = pipeline( | |
| "summarization", | |
| model=model_dir, | |
| device_map='cpu', | |
| max_length=200, | |
| min_length=60 | |
| ) | |
| def __call__(self, inputs): | |
| try: | |
| # If inputs is already a string, try to parse it | |
| if isinstance(inputs, str): | |
| try: | |
| input_data = json.loads(inputs) | |
| except json.JSONDecodeError: | |
| input_data = {"inputs": inputs} | |
| elif isinstance(inputs, dict): | |
| input_data = inputs | |
| else: | |
| raise ValueError("Input must be a string or dictionary") | |
| input_text = input_data.get("inputs", "") | |
| if not input_text: | |
| return json.dumps({"error": "No input text provided."}) | |
| # Generate the summary | |
| summary = self.summarizer(input_text)[0]["summary_text"] | |
| return json.dumps({"summary": summary}) | |
| except Exception as e: | |
| return json.dumps({"error": str(e)}) |