Spaces:
Runtime error
Runtime error
Modified app.py Replaced AWS Secretes Manager code. Used Hugging Face Secrets variable
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
from transformers import AutoTokenizer, TFAutoModelForSeq2SeqLM
|
5 |
+
import os
|
6 |
+
|
7 |
+
def get_api():
|
8 |
+
api_key = os.getenv("NYT_ARTICLE_API")
|
9 |
+
if api_key is None:
|
10 |
+
raise ValueError("NYT_ARTICLE_API environment variable not set.")
|
11 |
+
return api_key
|
12 |
+
|
13 |
+
def get_abstracts(query):
|
14 |
+
api_key = get_api()
|
15 |
+
url = f'https://api.nytimes.com/svc/search/v2/articlesearch.json?q={query}&fq=source:("The New York Times")&api-key={api_key}'
|
16 |
+
response = requests.get(url).json()
|
17 |
+
abstracts = []
|
18 |
+
docs = response.get('response', {}).get('docs', [])
|
19 |
+
for doc in docs:
|
20 |
+
abstract = doc.get('abstract', '')
|
21 |
+
if abstract:
|
22 |
+
abstracts.append(abstract)
|
23 |
+
return abstracts
|
24 |
+
|
25 |
+
def summarizer(query):
|
26 |
+
abstracts = get_abstracts(query)
|
27 |
+
input_text = ' '.join(abstracts)
|
28 |
+
|
29 |
+
tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_billsum_model")
|
30 |
+
inputs = tokenizer(input_text, return_tensors="tf").input_ids
|
31 |
+
|
32 |
+
model = TFAutoModelForSeq2SeqLM.from_pretrained("stevhliu/my_awesome_billsum_model", from_pt=True)
|
33 |
+
outputs = model.generate(inputs, max_length=100, do_sample=False)
|
34 |
+
|
35 |
+
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
36 |
+
return abstracts, summary
|
37 |
+
|
38 |
+
iface = gr.Interface(
|
39 |
+
fn=summarizer,
|
40 |
+
inputs=gr.inputs.Textbox(placeholder="Enter your query"),
|
41 |
+
outputs=[
|
42 |
+
gr.outputs.Textbox(label="Abstracts"),
|
43 |
+
gr.outputs.Textbox(label="Summary")
|
44 |
+
],
|
45 |
+
title="New York Times Articles Summarizer",
|
46 |
+
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."
|
47 |
+
)
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
iface.launch()
|