refactoring
Browse files
app.py
CHANGED
@@ -9,7 +9,7 @@ SECRET_KEY = os.environ.get("summarizer")
|
|
9 |
|
10 |
# asynchronous function
|
11 |
async def summarize(text, retries=5):
|
12 |
-
if len(text) <
|
13 |
return "⛔ You input is too short! Please input a longer text for summarization."
|
14 |
headers = {"Authorization": f"Bearer {SECRET_KEY}"}
|
15 |
data = {
|
@@ -67,100 +67,4 @@ iface = gr.Interface(
|
|
67 |
]
|
68 |
)
|
69 |
|
70 |
-
iface.launch()
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
# ------------------------------------- OLD CODE --------------------------------------------------------------------
|
78 |
-
# import gradio as gr
|
79 |
-
# import aiohttp
|
80 |
-
# import asyncio
|
81 |
-
# import os
|
82 |
-
|
83 |
-
# # API endpoint - model name
|
84 |
-
# API_URL = "https://api-inference.huggingface.co/models/Rahmat82/t5-small-finetuned-summarization-xsum"
|
85 |
-
# SECRET_KEY = os.environ.get("summarizer")
|
86 |
-
|
87 |
-
# async def summarize(text):
|
88 |
-
# headers = {"Authorization": f"Bearer {SECRET_KEY}"}
|
89 |
-
# data = {"inputs": text,
|
90 |
-
# "options": {
|
91 |
-
# "max_new_tokens": 50,
|
92 |
-
# "min_length": 20,
|
93 |
-
# "max_length": 300,
|
94 |
-
# "length_penalty": 2.0,
|
95 |
-
# "num_beams": 3,
|
96 |
-
# "early_stopping": True
|
97 |
-
# }
|
98 |
-
# }
|
99 |
-
# async with aiohttp.ClientSession() as session:
|
100 |
-
# async with session.post(API_URL, headers=headers, json=data) as response:
|
101 |
-
# response.raise_for_status() # Raise exception for non-200 status codes
|
102 |
-
# result = await response.json()
|
103 |
-
# return result[0]["summary_text"]
|
104 |
-
|
105 |
-
# iface = gr.Interface(
|
106 |
-
# theme=gr.themes.Soft(),
|
107 |
-
# fn=summarize,
|
108 |
-
# title="Text Summarizer",
|
109 |
-
# description="<h4>On local GPU/CPU it takes <1s 🚀</h4> <h5>Please keep in mind that the input text should be at least few sentences long 🙂</h5>" ,
|
110 |
-
# inputs=gr.Textbox(label="Write your text here --- quick🐰 input", lines=10),
|
111 |
-
# outputs=gr.Textbox(label="Summary --- quack🦆 output"),
|
112 |
-
# submit_btn=gr.Button("Summarize", variant="primary"),
|
113 |
-
# allow_flagging='never',
|
114 |
-
|
115 |
-
# examples=[
|
116 |
-
# ["""
|
117 |
-
# A new form of transport will be with us in the next few years – flying taxis. Many of us grew up watching sci-fi movies with airborne taxis. Science fiction is now becoming science fact. The Japanese airline ANA has teamed up with a U.S. tech start-up called Joby Aviation. The two companies aim to start operating air taxis at the 2025 World Expo in Osaka. They are currently working together on building the flying vehicle. They also need to work out what traffic rules the taxis will need to follow, and what kind of training flying taxi pilots will need. The five-seat, all-electric taxi will be able to take off and land vertically. It will have a flight range of 241 kilometers and a top speed of 321kph. Joby's CEO said the taxis would be good for the environment. He told reporters: "Joby exists to help people save time while reducing their carbon footprint. Japan offers us a spectacular opportunity to do just that with 92 per cent of the population living in urban areas, and Tokyo being one of the top 20 most congested cities in the world." The president of ANA, Koji Shibata, was also excited about the project. He said: "ANA has 70 years of experience delivering safe and reliable flights to customers across the world.…Being able to provide them with the option to travel rapidly, and sustainably, from an international airport to a downtown location is very appealing."
|
118 |
-
# """],
|
119 |
-
# ["""
|
120 |
-
# The koala is regarded as the epitome of cuddliness. However, animal lovers will be saddened to hear that this lovable marsupial has been moved to the endangered species list. The Australian Koala Foundation estimates there are somewhere between 43,000-100,000 koalas left in the wild. Their numbers have been dwindling rapidly due to disease, loss of habitat, bushfires, being hit by cars, and other threats. Stuart Blanch from the World Wildlife Fund in Australia said: "Koalas have gone from no listing to vulnerable to endangered within a decade. That is a shockingly fast decline." He added that koalas risk "sliding toward extinction" unless there are "stronger laws…to protect their forest homes". The koala has huge cultural significance for Australia. Wikipedia writes: "The koala is well known worldwide and is a major draw for Australian zoos and wildlife parks. It has been featured in advertisements, games, cartoons, and as soft toys. It benefited the national tourism industry by over an estimated billion Australian dollars in 1998, a figure that has since grown." Despite this, efforts to protect the koala have been failing. Australia's Environment Minister Sussan Ley said there have been "many pressures on the koala," and that it is "vulnerable to climate change and to disease". She said the 2019-2020 bushfires, which killed at least 6,400 of the animals, were "a tipping point".
|
121 |
-
# """],
|
122 |
-
# ["""
|
123 |
-
# Everybody knows that eating carrots is good for our eyesight. A new study suggests that grapes are also good for our eyes. Researchers from the National University of Singapore have found that eating just a few grapes a day can improve our vision. This is especially so for people who are older. Dr Eun Kim, the lead researcher, said: "Our study is the first to show that grape consumption beneficially impacts eye health in humans, which is very exciting, especially with a growing, ageing population." Dr Kim added that, "grapes are an easily accessible fruit that studies have shown can have a beneficial impact" on our eyesight. This is good news for people who don't really like carrots. The study is published in the journal "Food & Function". Thirty-four adults took part in a series of experiments over 16 weeks. Half of the participants ate one-and-a-half cups of grapes per day; the other half ate a placebo snack. Dr Kim did not tell the participants or the researchers whether she was testing the grapes or the snack. She thought that not revealing this information would give better test results. She found that people who ate the grapes had improved muscle strength around the retina. The retina passes information about light to the brain via electrical signals. It protects the eyes from damaging blue light. A lot of blue light comes from computer and smartphone screens, and from LED lights.
|
124 |
-
# """]
|
125 |
-
# ]
|
126 |
-
# )
|
127 |
-
|
128 |
-
# iface.launch()
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
# -------------------------------------------------------------------------------------------
|
133 |
-
# # asynchronous function
|
134 |
-
# async def summarize(text, retries=3):
|
135 |
-
# if len(text)<250:
|
136 |
-
# return "⛔ You input is too short! Please input a longer text for summarization."
|
137 |
-
# headers = {"Authorization": f"Bearer {SECRET_KEY}"}
|
138 |
-
# data = {
|
139 |
-
# "inputs": text,
|
140 |
-
# "options": {
|
141 |
-
# "max_new_tokens": 100,
|
142 |
-
# "min_length": 10,
|
143 |
-
# "max_length": 500,
|
144 |
-
# "length_penalty": 2.0,
|
145 |
-
# "num_beams": 3,
|
146 |
-
# "early_stopping": True
|
147 |
-
# }
|
148 |
-
# }
|
149 |
-
# async with aiohttp.ClientSession() as session:
|
150 |
-
# for attempt in range(retries):
|
151 |
-
# try:
|
152 |
-
# async with session.post(API_URL, headers=headers, json=data) as response:
|
153 |
-
# # raise exception for non-200 status codes
|
154 |
-
# response.raise_for_status()
|
155 |
-
# result = await response.json()
|
156 |
-
# return result[0]["summary_text"]
|
157 |
-
# except aiohttp.ClientResponseError as e:
|
158 |
-
# # retry only if the error is 503 (Service Unavailable)
|
159 |
-
# if e.status == 503 and attempt < retries - 1:
|
160 |
-
# # print(f"Retry attempt {attempt+1} for 503 error.")
|
161 |
-
# # add a short delay before retrying
|
162 |
-
# await asyncio.sleep(1)
|
163 |
-
# continue
|
164 |
-
# else:
|
165 |
-
# return "Oops! 🙈 It looks like those mischievous monkeys🐒 might be swinging around the server, causing a bit of chaos with the cables. Please try it again!"
|
166 |
-
# # return f"Error: {e.status}, message='{e.message}'. Please try again 🔁"
|
|
|
9 |
|
10 |
# asynchronous function
|
11 |
async def summarize(text, retries=5):
|
12 |
+
if len(text) < 264:
|
13 |
return "⛔ You input is too short! Please input a longer text for summarization."
|
14 |
headers = {"Authorization": f"Bearer {SECRET_KEY}"}
|
15 |
data = {
|
|
|
67 |
]
|
68 |
)
|
69 |
|
70 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|