dredzo/Bidirectional_GRU_Language_Classifier
0
1import gradio as gr2import numpy as np3import tensorflow as tf4from tensorflow.keras.preprocessing.sequence import pad_sequences5import pickle6 7# Load the tokenizer and model8with open("tokenizer.pkl", "rb") as tokenizer_file:9 tokenizer = pickle.load(tokenizer_file)10 11model = tf.keras.models.load_model("bidirectional_gru_language_classifier.h5")12 13# Load label encoder for decoding predictions14with open("label_encoder.pkl", "rb") as label_file:15 label_encoder = pickle.load(label_file)16 17# Define max sequence length18MAX_LENGTH = 20019 20# Prediction function21def classify_code(code_snippet):22 sequence = tokenizer.texts_to_sequences([code_snippet])23 padded_sequence = pad_sequences(sequence, maxlen=MAX_LENGTH, padding='post')24 prediction = model.predict(padded_sequence)25 predicted_label = label_encoder.inverse_transform([np.argmax(prediction)])26 confidence = np.max(prediction)27 return f"Language: {predicted_label[0]} (Confidence: {confidence:.2f})"28 29# Gradio Interface30title = "Bidirectional GRU Language Classifier"31description = (32 "This app classifies programming languages based on code snippets. "33 "Paste a snippet to see the language and confidence score."34)35 36inputs = gr.Textbox(lines=10, placeholder="Paste your code snippet here...", label="Code Snippet")37outputs = gr.Textbox(label="Prediction")38 39interface = gr.Interface(fn=classify_code, inputs=inputs, outputs=outputs, title=title, description=description)40 41if __name__ == "__main__":42 interface.launch()43 