JohanBeytell commited on
Commit
664181d
·
verified ·
1 Parent(s): 610b4ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -17,10 +17,10 @@ def generate_text(seed_text, next_words=30, temperature=0.5):
17
  hate_speech = detect_hate_speech(seed_text)
18
  profanity = detect_profanity([seed_text], language='All')
19
 
20
- if profanity: # Simplified check
21
  gr.Warning("Profanity detected in the prompt, using the default prompt.")
22
  seed_text = 'game name | '
23
- elif hate_speech and hate_speech[0] in ['Hate Speech', 'Offensive Speech']: # Simplified check
24
  gr.Warning('Harmful speech detected in the seed text, using default prompt.')
25
  seed_text = 'game name | '
26
 
@@ -30,9 +30,8 @@ def generate_text(seed_text, next_words=30, temperature=0.5):
30
  token_list = pad_sequences([token_list], maxlen=max_seq_len - 1, padding='pre')
31
  predicted = model.predict(token_list, verbose=0)[0]
32
 
33
- # Apply temperature with numerical stability fix
34
  predicted = np.asarray(predicted).astype("float64")
35
- predicted = np.log(predicted + 1e-8) / temperature # Add small epsilon to prevent log(0)
36
  exp_preds = np.exp(predicted)
37
  predicted = exp_preds / np.sum(exp_preds)
38
 
@@ -44,12 +43,16 @@ def generate_text(seed_text, next_words=30, temperature=0.5):
44
  break
45
 
46
  decoded = sp.decode_pieces(sp.encode_as_pieces(generated_text))
47
- decoded = decoded.replace("</s>", "").replace("<unk>", "").strip() # Combined replace and strip
48
-
 
 
 
 
49
  hate_speech2 = detect_hate_speech(decoded)
50
  profanity2 = detect_profanity([decoded], language='All')
51
-
52
- if profanity2 or (hate_speech2 and hate_speech2[0] in ['Hate Speech', 'Offensive Speech']): #Combined check
53
  gr.Warning("Flagged potentially harmful output.")
54
  decoded = 'Flagged Output'
55
 
@@ -59,12 +62,19 @@ demo = gr.Interface(
59
  fn=generate_text,
60
  inputs=[
61
  gr.Textbox(label="Prompt", value="a female character name", max_lines=1),
62
- gr.Slider(1, 100, step=1, label='Next Words', value=30),
63
  gr.Slider(0.1, 1, value=0.5, label='Temperature', info='Controls randomness of generation, higher values = more creative, lower values = more probalistic')
64
  ],
65
- outputs=gr.Textbox(label="Generated Names"), #Simplified output
66
  title='Dungen Dev - Name Generator',
67
- description='A prompt-based name generator for game developers.'
 
 
 
 
 
 
 
68
  )
69
 
70
  demo.launch()
 
17
  hate_speech = detect_hate_speech(seed_text)
18
  profanity = detect_profanity([seed_text], language='All')
19
 
20
+ if profanity:
21
  gr.Warning("Profanity detected in the prompt, using the default prompt.")
22
  seed_text = 'game name | '
23
+ elif hate_speech and hate_speech[0] in ['Hate Speech', 'Offensive Speech']:
24
  gr.Warning('Harmful speech detected in the seed text, using default prompt.')
25
  seed_text = 'game name | '
26
 
 
30
  token_list = pad_sequences([token_list], maxlen=max_seq_len - 1, padding='pre')
31
  predicted = model.predict(token_list, verbose=0)[0]
32
 
 
33
  predicted = np.asarray(predicted).astype("float64")
34
+ predicted = np.log(predicted + 1e-8) / temperature
35
  exp_preds = np.exp(predicted)
36
  predicted = exp_preds / np.sum(exp_preds)
37
 
 
43
  break
44
 
45
  decoded = sp.decode_pieces(sp.encode_as_pieces(generated_text))
46
+ decoded = decoded.replace("</s>", "").replace("<unk>", "").strip()
47
+
48
+ # Remove the prompt from the generated text
49
+ if '|' in decoded:
50
+ decoded = decoded.split('|', 1)[1].strip() #Split at the first occurence of '|' and take the second part
51
+
52
  hate_speech2 = detect_hate_speech(decoded)
53
  profanity2 = detect_profanity([decoded], language='All')
54
+
55
+ if profanity2 or (hate_speech2 and hate_speech2[0] in ['Hate Speech', 'Offensive Speech']):
56
  gr.Warning("Flagged potentially harmful output.")
57
  decoded = 'Flagged Output'
58
 
 
62
  fn=generate_text,
63
  inputs=[
64
  gr.Textbox(label="Prompt", value="a female character name", max_lines=1),
65
+ gr.Slider(1, 50, step=1, label='Next Words', value=30),
66
  gr.Slider(0.1, 1, value=0.5, label='Temperature', info='Controls randomness of generation, higher values = more creative, lower values = more probalistic')
67
  ],
68
+ outputs=gr.Textbox(label="Generated Names"),
69
  title='Dungen Dev - Name Generator',
70
+ description='A prompt-based name generator for game developers.',
71
+ examples=[
72
+ ["a male elf name"],
73
+ ["a futuristic city name"],
74
+ ["a powerful magic item name"],
75
+ ["a dark and mysterious forest name"],
76
+ ["a female character name"]
77
+ ]
78
  )
79
 
80
  demo.launch()