Grok 4.1 Fast· xai
Frontier Reasoning Traces
Implement a Python script to build a text summarizer using transformers.
- User
Implement a Python script to build a text summarizer using transformers.
- Assistant
#!/usr/bin/env python3 """ Text Summarizer using Hugging Face Transformers. Requirements: pip install transformers torch accelerate Usage: python text_summarizer.py - Paste your text when prompted. - Or pipe text: echo "your long text here" | python text_summarizer.py """ import sys from transformers import pipeline def main(): # Load the pre-trained BART model for summarization (good for CNN/DailyMail style summaries) # You can swap with 't5-small' or 'google/pegasus-xsum' for different styles print("Loading summarization model... (This may take a moment on first run.)") summarizer = pipeline( "summarization", model="facebook/bart-large-cnn", device=-1 # Use CPU; set to 0 for GPU if available ) # Get input text if not sys.stdin.isatty(): # If piped input, read from stdin text = sys.stdin.read().strip() else: # Interactive mode print("\nEnter your text to summarize (Ctrl+D or empty line to finish):") lines = [] try: while True: line = input() if not line.strip(): break lines.append(line) except EOFError: pass text = ' '.join(lines).strip() if not text: print("No text provided. Exiting.") return print("\nGenerating summary...") try: # Generate summary with length constraints # Adjust max_length/min_length as needed (BART max tokens ~1024 input, ~142 output) summary = summarizer( text, max_length=130, min_length=30, do_sample=False, # Deterministic output truncation=True )[0]['summary_text'] print("\n" + "="*50) print("SUMMARY:") print(summary) print("="*50) print(f"\nOriginal length: {len(text.split())} words") print(f"Summary length: {len(summary.split())} words") except Exception as e: print(f"Error generating summary: {e}") print("Tip: Try shorter text or a lighter model like 'sshleifer/distilbart-cnn-12-6'.") if __name__ == "__main__": main()How to Use:
- Save the script as
text_summarizer.py. - Install dependencies:
pip install transformers torch accelerate - Run interactively:
python text_summarizer.py- Paste your text and press Enter twice (or Ctrl+D on Unix).
- Pipe long text:
orpython text_summarizer.py < article.txtecho "Your very long article here..." | python text_summarizer.py - Customization:
- Change
modelfor different results (e.g.,'google/pegasus-xsum'for abstractive XSum-style). - For very long texts (>1024 tokens), consider splitting into chunks (advanced extension).
- Change
This uses BART-large-CNN, a state-of-the-art model fine-tuned on news articles. It handles truncation automatically and produces high-quality summaries! 🚀
- Save the script as
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.