Skip to content

Instantly share code, notes, and snippets.

@pszemraj
Created December 6, 2023 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pszemraj/08f527380ed00ef2f2169e220341c489 to your computer and use it in GitHub Desktop.
Save pszemraj/08f527380ed00ef2f2169e220341c489 to your computer and use it in GitHub Desktop.
test out synthsumm summarization models via the free inference api
import os
import time
import requests
class Timer:
"""Basic timer utility."""
def __enter__(self):
self.start_time = time.perf_counter()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.end_time = time.perf_counter()
self.elapsed_time = self.end_time - self.start_time
print(f"Elapsed time: {self.elapsed_time:.4f} seconds")
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
API_URL = "https://api-inference.huggingface.co/models/pszemraj/long-t5-tglobal-base-synthsumm_direct"
# API_URL = "https://api-inference.huggingface.co/models/pszemraj/pegasus-x-large-book_synthsumm"
print(f"HF API Endpoint:\t{API_URL}")
# Retrieve API token from environment variable
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
raise ValueError("No HF_TOKEN found in environment variables")
headers = {"Authorization": f"Bearer {hf_token}"}
# headers = None # if you have no token
input_text = """To be fair, you have to have a very high IQ to understand Rick and Morty. The humour is extremely subtle, and without a solid grasp of theoretical physics most of the jokes will go over a typical viewer's head. There's also Rick's nihilistic outlook, which is deftly woven into his characterisation- his personal philosophy draws heavily from Narodnaya Volya literature, for instance. The fans understand this stuff; they have the intellectual capacity to truly appreciate the depths of these jokes, to realise that they're not just funny- they say something deep about LIFE. As a consequence people who dislike Rick & Morty truly ARE idiots- of course they wouldn't appreciate, for instance, the humour in Rick's existential catchphrase 'Wubba Lubba Dub Dub,' which itself is a cryptic reference to Turgenev's Russian epic Fathers and Sons. I'm smirking right now just imagining one of those addlepated simpletons scratching their heads in confusion as Dan Harmon's genius wit unfolds itself on their television screens. What fools.. how I pity them. 😂
And yes, by the way, i DO have a Rick & Morty tattoo. And no, you cannot see it. It's for the ladies' eyes only- and even then they have to demonstrate that they're within 5 IQ points of my own (preferably lower) beforehand. Nothin personnel kid 😎"""
# Parameters to be passed along with the input text
params = {
"min_length": 8,
"max_length": 4096,
"no_repeat_ngram_size": 3,
"encoder_no_repeat_ngram_size": 4,
"repetition_penalty": 1.25,
"num_beams": 4,
"num_beam_groups": 1,
"length_penalty": 1.0,
"early_stopping": True,
"do_sample": False,
}
with Timer() as timer:
output = query(
{
"inputs": input_text,
"parameters": params,
}
)
if isinstance(output, list):
summary = output[0]["summary_text"]
print(f"Summary is:\t{summary}")
else:
print(f"Error running inference, see:\t{output}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment