import streamlit as st import torch import numpy as np from transformers import AutoTokenizer from transformers import BertForSequenceClassification st.set_page_config(layout='wide', initial_sidebar_state='expanded') col1, col2= st.columns(2) with col1: st.title("FireWatch") st.markdown("PREDICT WHETHER HEAT SIGNATURES AROUND THE GLOBE ARE LIKELY TO BE FIRES!") st.markdown("Traing Code at:") st.markdown("https://colab.research.google.com/drive/1-IfOMJ-X8MKzwm3UjbJbK6RmhT7tk_ye?usp=sharing") st.markdown("Try the Model Yourself at:") st.markdown("https://colab.research.google.com/drive/1GmweeQrkzs0OXQ_KNZsWd1PQVRLCWDKi?usp=sharing") st.markdown("## Sample Table") table_html = """
Category Latitude, Longitude, Brightness, FRP
Likely -26.76123, 147.15512, 393.02, 203.63
Likely -26.7598, 147.14514, 361.54, 79.4
Unlikely -25.70059, 149.48932, 313.9, 5.15
Unlikely -24.4318, 151.83102, 307.98, 8.79
Unlikely -23.21878, 148.91298, 314.08, 7.4
Likely 7.87518, 19.9241, 316.32, 39.63
Unlikely -20.10942, 148.14326, 314.39, 8.8
Unlikely 7.87772, 19.9048, 304.14, 13.43
Likely -20.79866, 124.46834, 366.74, 89.06
""" st.markdown(table_html, unsafe_allow_html=True) tree = """
""" with col2: @st.cache(suppress_st_warning=True, allow_output_mutation=True) def load_model(show_spinner=True): MODEL_PATH = "NimaKL/FireWatch_tiny_75k" model = BertForSequenceClassification.from_pretrained(MODEL_PATH) return model token_id = [] attention_masks = [] def preprocessing(input_text, tokenizer): ''' Returns with the following fields: - input_ids: list of token ids - token_type_ids: list of token type ids - attention_mask: list of indices (0,1) specifying which tokens should considered by the model (return_attention_mask = True). ''' return tokenizer.encode_plus( input_text, add_special_tokens = True, max_length = 16, pad_to_max_length = True, return_attention_mask = True, return_tensors = 'pt' ) def predict(new_sentence): device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # We need Token IDs and Attention Mask for inference on the new sentence test_ids = [] test_attention_mask = [] # Apply the tokenizer encoding = preprocessing(new_sentence, tokenizer) # Extract IDs and Attention Mask test_ids.append(encoding['input_ids']) test_attention_mask.append(encoding['attention_mask']) test_ids = torch.cat(test_ids, dim = 0) test_attention_mask = torch.cat(test_attention_mask, dim = 0) # Forward pass, calculate logit predictions with torch.no_grad(): output = model(test_ids.to(device), token_type_ids = None, attention_mask = test_attention_mask.to(device)) prediction = 'Likely' if np.argmax(output.logits.cpu().numpy()).flatten().item() == 1 else 'Unlikely' pred = 'Predicted Class: '+ prediction return pred model = load_model() tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") with col2: st.markdown('## Enter Prediction Data in Correct Format "Latitude, Longtitude, Brightness, FRP"') text = st.text_input('Predition Data: ', 'Example: 8.81064, -65.07661, 328.04, 18.76') aButton = st.button('Predict') if text or aButton: with st.spinner('Wait for it...'): st.success(predict(text)) st.markdown(tree, unsafe_allow_html=True)