topic_suggest / app.py
Manish-4007's picture
updated Summary also
8242e54
raw
history blame
3.24 kB
import streamlit as st
import time
import requests
API_URL = "https://api-inference.huggingface.co/models/tuner007/pegasus_summarizer"
headers = {"Authorization": "Bearer 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"}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
@st.cache_resource
def load_topic_transfomers():
from transformers import pipeline
try:
topic_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli",device="cuda", compute_type="float16")
except Exception as e:
topic_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
print("Error: ", e)
return topic_classifier
def suggest_topic(topic_classifier,text):
# while len(text)> 1024:
# text = summarize(whole_text[:-10])
possible_topics = ["Gadgets", 'Business','Finance', 'Health', 'Sports', 'Politics','Government','Science','Education', 'Travel', 'Tourism', 'Finance & Economics','Market','Technology','Scientific Discovery',
'Entertainment','Environment','News & Media', "Space,Universe & Cosmos", "Fashion", "Manufacturing and Constructions","Law & Crime","Motivation", "Development & Socialization", "Archeology"]
result = topic_classifier(text, possible_topics)
return result['labels']
st.title("Topic Suggestion")
if 'topic_model' not in st.session_state:
with st.spinner("Loading Model....."):
st.session_state.topic_model = load_topic_transfomers()
st.success("Model_loaded")
st.session_state.model = True
whole_text = st.text_input("Enter the text Here: ")
output = query({
"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.",
})
try:
if st.button('Suggest topic'):
st.subheader('Original Text: ')
st.write(whole_text)
st.subheader('\nSummarized Text:')
st.write(output[0]["summary_text"])
start= time.time()
with st.spinner("Scanning content to suggest topics"):
topic_classifier = st.session_state.topic_model
predicted_topic = suggest_topic(topic_classifier,whole_text)
clk = time.time()-start
if clk < 60:
st.write(f'Generated in {(clk)} secs')
else:
st.write(f'Generated in {(clk)/60} minutes')
st.subheader('Top 10 Topics related to the content')
for i in predicted_topic[:10]:
st.write(i)
except Exception as e:
print("Error", e)