File size: 2,544 Bytes
a26068c
84f87ff
8242e54
 
 
 
 
 
 
 
a26068c
6bfa6c6
a26068c
 
 
 
 
 
 
 
 
528ab98
a26068c
0cd5f06
 
a26068c
 
4c57eb5
a26068c
 
 
 
 
 
 
6bfa6c6
 
528ab98
6bfa6c6
528ab98
a26068c
6bfa6c6
9e26e04
6bfa6c6
528ab98
9e26e04
 
 
 
 
8242e54
 
 
 
 
 
aa700c5
528ab98
 
d930cf3
566ee24
40e5e4c
d0fa0e7
d930cf3
 
f98a663
 
 
6bfa6c6
 
a26068c
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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: ")

try:
    if st.button('Suggest topic'):
        start= time.time()  
                	
        output = query({
        	"inputs": whole_text,
        })
        st.subheader('Original Text: ')
        st.write(whole_text)
        
        st.subheader('\nSummarized Text:')
        st.write(output[0]["summary_text"])
        
        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)