Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,22 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
-
from
|
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 |
-
|
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
|
17 |
-
|
18 |
-
model =
|
19 |
|
20 |
-
#
|
21 |
-
|
|
|
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
|
48 |
-
inputs = tokenizer(short_description, return_tensors='
|
49 |
-
|
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 |
-
|
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 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|