NarayanaMayya commited on
Commit
32664e6
1 Parent(s): aeb0a95

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +185 -0
app.py ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import streamlit as st
3
+ import torch
4
+ from transformers import AutoTokenizer, LEDForConditionalGeneration
5
+ from tensorflow.keras.models import load_model
6
+ from transformers import TFBertForSequenceClassification, BertTokenizer
7
+ st.set_page_config(page_title="Summarization&tweet_analysis", page_icon="📈",layout="wide")
8
+ hide_streamlit_style = """
9
+ <style>
10
+ #MainMenu {visibility: hidden;}
11
+ footer {
12
+ visibility: hidden;
13
+
14
+ }
15
+ footer:after {
16
+ content:'©2023 Sravathi AI Technology. All rights reserved.';
17
+ visibility: visible;
18
+ display: block;
19
+ position: relative;
20
+ #background-color: red;
21
+ padding: 5px;
22
+ top: 2px;
23
+ }
24
+ </style>
25
+ """
26
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)
27
+ import pandas as pd
28
+ import time
29
+ import sys
30
+ import pickle
31
+ #from stqdm import stqdm
32
+ import base64
33
+ #from tensorflow.keras.preprocessing.text import Tokenizer
34
+ #from tensorflow.keras.preprocessing.sequence import pad_sequences
35
+ import numpy as np
36
+ import json
37
+ import os
38
+ import re
39
+
40
+ #from tensorflow.keras.models import load_model
41
+ #st.write("API examples - Dermatophagoides, Miconazole, neomycin,Iothalamate")
42
+
43
+ #background_image = sys.path[1]+"/streamlit_datafile_links/audience-1853662_960_720.jpg" # Path to your background image
44
+ def add_bg_from_local(image_file):
45
+ with open(image_file, "rb") as image_file:
46
+ encoded_string = base64.b64encode(image_file.read())
47
+ st.markdown(
48
+ f"""
49
+ <style>
50
+ .stApp {{
51
+ background-image: url(data:image/{"jpg"};base64,{encoded_string.decode()});
52
+ background-size: cover
53
+ }}
54
+ </style>
55
+ """,
56
+ unsafe_allow_html=True
57
+ )
58
+ #add_bg_from_local(background_image)
59
+
60
+ #@st.cache
61
+ st.header('Summarization & tweet_analysis')
62
+ def convert_df(df):
63
+ # IMPORTANT: Cache the conversion to prevent computation on every rerun
64
+ return df.to_csv(index=False).encode('utf-8')
65
+ col1, col2 = st.columns([4,1])
66
+ result_csv_batch_sql = result_csv_batch_fail=result_csv_batch=result_csv4=result_csv3=result_csv1=result_csv2=0
67
+
68
+ with col1:
69
+ models = st.selectbox(
70
+ 'Select the option',
71
+ ('summarization_model1','tweet_analysis' ))
72
+ #try:
73
+ if models == 'summarization_model1':
74
+ st.markdown("")
75
+ else:
76
+ st.markdown("")
77
+ with st.form("form1"):
78
+ hide_label = """
79
+ <style>
80
+ .css-9ycgxx {
81
+ display: none;
82
+ }
83
+ </style>
84
+ """
85
+ text_data = st.text_input('Enter the text')
86
+ print(text_data)
87
+ st.markdown(hide_label, unsafe_allow_html=True)
88
+ submitted = st.form_submit_button("Submit")
89
+ if submitted:
90
+ if models == 'summarization_model1':
91
+ #torch.cuda.set_device(2)
92
+ tokenizer = AutoTokenizer.from_pretrained('allenai/PRIMERA-multinews')
93
+ model = LEDForConditionalGeneration.from_pretrained('allenai/PRIMERA-multinews')
94
+ #device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # get the device
95
+ device = "cpu"
96
+ model.to(device) # move the model to the device
97
+ documents = text_data
98
+
99
+ # Tokenize and encode the documents
100
+ inputs = tokenizer(documents, return_tensors='pt', padding=True, truncation=True,max_length=1000000)
101
+
102
+ # Move the inputs to the device
103
+ inputs = inputs.to(device)
104
+
105
+ # Generate summaries
106
+ outputs = model.generate(**inputs,max_length=1000000)
107
+
108
+ # Decode the summaries
109
+ st.write(tokenizer.batch_decode(outputs, skip_special_tokens=True))
110
+ st.success('Prediction done successfully!', icon="✅")
111
+ else:
112
+ # Define the custom objects (custom layers) needed for loading the model
113
+ custom_objects = {"TFBertForSequenceClassification": TFBertForSequenceClassification}
114
+
115
+ # Load the best model checkpoint
116
+ best_model = load_model('best_model_checkpoint_val_acc_0.8697_epoch_03.h5', custom_objects=custom_objects)
117
+
118
+ # Assuming you already have the test set DataFrame (df_test) and tokenizer
119
+ tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
120
+ test_encodings = tokenizer(text_data, padding=True, truncation=True, return_tensors='tf')
121
+ test_dataset = tf.data.Dataset.from_tensor_slices((dict(test_encodings)))
122
+
123
+ # Make predictions on the test set using the loaded model
124
+ predictions_probabilities = best_model.predict(test_dataset.batch(8))
125
+
126
+ # Convert probabilities to one-hot encoded predictions
127
+ predictions_onehot = np.eye(9)[np.argmax(predictions_probabilities, axis=1)]
128
+
129
+ # Display or save the DataFrame with predicted labels
130
+ index_arg = np.argmax(predictions_probabilities, axis=1)
131
+ # Later, you can load the LabelEncoder
132
+ label_encoder = joblib.load('label_encoder.joblib')
133
+ result_label = label_encoder.inverse_transform(index_arg)
134
+
135
+ # Display or save the DataFrame with predicted labels
136
+ st.write("Item name: ", result_label[0])
137
+
138
+ from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification
139
+ from scipy.special import softmax
140
+
141
+ MODEL = f"cardiffnlp/twitter-roberta-base-sentiment-latest"
142
+ tokenizer = AutoTokenizer.from_pretrained(MODEL)
143
+ config = AutoConfig.from_pretrained(MODEL)
144
+ # PT
145
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL)
146
+ #model.save_pretrained(MODEL)
147
+ #text = "Covid cases are increasing fast!"
148
+ pred_label = []
149
+ pred_scor = []
150
+ def preprocess(text):
151
+ new_text = []
152
+ for t in text.split(" "):
153
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
154
+ t = 'http' if t.startswith('http') else t
155
+ new_text.append(t)
156
+ return " ".join(new_text)
157
+ def predict_pret(text):
158
+ #print(text)
159
+ text = preprocess(text)
160
+ encoded_input = tokenizer(text, return_tensors='pt')
161
+ output = model(**encoded_input)
162
+ scores = output[0][0].detach().numpy()
163
+ scores = softmax(scores)
164
+
165
+ ranking = np.argsort(scores)
166
+ ranking = ranking[::-1]
167
+ l = config.id2label[ranking[0]]
168
+ s = scores[ranking[0]]
169
+ return l,s
170
+
171
+ l,s = predict_pret(text_data)
172
+
173
+ st.write("Sentiment is: ", l)
174
+
175
+ st.success('Prediction done successfully!', icon="✅")
176
+ _='''
177
+ except Exception as e:
178
+ if 'NoneType' or 'not defined' in str(e):
179
+ st.warning('Enter the required inputs', icon="⚠️")
180
+ else:
181
+ st.warning(str(e), icon="⚠️")
182
+ '''
183
+
184
+ for i in range(30):
185
+ st.markdown('##')