Zahnanni's picture
Update app.py
d8e1513
Raw History Blame Contribute Delete
3.51 kB
#import pickle
import gradio as gr
import tensorflow as tf
import numpy as np
import json
#import nltk
#from sklearn.model_selection import train_test_split
#from sklearn.preprocessing import LabelEncoder
from tensorflow import keras
from keras.preprocessing.text import Tokenizer
from keras.models import load_model
#from keras.models import Sequential
#from keras.layers import Embedding, LSTM, Dense
from tensorflow.keras.preprocessing.sequence import pad_sequences
#from nltk.tokenize.simple import CharTokenizer
tokenizer = Tokenizer(char_level=True)
tokenizer_json = "tokenizer.json" # Replace with the path to your tokenizer configuration
# Load the tokenizer configuration from the JSON file
with open(tokenizer_json, "r") as json_file:
tokenizer_config = json.load(json_file)
# Manually set word_index and index_word from the 'config' dictionary
tokenizer.word_index = tokenizer_config['config']['word_index']
tokenizer.index_word = tokenizer_config['config']['index_word']
#with open("word_guessing.pkl", "rb") as f:
# model_standardform = pickle.load(f)
model_standardform = keras.models.load_model("FINALword_guessing.h5")
model_dialect = keras.models.load_model("dialect_guessing.h5")
#tokenizer = Tokenizer()
#def prediction(input):
#output_standardform = model_standardform.predict(input)
#output_dialect = model_dialect.predict(input)
#return(output_dialect)
#def predict_dialect(input_word):
# Tokenize the input word
# input_sequence = tokenizer.texts_to_sequences([input_word])
# if not input_sequence or not input_sequence[0]:
# return "Word not in vocabulary"
# Make a prediction using the model
# predicted_indices = model_dialect.predict(input_sequence)
# Find the index with the highest probability
#predicted_index = predicted_indices.argmax()
# Define a list of dialect labels
#dialect_labels = ["Dialect1", "Dialect2", "Dialect3"] # Customize as needed
# Get the predicted dialect label
#predicted_dialect = dialect_labels[predicted_index]
#return predicted_dialect
def prediction(input_word):
# Tokenize the input word
input_sequence = tokenizer.texts_to_sequences([input_word])[0]
if not input_sequence or not input_sequence[0]:
return "Word not in vocabulary"
# Make a prediction using the model
predicted_indices = model_standardform.predict(np.array(input_sequence))
# Find the index with the highest probability
predicted_index = np.argmax(predicted_indices)
# Convert the predicted index back to a word using the tokenizer
predicted_word = tokenizer.index_word.get(predicted_index, "Word not in vocabulary")
return predicted_word
desc = "Have you encountered a Finnish dialect or slang word and you cannot find it in a dictionary? This translator predicts the Finnish standard form of the word to enable you to check those words in your usual Finnish sources! Try it out! But, it needs to be stated that not all predictions will be right, although maybe still helpful..."
#def testfunction(input):
# output = input.lower()
# return(output)
demo = gr.Interface(fn=prediction , inputs = gr.Textbox(placeholder="Write a Finnish slang or dialct word here."), outputs = gr.Textbox(placeholder="This is the predicted Finnish standard form of the word."), title = "Local Finnish Translator", description = desc)
demo.launch()
#gr.Textbox(placeholder="This is the predicted location where the word may be used."),