Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import pickle
|
| 4 |
+
import numpy as np
|
| 5 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 6 |
+
import os
|
| 7 |
+
import time
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
# Load the trained model
|
| 13 |
+
model = tf.keras.models.load_model("best_binary_model_after_tuning.h5")
|
| 14 |
+
|
| 15 |
+
# Load the tokenizer
|
| 16 |
+
with open("binary_tokenizer.pkl", "rb") as handle:
|
| 17 |
+
tokenizer = pickle.load(handle)
|
| 18 |
+
|
| 19 |
+
# Define fixed categories for 'type'
|
| 20 |
+
type_options = ["Change", "Incident", "Problem", "Request"]
|
| 21 |
+
|
| 22 |
+
# Define hardcoded label mapping for encoded results
|
| 23 |
+
priority_mapping = {0: "Low", 1: "Med/High"}
|
| 24 |
+
|
| 25 |
+
# Constants
|
| 26 |
+
MAX_LENGTH = 512
|
| 27 |
+
|
| 28 |
+
# Function to preprocess text input
|
| 29 |
+
def preprocess_text(text):
|
| 30 |
+
sequence = tokenizer.texts_to_sequences([text])
|
| 31 |
+
padded_sequence = pad_sequences(sequence, maxlen=MAX_LENGTH, padding='post', truncating='post')
|
| 32 |
+
return padded_sequence
|
| 33 |
+
|
| 34 |
+
# Function to preprocess categorical input (type)
|
| 35 |
+
def preprocess_type(selected_type):
|
| 36 |
+
mapping = {val: idx for idx, val in enumerate(type_options)}
|
| 37 |
+
return np.array([[mapping[selected_type]]])
|
| 38 |
+
|
| 39 |
+
# Function to make predictions
|
| 40 |
+
def generate_prediction(text_input, type_input):
|
| 41 |
+
features_combined = np.concatenate([text_input, type_input], axis=1)
|
| 42 |
+
prediction = model.predict(features_combined)[0][0] # Get the probability
|
| 43 |
+
predicted_label = int(prediction > 0.5) # Convert to 0 or 1
|
| 44 |
+
return priority_mapping[predicted_label]
|
| 45 |
+
|
| 46 |
+
# Streamlit UI
|
| 47 |
+
st.title("Resolve AI")
|
| 48 |
+
st.write("Enter your request and select a type to generate a prediction.")
|
| 49 |
+
|
| 50 |
+
user_input = st.text_area("Enter your text:", "")
|
| 51 |
+
type_selection = st.selectbox("Select type:", type_options)
|
| 52 |
+
|
| 53 |
+
if st.button("Generate Prediction"):
|
| 54 |
+
if user_input:
|
| 55 |
+
text_input = preprocess_text(user_input)
|
| 56 |
+
type_input = preprocess_type(type_selection)
|
| 57 |
+
predicted_priority = generate_prediction(text_input, type_input)
|
| 58 |
+
|
| 59 |
+
st.write(f"Predicted priority: {predicted_priority}")
|
| 60 |
+
|
| 61 |
+
if predicted_priority == "Med/High":
|
| 62 |
+
st.warning("This issue may require human intervention. Please contact support.")
|
| 63 |
+
else:
|
| 64 |
+
chatbot_link = 'https://huggingface.co/spaces/kdevoe/ResolveAI'
|
| 65 |
+
st.write('Please chat with our [assistant](%s) for further resolution'% chatbot_link)
|