Spaces:
Sleeping
Sleeping
Amitontheweb
commited on
Commit
•
5e1aec1
1
Parent(s):
bc710b3
Upload app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# # Summarization - ahmedmbutt/PTS-Bart-Large-CNN - MIT license
|
5 |
+
|
6 |
+
# In[22]:
|
7 |
+
|
8 |
+
|
9 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
10 |
+
import torch
|
11 |
import gradio as gr
|
12 |
|
13 |
+
|
14 |
+
# In[34]:
|
15 |
+
|
16 |
+
|
17 |
+
# Load model directly
|
18 |
+
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained("ahmedmbutt/PTS-Bart-Large-CNN")
|
20 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("ahmedmbutt/PTS-Bart-Large-CNN")
|
21 |
+
|
22 |
+
|
23 |
+
# In[60]:
|
24 |
+
|
25 |
+
|
26 |
+
def summarize (text):
|
27 |
+
|
28 |
+
input_ids = tokenizer(text, return_tensors="pt").input_ids
|
29 |
+
|
30 |
+
#run through model - use [0] to select the tensor as .generate returns a list
|
31 |
+
output_ids = model.generate(
|
32 |
+
input_ids,
|
33 |
+
max_new_tokens=150,
|
34 |
+
no_repeat_ngram_size=4,
|
35 |
+
do_sample=True
|
36 |
+
)[0]
|
37 |
+
|
38 |
+
output = tokenizer.decode (output_ids, skip_special_tokens=True)
|
39 |
+
|
40 |
+
return (output)
|
41 |
+
|
42 |
+
|
43 |
+
# In[74]:
|
44 |
+
|
45 |
+
|
46 |
+
iface = gr.Interface(fn=summarize,
|
47 |
+
inputs=[gr.Textbox(label="Paste text to summarize and hit 'Submit'.", lines=10)],
|
48 |
+
outputs=[gr.Textbox(label="Summarized text:", lines=10)],
|
49 |
+
title="AI Text Summarizer",
|
50 |
+
#description="Turn keywords into creative suggestions",
|
51 |
+
article="""
|
52 |
+
<div align=left>
|
53 |
+
<h1>AI Text Summarizer</h1>
|
54 |
+
<li>Generate a succinct and overall summary of text of any length.</li>
|
55 |
+
<li>Many AI models fail to generate a good summary - they either pick up first sentences, or fail to blend information together</li>
|
56 |
+
<li>The AI text summarizer is well trained to create better summaries that incorporate details and put facts together.</li>
|
57 |
+
<li>Click on the example for a demo</li>
|
58 |
+
|
59 |
+
<p>AI Model:<br>
|
60 |
+
<li>Bart-Large-CNN trained on the PTS dataset.</li>
|
61 |
+
<li>Original model id: ahmedmbutt/PTS-Bart-Large-CNN</li>
|
62 |
+
|
63 |
+
</p><p>Default parameter details:<br>
|
64 |
+
<li><code>do_sample=True</code>, <code>no_repeat_ngram_size=3</code>,<code>max_new_tokens = 30</code></li>
|
65 |
+
</div>
|
66 |
+
|
67 |
+
""",
|
68 |
+
flagging_mode='never',
|
69 |
+
examples= ["""What is driving food inflation higher?
|
70 |
+
A drought last year and an ongoing heat wave have significantly reduced the supplies of foods like pulses, vegetables, and cereals. Curbs on food exports and reducing tariffs on imports have had little effect. Although vegetable supplies generally decrease during the summer months, this year's decline is much more pronounced. Temperatures in nearly half of the country are soaring 4-9 degrees Celsius above normal, spoiling harvested and stored vegetables and hindering the planting of crops such as onions, tomatoes, eggplant and spinach."""],
|
71 |
+
cache_examples=True,
|
72 |
+
)
|
73 |
+
|
74 |
+
iface.launch()
|
75 |
+
|
76 |
+
|
77 |
+
# In[ ]:
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
|
82 |
+
|
83 |
+
# #### Run Using Inference Endpoing - Serverless with Access Token generated on HF
|
84 |
+
|
85 |
+
# In[ ]:
|
86 |
+
|
87 |
+
|
88 |
+
import requests
|
89 |
+
|
90 |
+
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
91 |
+
headers = {"Authorization": 'Bearer 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'}
|
92 |
+
|
93 |
+
def query(payload):
|
94 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
95 |
+
return response.json()
|
96 |
+
|
97 |
+
output = query({
|
98 |
+
"inputs": "The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.",
|
99 |
+
})
|
100 |
+
|