Spaces:
Sleeping
Sleeping
Infinitode Pty Ltd
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,65 @@ import numpy as np
|
|
| 6 |
import pandas as pd
|
| 7 |
import tensorflow as tf
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def generateNames(type, amount, max_length=30, temperature=0.5, seed_text=""):
|
| 10 |
if type == "Terraria":
|
| 11 |
max_seq_len = 12 # For skyrim = 13, for terraria = 12
|
|
@@ -86,63 +145,4 @@ def custom_pad_sequences(sequences, maxlen, padding='pre', value=0):
|
|
| 86 |
padded_sequences[i, :len(seq)] = seq
|
| 87 |
else:
|
| 88 |
padded_sequences[i, :] = seq[:maxlen]
|
| 89 |
-
return padded_sequences
|
| 90 |
-
|
| 91 |
-
def generate_random_name(interpreter, vocab_size, sp, max_length=10, temperature=0.5, seed_text=""):
|
| 92 |
-
# Get input and output tensors
|
| 93 |
-
input_details = interpreter.get_input_details()
|
| 94 |
-
output_details = interpreter.get_output_details()
|
| 95 |
-
|
| 96 |
-
if seed_text:
|
| 97 |
-
generated_name = seed_text
|
| 98 |
-
else:
|
| 99 |
-
random_index = np.random.randint(1, vocab_size)
|
| 100 |
-
random_token = sp.id_to_piece(random_index)
|
| 101 |
-
generated_name = random_token
|
| 102 |
-
|
| 103 |
-
for _ in range(max_length - 1):
|
| 104 |
-
token_list = sp.encode_as_ids(generated_name)
|
| 105 |
-
# Pad to the correct length expected by the model
|
| 106 |
-
token_list = custom_pad_sequences([token_list], maxlen=max_seq_len, padding='pre')
|
| 107 |
-
|
| 108 |
-
# Convert token_list to FLOAT32 before setting the tensor
|
| 109 |
-
token_list = token_list.astype(np.float32)
|
| 110 |
-
|
| 111 |
-
# Set the input tensor
|
| 112 |
-
interpreter.set_tensor(input_details[0]['index'], token_list)
|
| 113 |
-
|
| 114 |
-
# Run inference
|
| 115 |
-
interpreter.invoke()
|
| 116 |
-
|
| 117 |
-
# Get the output tensor
|
| 118 |
-
predicted = interpreter.get_tensor(output_details[0]['index'])[0]
|
| 119 |
-
|
| 120 |
-
# Apply temperature to predictions
|
| 121 |
-
predicted = np.log(predicted + 1e-8) / temperature
|
| 122 |
-
predicted = np.exp(predicted) / np.sum(np.exp(predicted))
|
| 123 |
-
|
| 124 |
-
# Sample from the distribution
|
| 125 |
-
next_index = np.random.choice(range(vocab_size), p=predicted)
|
| 126 |
-
next_index = int(next_index)
|
| 127 |
-
next_token = sp.id_to_piece(next_index)
|
| 128 |
-
|
| 129 |
-
generated_name += next_token
|
| 130 |
-
|
| 131 |
-
# Decode the generated subword tokens into a string
|
| 132 |
-
decoded_name = sp.decode_pieces(generated_name.split())
|
| 133 |
-
|
| 134 |
-
# Stop if end token is predicted (optional)
|
| 135 |
-
if next_token == '' or len(decoded_name) > max_length:
|
| 136 |
-
break
|
| 137 |
-
|
| 138 |
-
decoded_name = decoded_name.replace("▁", " ")
|
| 139 |
-
decoded_name = decoded_name.replace("</s>", "")
|
| 140 |
-
generated_name = decoded_name.rsplit(' ', 1)[0]
|
| 141 |
-
generated_name = generated_name[0].upper() + generated_name[1:]
|
| 142 |
-
|
| 143 |
-
# Split the name and check the last part
|
| 144 |
-
parts = generated_name.split()
|
| 145 |
-
if parts and len(parts[-1]) < 3:
|
| 146 |
-
generated_name = " ".join(parts[:-1])
|
| 147 |
-
|
| 148 |
-
return generated_name.strip()
|
|
|
|
| 6 |
import pandas as pd
|
| 7 |
import tensorflow as tf
|
| 8 |
|
| 9 |
+
def generate_random_name(interpreter, vocab_size, sp, max_length=10, temperature=0.5, seed_text=""):
|
| 10 |
+
# Get input and output tensors
|
| 11 |
+
input_details = interpreter.get_input_details()
|
| 12 |
+
output_details = interpreter.get_output_details()
|
| 13 |
+
|
| 14 |
+
if seed_text:
|
| 15 |
+
generated_name = seed_text
|
| 16 |
+
else:
|
| 17 |
+
random_index = np.random.randint(1, vocab_size)
|
| 18 |
+
random_token = sp.id_to_piece(random_index)
|
| 19 |
+
generated_name = random_token
|
| 20 |
+
|
| 21 |
+
for _ in range(max_length - 1):
|
| 22 |
+
token_list = sp.encode_as_ids(generated_name)
|
| 23 |
+
# Pad to the correct length expected by the model
|
| 24 |
+
token_list = custom_pad_sequences([token_list], maxlen=max_seq_len, padding='pre')
|
| 25 |
+
|
| 26 |
+
# Convert token_list to FLOAT32 before setting the tensor
|
| 27 |
+
token_list = token_list.astype(np.float32)
|
| 28 |
+
|
| 29 |
+
# Set the input tensor
|
| 30 |
+
interpreter.set_tensor(input_details[0]['index'], token_list)
|
| 31 |
+
|
| 32 |
+
# Run inference
|
| 33 |
+
interpreter.invoke()
|
| 34 |
+
|
| 35 |
+
# Get the output tensor
|
| 36 |
+
predicted = interpreter.get_tensor(output_details[0]['index'])[0]
|
| 37 |
+
|
| 38 |
+
# Apply temperature to predictions
|
| 39 |
+
predicted = np.log(predicted + 1e-8) / temperature
|
| 40 |
+
predicted = np.exp(predicted) / np.sum(np.exp(predicted))
|
| 41 |
+
|
| 42 |
+
# Sample from the distribution
|
| 43 |
+
next_index = np.random.choice(range(vocab_size), p=predicted)
|
| 44 |
+
next_index = int(next_index)
|
| 45 |
+
next_token = sp.id_to_piece(next_index)
|
| 46 |
+
|
| 47 |
+
generated_name += next_token
|
| 48 |
+
|
| 49 |
+
# Decode the generated subword tokens into a string
|
| 50 |
+
decoded_name = sp.decode_pieces(generated_name.split())
|
| 51 |
+
|
| 52 |
+
# Stop if end token is predicted (optional)
|
| 53 |
+
if next_token == '' or len(decoded_name) > max_length:
|
| 54 |
+
break
|
| 55 |
+
|
| 56 |
+
decoded_name = decoded_name.replace("▁", " ")
|
| 57 |
+
decoded_name = decoded_name.replace("</s>", "")
|
| 58 |
+
generated_name = decoded_name.rsplit(' ', 1)[0]
|
| 59 |
+
generated_name = generated_name[0].upper() + generated_name[1:]
|
| 60 |
+
|
| 61 |
+
# Split the name and check the last part
|
| 62 |
+
parts = generated_name.split()
|
| 63 |
+
if parts and len(parts[-1]) < 3:
|
| 64 |
+
generated_name = " ".join(parts[:-1])
|
| 65 |
+
|
| 66 |
+
return generated_name.strip()
|
| 67 |
+
|
| 68 |
def generateNames(type, amount, max_length=30, temperature=0.5, seed_text=""):
|
| 69 |
if type == "Terraria":
|
| 70 |
max_seq_len = 12 # For skyrim = 13, for terraria = 12
|
|
|
|
| 145 |
padded_sequences[i, :len(seq)] = seq
|
| 146 |
else:
|
| 147 |
padded_sequences[i, :] = seq[:maxlen]
|
| 148 |
+
return padded_sequences
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|