Infinitode Pty Ltd commited on
Commit
d4c1f23
·
verified ·
1 Parent(s): 349b488

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -57
app.py CHANGED
@@ -6,6 +6,62 @@ import numpy as np
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.
@@ -89,60 +145,4 @@ def generate_random_name(interpreter, vocab_size, sp, max_length=10, temperature
89
  if parts and len(parts[-1]) < 3:
90
  generated_name = " ".join(parts[:-1])
91
 
92
- return generated_name.strip()
93
-
94
- def generateNames(type, amount, max_length=30, temperature=0.5, seed_text=""):
95
- if type == "Terraria":
96
- max_seq_len = 12 # For skyrim = 13, for terraria = 12
97
- sp = spm.SentencePieceProcessor()
98
- sp.load("models/terraria_names.model")
99
- amount = int(amount)
100
- max_length = int(max_length)
101
-
102
- names = []
103
-
104
- # Define necessary variables
105
- vocab_size = sp.GetPieceSize()
106
-
107
- # Load TFLite model
108
- interpreter = tf.lite.Interpreter(model_path="models/dungen_terraria_model.tflite")
109
- interpreter.allocate_tensors()
110
-
111
- # Use the function to generate a name
112
- # Assuming `vocab_size` and `sp` (SentencePiece processor) are defined elsewhere
113
- for _ in range(amount):
114
- generated_name = generate_random_name(interpreter, vocab_size, sp, seed_text=seed_text, max_length=max_length, temperature=temperature)
115
- names.append(generated_name)
116
- return pd.DataFrame(names, columns=['Names'])
117
- elif type == "Skyrim":
118
- max_seq_len = 13 # For skyrim = 13, for terraria = 12
119
- sp = spm.SentencePieceProcessor()
120
- sp.load("models/skyrim_names.model")
121
- amount = int(amount)
122
- max_length = int(max_length)
123
-
124
- names = []
125
-
126
- # Define necessary variables
127
- vocab_size = sp.GetPieceSize()
128
-
129
- # Load TFLite model
130
- interpreter = tf.lite.Interpreter(model_path="models/dungen_skyrim_model.tflite")
131
- interpreter.allocate_tensors()
132
-
133
- # Use the function to generate a name
134
- # Assuming `vocab_size` and `sp` (SentencePiece processor) are defined elsewhere
135
- for _ in range(amount):
136
- generated_name = generate_random_name(interpreter, vocab_size, sp, seed_text=seed_text, max_length=max_length, temperature=temperature)
137
- names.append(generated_name)
138
- return pd.DataFrame(names, columns=['Names'])
139
-
140
- demo = gr.Interface(
141
- fn=generateNames,
142
- inputs=[gr.Radio(choices=["Terraria", "Skyrim"], label="Choose a model for your request"), gr.Slider(1,25, step=1, label='Amount of Names', info='How many names to generate, must be greater than 0'), gr.Slider(10, 60, value=30, step=1, label='Max Length', info='Max length of the generated word'), gr.Slider(0.1, 1, value=0.5, label='Temperature', info='Controls randomness of generation, higher values = more creative, lower values = more probalistic'), gr.Textbox('', label='Seed text (optional)', info='The starting text to begin with', max_lines=1, )],
143
- outputs=[gr.Dataframe(row_count = (2, "dynamic"), col_count=(1, "fixed"), label="Generated Names", headers=["Names"])],
144
- title='Dungen - Name Generator',
145
- 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).'
146
- )
147
-
148
- demo.launch()
 
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
12
+ sp = spm.SentencePieceProcessor()
13
+ sp.load("models/terraria_names.model")
14
+ amount = int(amount)
15
+ max_length = int(max_length)
16
+
17
+ names = []
18
+
19
+ # Define necessary variables
20
+ vocab_size = sp.GetPieceSize()
21
+
22
+ # Load TFLite model
23
+ interpreter = tf.lite.Interpreter(model_path="models/dungen_terraria_model.tflite")
24
+ interpreter.allocate_tensors()
25
+
26
+ # Use the function to generate a name
27
+ # Assuming `vocab_size` and `sp` (SentencePiece processor) are defined elsewhere
28
+ for _ in range(amount):
29
+ generated_name = generate_random_name(interpreter, vocab_size, sp, seed_text=seed_text, max_length=max_length, temperature=temperature)
30
+ names.append(generated_name)
31
+ return pd.DataFrame(names, columns=['Names'])
32
+ elif type == "Skyrim":
33
+ max_seq_len = 13 # For skyrim = 13, for terraria = 12
34
+ sp = spm.SentencePieceProcessor()
35
+ sp.load("models/skyrim_names.model")
36
+ amount = int(amount)
37
+ max_length = int(max_length)
38
+
39
+ names = []
40
+
41
+ # Define necessary variables
42
+ vocab_size = sp.GetPieceSize()
43
+
44
+ # Load TFLite model
45
+ interpreter = tf.lite.Interpreter(model_path="models/dungen_skyrim_model.tflite")
46
+ interpreter.allocate_tensors()
47
+
48
+ # Use the function to generate a name
49
+ # Assuming `vocab_size` and `sp` (SentencePiece processor) are defined elsewhere
50
+ for _ in range(amount):
51
+ generated_name = generate_random_name(interpreter, vocab_size, sp, seed_text=seed_text, max_length=max_length, temperature=temperature)
52
+ names.append(generated_name)
53
+ return pd.DataFrame(names, columns=['Names'])
54
+
55
+ demo = gr.Interface(
56
+ fn=generateNames,
57
+ inputs=[gr.Radio(choices=["Terraria", "Skyrim"], label="Choose a model for your request"), gr.Slider(1,25, step=1, label='Amount of Names', info='How many names to generate, must be greater than 0'), gr.Slider(10, 60, value=30, step=1, label='Max Length', info='Max length of the generated word'), gr.Slider(0.1, 1, value=0.5, label='Temperature', info='Controls randomness of generation, higher values = more creative, lower values = more probalistic'), gr.Textbox('', label='Seed text (optional)', info='The starting text to begin with', max_lines=1, )],
58
+ outputs=[gr.Dataframe(row_count = (2, "dynamic"), col_count=(1, "fixed"), label="Generated Names", headers=["Names"])],
59
+ title='Dungen - Name Generator',
60
+ 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).'
61
+ )
62
+
63
+ demo.launch()
64
+
65
  def custom_pad_sequences(sequences, maxlen, padding='pre', value=0):
66
  """
67
  Pads sequences to the same length.
 
145
  if parts and len(parts[-1]) < 3:
146
  generated_name = " ".join(parts[:-1])
147
 
148
+ return generated_name.strip()