xeroISB commited on
Commit
976bdda
·
verified ·
1 Parent(s): b427db3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -19
app.py CHANGED
@@ -1,24 +1,22 @@
1
  import gradio as gr
2
  import numpy as np
3
  import pandas as pd
4
- from keras.models import load_model
5
- from huggingface_hub import hf_hub_download
6
- from transformers import BertTokenizer
7
  from sklearn.preprocessing import LabelEncoder, StandardScaler
8
  from nltk.sentiment.vader import SentimentIntensityAnalyzer
9
  import nltk
10
- from transformers import TFBertModel
11
- bert_model = TFBertModel.from_pretrained('bert-base-uncased')
12
 
13
  # Download VADER lexicon for sentiment analysis
14
  nltk.download('vader_lexicon')
15
 
16
- # Load the model from Hugging Face
17
- model_path = hf_hub_download(repo_id="xeroISB/ServiceNowMTTR", filename="newIncidentModel.h5")
18
- model = load_model(model_path)
19
 
20
- # Initialize BERT tokenizer
21
- tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
 
22
 
23
  # Initialize LabelEncoders
24
  label_encoders = {
@@ -44,9 +42,9 @@ def preprocess_input(short_description, impact, priority, category, urgency):
44
 
45
  short_description = input_data['short_description'].iloc[0].lower()
46
 
47
- # Tokenize text data using BERT tokenizer
48
- inputs = tokenizer(short_description, return_tensors='tf', padding='max_length', truncation=True, max_length=50)
49
- padded_sequences = np.array(inputs['input_ids'])
50
 
51
  # Feature engineering: Add sentiment score
52
  sid = SentimentIntensityAnalyzer()
@@ -57,15 +55,19 @@ def preprocess_input(short_description, impact, priority, category, urgency):
57
  scaler = StandardScaler()
58
  scaled_numerical_features = scaler.fit_transform(numerical_features)
59
 
60
- # Prepare the final input features
61
- X_input = np.concatenate([padded_sequences, scaled_numerical_features], axis=1)
62
- return X_input, input_data['sentiment_score'].iloc[0]
63
 
64
  # Function to make predictions
65
  def predict(short_description, impact, priority, category, urgency):
66
- X_input, sentiment_score = preprocess_input(short_description, impact, priority, category, urgency)
67
- predictions = model.predict(X_input)
68
- predicted_label = np.argmax(predictions, axis=1)[0]
 
 
 
 
 
 
69
  return predicted_label, sentiment_score
70
 
71
  # Define Gradio interface
 
1
  import gradio as gr
2
  import numpy as np
3
  import pandas as pd
4
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
 
 
5
  from sklearn.preprocessing import LabelEncoder, StandardScaler
6
  from nltk.sentiment.vader import SentimentIntensityAnalyzer
7
  import nltk
8
+ import torch
 
9
 
10
  # Download VADER lexicon for sentiment analysis
11
  nltk.download('vader_lexicon')
12
 
13
+ # Load model and tokenizer from Hugging Face
14
+ tokenizer = AutoTokenizer.from_pretrained("xeroISB/ServiceNowMTTR")
15
+ model = AutoModelForSequenceClassification.from_pretrained("xeroISB/ServiceNowMTTR")
16
 
17
+ # Move model to GPU if available
18
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
19
+ model.to(device)
20
 
21
  # Initialize LabelEncoders
22
  label_encoders = {
 
42
 
43
  short_description = input_data['short_description'].iloc[0].lower()
44
 
45
+ # Tokenize text data using the new tokenizer
46
+ inputs = tokenizer(short_description, return_tensors='pt', padding='max_length', truncation=True, max_length=50)
47
+ inputs = {k: v.to(device) for k, v in inputs.items()}
48
 
49
  # Feature engineering: Add sentiment score
50
  sid = SentimentIntensityAnalyzer()
 
55
  scaler = StandardScaler()
56
  scaled_numerical_features = scaler.fit_transform(numerical_features)
57
 
58
+ return inputs, scaled_numerical_features
 
 
59
 
60
  # Function to make predictions
61
  def predict(short_description, impact, priority, category, urgency):
62
+ inputs, scaled_numerical_features = preprocess_input(short_description, impact, priority, category, urgency)
63
+
64
+ # Make prediction
65
+ with torch.no_grad():
66
+ outputs = model(**inputs)
67
+ logits = outputs.logits
68
+ predicted_label = torch.argmax(logits, axis=1).item()
69
+
70
+ sentiment_score = scaled_numerical_features[0][-1]
71
  return predicted_label, sentiment_score
72
 
73
  # Define Gradio interface