Spaces:
Build error
Build error
Update app.py
Browse files6 unmodified lines7891011121314151617182 unmodified lines2122232425262728293031323334353637381 unmodified line4041424344454647484950513 unmodified lines5556575859606 unmodified lines676869707172736 unmodified linesfrom tensorflow.keras.preprocessing.sequence import pad_sequencesfrom tensorflow.keras.preprocessing.text import tokenizer_from_json# Load the Keras modeldef load_model():model = tf.keras.models.load_model('polarisatie_model.h5')return modelmodel = load_model()# Load the tokenizer from tokenizer.jsondef load_tokenizer(tokenizer_path='tokenizer.json'):2 unmodified linestokenizer = tokenizer_from_json(tokenizer_json)return tokenizertokenizer = load_tokenizer()# Load max_length from max_length.txtdef load_max_length(max_length_path='max_length.txt'):with open(max_length_path, 'r') as f:max_length = int(f.read().strip())return max_lengthmax_length = load_max_length()# Preprocessing functiondef preprocess(text):# Tokenize the textsequences = tokenizer.texts_to_sequences([text])# Pad the sequences1 unmodified linereturn padded# Prediction functiondef predict_polarization(text):if not text.strip():return "Voer alstublieft een geldige zin in."# Preprocess the textprocessed_text = preprocess(text)# Make predictionprediction = model.predict(processed_text)3 unmodified linesreturn "Ja" if is_polarizing else "Nee"# Define the Gradio interface using Blockswith gr.Blocks() as demo:gr.Markdown("# Polarisatie Thermometer")6 unmodified linesresult = gr.Textbox(label="Is polariserend?", interactive=False)evaluate_button.click(fn=predict_polarization, inputs=input_text, outputs=result)# Launch the Gradio appif __name__ == "__main__":6 unmodified lines78910111213141516171819202122232 unmodified lines26272829303132333435363738391 unmodified line4142434445464748495051523 unmodified lines5657585960616263646566676 unmodified lines74757677787980818283846 unmodified linesfrom tensorflow.keras.preprocessing.sequence import pad_sequencesfrom tensorflow.keras.preprocessing.text import tokenizer_from_json# Load the Keras model architecture from model.jsondef load_model_architecture(model_json_path='model.json'):with open(model_json_path, 'r', encoding='utf-8') as json_file:model_json = json_file.read()model = tf.keras.models.model_from_json(model_json)return model# Load the model weights from polarisatie_model.h5def load_model_weights(model, weights_path='polarisatie_model.h5'):model.load_weights(weights_path)return model# Load the tokenizer from tokenizer.jsondef load_tokenizer(tokenizer_path='tokenizer.json'):2 unmodified linestokenizer = tokenizer_from_json(tokenizer_json)return tokenizer# Load max_length from max_length.txtdef load_max_length(max_length_path='max_length.txt'):with open(max_length_path, 'r') as f:max_length = int(f.read().strip())return max_length# Preprocessing functiondef preprocess(text, tokenizer, max_length):# Tokenize the textsequences = tokenizer.texts_to_sequences([text])# Pad the sequences1 unmodified linereturn padded# Prediction functiondef predict_polarization(text, model, tokenizer, max_length):if not text.strip():return "Voer alstublieft een geldige zin in."# Preprocess the textprocessed_text = preprocess(text, tokenizer, max_length)# Make predictionprediction = model.predict(processed_text)3 unmodified linesreturn "Ja" if is_polarizing else "Nee"# Load all componentsmodel = load_model_architecture()model = load_model_weights(model)tokenizer = load_tokenizer()max_length = load_max_length()# Define the Gradio interface using Blockswith gr.Blocks() as demo:gr.Markdown("# Polarisatie Thermometer")6 unmodified linesresult = gr.Textbox(label="Is polariserend?", interactive=False)evaluate_button.click(fn=lambda text: predict_polarization(text, model, tokenizer, max_length),inputs=input_text,outputs=result)# Launch the Gradio appif __name__ == "__main__":
app.py+23-12