Spaces:
Runtime error

File size: 3,505 Bytes
f7eeef2
53f446d
 
2d4bf46
bf7abce
 
9c5e1b2
 
a9dc1d3
82ee3cd
ff44835
9c5e1b2
0ffe492
9e09c58
bf7abce
4ba8865
9e09c58
c8e1c9b
36df212
87f5b9a
36df212
 
 
 
 
 
 
53f446d
64b639a
 
 
236afa2
53f446d
8aa9af0
 
d970d6a
82ee3cd
 
 
11eb4b4
aa9bcdd
11eb4b4
 
 
86f2198
11eb4b4
86f2198
11eb4b4
86f2198
 
11eb4b4
 
86f2198
11eb4b4
 
86f2198
11eb4b4
 
86f2198
11eb4b4
 
86f2198
11eb4b4
86f2198
2d4bf46
26ec57b
2d4bf46
d8e1513
2d4bf46
6d759e0
 
2d4bf46
 
6d759e0
2d4bf46
 
 
 
 
6d759e0
2d4bf46
 
53f446d
e6879e2
4c7d064
2dc9b23
 
 
a7279a6
53f446d
 
d970d6a
53f446d
aa9bcdd
166f698
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#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."),