Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 keras.preprocessing.text import Tokenizer
|
7 |
+
from keras.preprocessing.sequence import pad_sequences
|
8 |
+
from sklearn.preprocessing import LabelEncoder, StandardScaler
|
9 |
+
from nltk.sentiment.vader import SentimentIntensityAnalyzer
|
10 |
+
import nltk
|
11 |
+
|
12 |
+
# Download VADER lexicon for sentiment analysis
|
13 |
+
nltk.download('vader_lexicon')
|
14 |
+
|
15 |
+
# Load the model from Hugging Face
|
16 |
+
model_path = hf_hub_download(repo_id="your-username/your-model-repo", filename="my_model.h5")
|
17 |
+
model = load_model(model_path)
|
18 |
+
|
19 |
+
# Initialize Tokenizer and LabelEncoders
|
20 |
+
tokenizer = Tokenizer(num_words=10000, oov_token='<OOV>')
|
21 |
+
label_encoders = {
|
22 |
+
'impact': LabelEncoder(),
|
23 |
+
'priority': LabelEncoder(),
|
24 |
+
'category': LabelEncoder(),
|
25 |
+
'urgency': LabelEncoder()
|
26 |
+
}
|
27 |
+
|
28 |
+
# Function to preprocess input data
|
29 |
+
def preprocess_input(short_description, impact, priority, category, urgency):
|
30 |
+
# Encode categorical features
|
31 |
+
input_data = pd.DataFrame({
|
32 |
+
'short_description': [short_description],
|
33 |
+
'impact': [impact],
|
34 |
+
'priority': [priority],
|
35 |
+
'category': [category],
|
36 |
+
'urgency': [urgency]
|
37 |
+
})
|
38 |
+
|
39 |
+
for column in ['impact', 'priority', 'category', 'urgency']:
|
40 |
+
input_data[column] = label_encoders[column].fit_transform(input_data[column])
|
41 |
+
|
42 |
+
# Tokenize text data
|
43 |
+
sequences = tokenizer.texts_to_sequences(input_data['short_description'])
|
44 |
+
padded_sequences = pad_sequences(sequences, maxlen=50, padding='post', truncating='post')
|
45 |
+
|
46 |
+
# Feature engineering: Add sentiment score
|
47 |
+
sid = SentimentIntensityAnalyzer()
|
48 |
+
input_data['sentiment_score'] = input_data['short_description'].apply(lambda x: sid.polarity_scores(x)['compound'])
|
49 |
+
|
50 |
+
# Normalize numerical features
|
51 |
+
numerical_features = input_data[['impact', 'priority', 'category', 'urgency', 'sentiment_score']]
|
52 |
+
scaler = StandardScaler()
|
53 |
+
scaled_numerical_features = scaler.fit_transform(numerical_features)
|
54 |
+
|
55 |
+
# Prepare the final input features
|
56 |
+
X_input = np.concatenate([padded_sequences, scaled_numerical_features], axis=1)
|
57 |
+
return X_input, input_data['sentiment_score'].iloc[0]
|
58 |
+
|
59 |
+
# Function to make predictions
|
60 |
+
def predict(short_description, impact, priority, category, urgency):
|
61 |
+
X_input, sentiment_score = preprocess_input(short_description, impact, priority, category, urgency)
|
62 |
+
predictions = model.predict(X_input)
|
63 |
+
predicted_label = np.argmax(predictions, axis=1)[0]
|
64 |
+
return predicted_label, sentiment_score
|
65 |
+
|
66 |
+
# Define Gradio interface
|
67 |
+
inputs = [
|
68 |
+
gr.inputs.Textbox(lines=2, placeholder="Enter short description...", label="Short Description"),
|
69 |
+
gr.inputs.Textbox(lines=1, placeholder="Enter impact...", label="Impact (e.g., '2 - Medium')"),
|
70 |
+
gr.inputs.Textbox(lines=1, placeholder="Enter priority...", label="Priority (e.g., '2 - Medium')"),
|
71 |
+
gr.inputs.Textbox(lines=1, placeholder="Enter category...", label="Category (e.g., 'Network')"),
|
72 |
+
gr.inputs.Textbox(lines=1, placeholder="Enter urgency...", label="Urgency (e.g., '1 - High')")
|
73 |
+
]
|
74 |
+
|
75 |
+
outputs = [
|
76 |
+
gr.outputs.Textbox(label="Predicted Duration Bin"),
|
77 |
+
gr.outputs.Textbox(label="Sentiment Score")
|
78 |
+
]
|
79 |
+
|
80 |
+
interface = gr.Interface(fn=predict, inputs=inputs, outputs=outputs, title="Issue Resolution Predictor", description="Predict the duration bin and sentiment score based on issue description and related features.")
|
81 |
+
|
82 |
+
# Launch the interface
|
83 |
+
interface.launch()
|