Sourav-003/StackExchange-Tag-Predictor
0
1 2import gradio as gr3import numpy as np4import pandas as pd5import re6from bs4 import BeautifulSoup7from tensorflow.keras.models import load_model8from tensorflow.keras.preprocessing.sequence import pad_sequences9from tensorflow.keras.preprocessing.text import Tokenizer10import tensorflow as tf11import pickle12import os # Import os for reading params.txt13 14x_voc_size = 12575 # Replace with your actual vocabulary size from your notebook15max_len = 100 # Replace with your actual max_len from your notebook16 17# Rebuild the model architecture18from tensorflow.keras.models import Sequential19from tensorflow.keras.layers import Embedding, GRU, Dense20model = Sequential()21model.add(Embedding(x_voc_size, 50, trainable = True, input_shape=(max_len,),mask_zero=True))22model.add(GRU(128))23model.add(Dense(128,activation='relu'))24model.add(Dense(10,activation='sigmoid'))25 26# Load the best weights27try:28 model.load_weights("weights.best.keras")29except Exception as e:30 print(f"Error loading model weights: {e}")31 print("Please ensure 'weights.best.keras' is in the same directory as app.py")32 33 34# Load the tokenizer35try:36 with open('x_tokenizer.pickle', 'rb') as handle:37 x_tokenizer = pickle.load(handle)38except FileNotFoundError:39 print("Error: x_tokenizer.pickle not found. Please ensure it is in the same directory as app.py")40 exit() # Exit if a critical file is missing41 42# Load the MultiLabelBinarizer43try:44 with open('mlb.pickle', 'rb') as handle:45 mlb = pickle.load(handle)46except FileNotFoundError:47 print("Error: mlb.pickle not found. Please ensure it is in the same directory as app.py")48 exit() # Exit if a critical file is missing49 50# Load opt and max_len from params.txt51opt = 0.34 # Default value, will try to load from file52max_len = 100 # Default value, will try to load from file53try:54 with open('params.txt', 'r') as f:55 for line in f:56 line = line.strip()57 if line.startswith("opt:"):58 opt = float(line.split(":")[1])59 elif line.startswith("max_len:"):60 max_len = int(line.split(":")[1])61except FileNotFoundError:62 print("Warning: params.txt not found. Using default values for opt and max_len.")63except Exception as e:64 print(f"Warning: Error reading params.txt: {e}. Using default values for opt and max_len.")65 66 67# Preprocessing function (same as in the notebook)68def cleaner(text):69 text = BeautifulSoup(text, "html.parser").get_text()70 text = re.sub("[^a-zA-Z]", " ", text)71 text = text.lower()72 tokens = text.split()73 return " ".join(tokens)74 75# Classification function (same as in the notebook)76def classify(pred_prob,thresh):77 y_pred_seq = []78 for i in pred_prob:79 temp=[]80 for j in i:81 if j>=thresh:82 temp.append(1)83 else:84 temp.append(0)85 y_pred_seq.append(temp)86 return y_pred_seq87 88# Prediction function (adapted for a single comment input)89def predict_tag(comment):90 #preprocess91 cleaned_text = [cleaner(comment)]92 93 #convert to integer sequences94 seq = x_tokenizer.texts_to_sequences(cleaned_text)95 96 #pad the sequence97 pad_seq = pad_sequences(seq, padding='post', maxlen=max_len)98 99 #make predictions100 pred_prob = model.predict(pad_seq)101 classes = classify(pred_prob,opt)[0]102 103 classes = np.array([classes])104 predicted_tags = mlb.inverse_transform(classes)105 return ", ".join(predicted_tags[0]) # Return as a comma-separated string106 107# Create the Gradio interface108iface = gr.Interface(109 fn=predict_tag,110 inputs=gr.Textbox(lines=5, label="Enter StackExchange Question Text"),111 outputs=gr.Textbox(label="Predicted Tags"),112 title="StackExchange Tag Predictor",113 description="Predict tags for a given StackExchange question using a trained deep learning model."114)115 116# Launch the interface117if __name__ == "__main__":118 iface.launch(server_name="0.0.0.0", server_port=7860)119 120 