Spaces:
Configuration error
Configuration error
File size: 1,118 Bytes
eb6064a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | from typing import Optional
from huggingface_hub import InferenceClient
def hf_summarize(text: str, *, model: str, api_token: Optional[str] = None, max_new_tokens: int = 200) -> str:
"""
Call the Hugging Face Inference API for summarization.
Args:
text: Input text to summarize.
model: Hugging Face model repo id (e.g., 'facebook/bart-large-cnn').
api_token: Optional HF API token for higher rate limits / private models.
max_new_tokens: Maximum tokens to generate in the summary.
Returns:
The summarized text.
"""
client = InferenceClient(model=model, token=api_token)
# The library returns either a string or a dict depending on the task helper used.
# Using the summarization task helper yields a string.
result = client.summarization(text, max_new_tokens=max_new_tokens)
if isinstance(result, str):
return result.strip()
# Fallback if a different payload format is returned
if isinstance(result, dict) and 'summary_text' in result:
return str(result['summary_text']).strip()
return str(result).strip()
|