JohanBeytell commited on
Commit
f785ebe
·
verified ·
1 Parent(s): 3711b10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -28,13 +28,13 @@ MODEL_CONFIGS = {
28
  # Global dictionary to store loaded models in memory
29
  MODEL_CACHE = {}
30
 
31
- def get_loaded_models(game_type):
32
  """
33
  Lazy-loads models into memory. If the model is already in the cache,
34
  it returns it instantly. Otherwise, it loads it from disk, caches it, and returns it.
35
  """
36
- if game_type not in MODEL_CACHE:
37
- file_prefix = game_type.lower().replace(" ", "_").replace("'", "")
38
 
39
  # Load SentencePiece Model
40
  sp = spm.SentencePieceProcessor()
@@ -45,13 +45,13 @@ def get_loaded_models(game_type):
45
  interpreter.allocate_tensors()
46
 
47
  # Store in cache
48
- MODEL_CACHE[game_type] = {
49
  "sp": sp,
50
  "interpreter": interpreter,
51
  "vocab_size": sp.GetPieceSize()
52
  }
53
 
54
- return MODEL_CACHE[game_type]
55
 
56
  def custom_pad_sequences(sequences, maxlen, padding='pre', value=0):
57
  padded_sequences = np.full((len(sequences), maxlen), value)
@@ -125,7 +125,8 @@ def generate_random_name(interpreter, vocab_size, sp, max_length=10, temperature
125
 
126
  return normalized_name.strip()
127
 
128
- def generateNames(game_type, amount, max_length=30, temperature=0.5, seed_text=""):
 
129
  hate_speech = detect_hate_speech(seed_text)
130
  profanity = detect_profanity([seed_text], language='All')
131
 
@@ -140,14 +141,14 @@ def generateNames(game_type, amount, max_length=30, temperature=0.5, seed_text="
140
  gr.Warning('Offensive speech detected in the seed text, using an empty seed text.')
141
  seed_text = ''
142
 
143
- if game_type not in MODEL_CONFIGS:
144
  return pd.DataFrame([], columns=['Names'])
145
 
146
  # Fetch max sequence length
147
- max_seq_len = MODEL_CONFIGS[game_type]
148
 
149
  # Fetch cached models (loads them instantly if already cached)
150
- cached_data = get_loaded_models(game_type)
151
  sp = cached_data["sp"]
152
  interpreter = cached_data["interpreter"]
153
  vocab_size = cached_data["vocab_size"]
@@ -200,10 +201,15 @@ demo = gr.Interface(
200
  gr.Slider(0.1, 1, value=0.5, label='Temperature', info='Controls randomness of generation, higher values = more creative, lower values = more probalistic'),
201
  gr.Textbox('', label='Seed text (optional)', info='The starting text to begin with', max_lines=1)
202
  ],
203
- outputs=[gr.Dataframe(row_count=(2, "dynamic"), column_count=(1, "fixed"), label="Generated Names", headers=["Names"])],
 
204
  title='Dungen - Name Generator',
205
- description='A fun game-inspired name generator. For an example of how to create, and train your model, like 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). Use on the web: https://infinitode.netlify.app/experiments/dungen-ai.'
 
 
 
206
  )
207
 
208
  if __name__ == "__main__":
209
- demo.launch()
 
 
28
  # Global dictionary to store loaded models in memory
29
  MODEL_CACHE = {}
30
 
31
+ def get_loaded_models(game_identifier):
32
  """
33
  Lazy-loads models into memory. If the model is already in the cache,
34
  it returns it instantly. Otherwise, it loads it from disk, caches it, and returns it.
35
  """
36
+ if game_identifier not in MODEL_CACHE:
37
+ file_prefix = game_identifier.lower().replace(" ", "_").replace("'", "")
38
 
39
  # Load SentencePiece Model
40
  sp = spm.SentencePieceProcessor()
 
45
  interpreter.allocate_tensors()
46
 
47
  # Store in cache
48
+ MODEL_CACHE[game_identifier] = {
49
  "sp": sp,
50
  "interpreter": interpreter,
51
  "vocab_size": sp.GetPieceSize()
52
  }
53
 
54
+ return MODEL_CACHE[game_identifier]
55
 
56
  def custom_pad_sequences(sequences, maxlen, padding='pre', value=0):
57
  padded_sequences = np.full((len(sequences), maxlen), value)
 
125
 
126
  return normalized_name.strip()
127
 
128
+ # Note: Preserving the exact parameter names (like 'type') to ensure the API contract remains unbroken
129
+ def generateNames(type, amount, max_length=30, temperature=0.5, seed_text=""):
130
  hate_speech = detect_hate_speech(seed_text)
131
  profanity = detect_profanity([seed_text], language='All')
132
 
 
141
  gr.Warning('Offensive speech detected in the seed text, using an empty seed text.')
142
  seed_text = ''
143
 
144
+ if type not in MODEL_CONFIGS:
145
  return pd.DataFrame([], columns=['Names'])
146
 
147
  # Fetch max sequence length
148
+ max_seq_len = MODEL_CONFIGS[type]
149
 
150
  # Fetch cached models (loads them instantly if already cached)
151
+ cached_data = get_loaded_models(type)
152
  sp = cached_data["sp"]
153
  interpreter = cached_data["interpreter"]
154
  vocab_size = cached_data["vocab_size"]
 
201
  gr.Slider(0.1, 1, value=0.5, label='Temperature', info='Controls randomness of generation, higher values = more creative, lower values = more probalistic'),
202
  gr.Textbox('', label='Seed text (optional)', info='The starting text to begin with', max_lines=1)
203
  ],
204
+ # Removed row_count and column_count to prevent the dataframe from aggressively paginating or limiting the array visualization
205
+ outputs=[gr.Dataframe(label="Generated Names", headers=["Names"])],
206
  title='Dungen - Name Generator',
207
+ description=(
208
+ "A fun game-inspired name generator. For an example of how to create, and train your model, like 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).\n\n"
209
+ "Try Dungen online: [Dungen AI | Advanced Neural Name Generator](https://infinitode.netlify.app/experiments/dungen-ai/)"
210
+ )
211
  )
212
 
213
  if __name__ == "__main__":
214
+ # Added ssr_mode=False to bypass the new Gradio 5 Async loop teardown bug
215
+ demo.launch(ssr_mode=False)