import gradio as gr import requests import json from transformers import AutoTokenizer, TFAutoModelForSeq2SeqLM import os def get_api(): api_key = os.getenv("NYT_ARTICLE_API") if api_key is None: raise ValueError("NYT_ARTICLE_API environment variable not set.") return api_key def get_abstracts(query): api_key = get_api() url = f'https://api.nytimes.com/svc/search/v2/articlesearch.json?q={query}&fq=source:("The New York Times")&api-key={api_key}' response = requests.get(url).json() abstracts = [] docs = response.get('response', {}).get('docs', []) for doc in docs: abstract = doc.get('abstract', '') if abstract: abstracts.append(abstract) return abstracts def summarizer(query): abstracts = get_abstracts(query) input_text = ' '.join(abstracts) tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_billsum_model") inputs = tokenizer(input_text, return_tensors="tf").input_ids model = TFAutoModelForSeq2SeqLM.from_pretrained("stevhliu/my_awesome_billsum_model", from_pt=True) outputs = model.generate(inputs, max_length=100, do_sample=False) summary = tokenizer.decode(outputs[0], skip_special_tokens=True) return abstracts, summary iface = gr.Interface( fn=summarizer, inputs=gr.inputs.Textbox(placeholder="Enter your query"), outputs=[ gr.outputs.Textbox(label="Abstracts"), gr.outputs.Textbox(label="Summary") ], title="New York Times Articles Summarizer", description="This summarizer actually does not yet summarize New York Times articles because of certain limitations. Type in something like 'Manipur' or 'Novak Djokovic' you will get a summary of that topic. What actually happens is that the query goes through the API. The abstract of article's content is added or concatenated, and then a text of considerable length is generated. That text is then summarized. So, this is an article summarizer but summarizes only abstracts of a particular article, ensuring that readers get the essence of a topic. This is a successful implementation of a pretrained T5 Transformer model." ) if __name__ == "__main__": iface.launch()