mypythonapp / hf_api.py
dkshah's picture
Upload 4 files
eb6064a verified
Raw History Blame Contribute Delete
1.12 kB
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()