Spaces:
Configuration error
Configuration error
Download hf_api.py from dkshah/mypythonapp: direct link, hf CLI and curl.
- Browser
- Download file 1.12 kB
-
https://huggingface.co/spaces/dkshah/mypythonapp/resolve/main/hf_api.py
- Command line
-
hf download hf://spaces/dkshah/mypythonapp/hf_api.py
-
curl -L -o hf_api.py https://huggingface.co/spaces/dkshah/mypythonapp/resolve/main/hf_api.py
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() | |