Infinitode Pty Ltd commited on
Commit
fafc478
·
verified ·
1 Parent(s): 608d8aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -32
app.py CHANGED
@@ -6,7 +6,31 @@ import numpy as np
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()
@@ -83,11 +107,11 @@ def generateNames(type, amount, max_length=30, temperature=0.5, seed_text=""):
83
  interpreter.allocate_tensors()
84
 
85
  # Use the function to generate a name
86
- # Assuming `vocab_size` and `sp` (SentencePiece processor) are defined elsewhere
87
  for _ in range(amount):
88
- generated_name = generate_random_name(interpreter, vocab_size, sp, seed_text=seed_text, max_length=max_length, temperature=temperature)
89
  names.append(generated_name)
90
  return pd.DataFrame(names, columns=['Names'])
 
91
  elif type == "Skyrim":
92
  max_seq_len = 13 # For skyrim = 13, for terraria = 12
93
  sp = spm.SentencePieceProcessor()
@@ -105,9 +129,8 @@ def generateNames(type, amount, max_length=30, temperature=0.5, seed_text=""):
105
  interpreter.allocate_tensors()
106
 
107
  # Use the function to generate a name
108
- # Assuming `vocab_size` and `sp` (SentencePiece processor) are defined elsewhere
109
  for _ in range(amount):
110
- generated_name = generate_random_name(interpreter, vocab_size, sp, seed_text=seed_text, max_length=max_length, temperature=temperature)
111
  names.append(generated_name)
112
  return pd.DataFrame(names, columns=['Names'])
113
 
@@ -119,30 +142,4 @@ demo = gr.Interface(
119
  description='A fun game-inspired name generator. For an example of how to create, and train your model, similar to this one, head over to: https://github.com/infinitode/open-arc/tree/main/project-5-twng/. There you will find our base model, the dataset we used, and implementation code in the form of a Jupyter Notebook (exported from Kaggle).'
120
  )
121
 
122
- demo.launch()
123
-
124
- def custom_pad_sequences(sequences, maxlen, padding='pre', value=0):
125
- """
126
- Pads sequences to the same length.
127
-
128
- :param sequences: List of lists, where each element is a sequence.
129
- :param maxlen: Maximum length of all sequences.
130
- :param padding: 'pre' or 'post', pad either before or after each sequence.
131
- :param value: Float, padding value.
132
- :return: Numpy array with dimensions (number_of_sequences, maxlen)
133
- """
134
- maxlen = max_seq_len
135
-
136
- padded_sequences = np.full((len(sequences), maxlen), value)
137
- for i, seq in enumerate(sequences):
138
- if padding == 'pre':
139
- if len(seq) <= maxlen:
140
- padded_sequences[i, -len(seq):] = seq
141
- else:
142
- padded_sequences[i, :] = seq[-maxlen:]
143
- elif padding == 'post':
144
- if len(seq) <= maxlen:
145
- padded_sequences[i, :len(seq)] = seq
146
- else:
147
- padded_sequences[i, :] = seq[:maxlen]
148
- return padded_sequences
 
6
  import pandas as pd
7
  import tensorflow as tf
8
 
9
+ def custom_pad_sequences(sequences, maxlen, padding='pre', value=0):
10
+ """
11
+ Pads sequences to the same length.
12
+
13
+ :param sequences: List of lists, where each element is a sequence.
14
+ :param maxlen: Maximum length of all sequences.
15
+ :param padding: 'pre' or 'post', pad either before or after each sequence.
16
+ :param value: Float, padding value.
17
+ :return: Numpy array with dimensions (number_of_sequences, maxlen)
18
+ """
19
+ padded_sequences = np.full((len(sequences), maxlen), value)
20
+ for i, seq in enumerate(sequences):
21
+ if padding == 'pre':
22
+ if len(seq) <= maxlen:
23
+ padded_sequences[i, -len(seq):] = seq
24
+ else:
25
+ padded_sequences[i, :] = seq[-maxlen:]
26
+ elif padding == 'post':
27
+ if len(seq) <= maxlen:
28
+ padded_sequences[i, :len(seq)] = seq
29
+ else:
30
+ padded_sequences[i, :] = seq[:maxlen]
31
+ return padded_sequences
32
+
33
+ def generate_random_name(interpreter, vocab_size, sp, max_length=10, temperature=0.5, seed_text="", max_seq_len=12):
34
  # Get input and output tensors
35
  input_details = interpreter.get_input_details()
36
  output_details = interpreter.get_output_details()
 
107
  interpreter.allocate_tensors()
108
 
109
  # Use the function to generate a name
 
110
  for _ in range(amount):
111
+ generated_name = generate_random_name(interpreter, vocab_size, sp, seed_text=seed_text, max_length=max_length, temperature=temperature, max_seq_len=max_seq_len)
112
  names.append(generated_name)
113
  return pd.DataFrame(names, columns=['Names'])
114
+
115
  elif type == "Skyrim":
116
  max_seq_len = 13 # For skyrim = 13, for terraria = 12
117
  sp = spm.SentencePieceProcessor()
 
129
  interpreter.allocate_tensors()
130
 
131
  # Use the function to generate a name
 
132
  for _ in range(amount):
133
+ generated_name = generate_random_name(interpreter, vocab_size, sp, seed_text=seed_text, max_length=max_length, temperature=temperature, max_seq_len=max_seq_len)
134
  names.append(generated_name)
135
  return pd.DataFrame(names, columns=['Names'])
136
 
 
142
  description='A fun game-inspired name generator. For an example of how to create, and train your model, similar to this one, head over to: https://github.com/infinitode/open-arc/tree/main/project-5-twng/. There you will find our base model, the dataset we used, and implementation code in the form of a Jupyter Notebook (exported from Kaggle).'
143
  )
144
 
145
+ demo.launch()