JohanBeytell commited on
Commit
44ea375
·
verified ·
1 Parent(s): 0b0a0da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -4
app.py CHANGED
@@ -1,9 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import sentencepiece as spm
2
  import numpy as np
3
  import tensorflow as tf
4
  from tensorflow.keras.preprocessing.sequence import pad_sequences
5
  from valx import detect_profanity, detect_hate_speech
6
  import gradio as gr
 
 
7
 
8
  sp = spm.SentencePieceProcessor()
9
  sp.Load("dungen_dev_preview.model")
@@ -28,7 +110,7 @@ def generate_text(seed_text, next_words=30, temperature=0.5):
28
  seed_text += ' | '
29
 
30
  generated_text = seed_text
31
- if generated_text != 'game name | ': # only generate if not the default prompt
32
  for _ in range(next_words):
33
  token_list = sp.encode_as_ids(generated_text)
34
  token_list = pad_sequences([token_list], maxlen=max_seq_len - 1, padding='pre')
@@ -58,16 +140,62 @@ def generate_text(seed_text, next_words=30, temperature=0.5):
58
 
59
  return decoded
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  demo = gr.Interface(
62
  fn=generate_text,
63
  inputs=[
64
  gr.Textbox(label="Prompt", value="a female character name", max_lines=1),
65
  gr.Slider(1, 100, 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. Dungen Dev is an experimental model, and may produce outputs that are inappropriate, biased, or potentially harmful and inaccurate. Caution is advised.',
71
  examples=[
72
  ["a male character name", 30, 0.5],
73
  ["a futuristic city name", 30, 0.5],
 
1
+ # import sentencepiece as spm
2
+ # import numpy as np
3
+ # import tensorflow as tf
4
+ # from tensorflow.keras.preprocessing.sequence import pad_sequences
5
+ # from valx import detect_profanity, detect_hate_speech
6
+ # import gradio as gr
7
+
8
+ # sp = spm.SentencePieceProcessor()
9
+ # sp.Load("dungen_dev_preview.model")
10
+
11
+ # model = tf.keras.models.load_model("dungen_dev_preview_model.keras")
12
+
13
+ # max_seq_len = 25
14
+
15
+ # def generate_text(seed_text, next_words=30, temperature=0.5):
16
+ # seed_text = seed_text.strip().lower()
17
+
18
+ # if "|" in seed_text:
19
+ # gr.Warning("The prompt should not contain the '|' character. Using default prompt.")
20
+ # seed_text = 'game name | '
21
+ # elif detect_profanity([seed_text], language='All'):
22
+ # gr.Warning("Profanity detected in the prompt, using the default prompt.")
23
+ # seed_text = 'game name | '
24
+ # elif (hate_speech_result := detect_hate_speech(seed_text)) and hate_speech_result[0] in ['Hate Speech', 'Offensive Speech']:
25
+ # gr.Warning('Harmful speech detected in the prompt, using default prompt.')
26
+ # seed_text = 'game name | '
27
+ # else:
28
+ # seed_text += ' | '
29
+
30
+ # generated_text = seed_text
31
+ # if generated_text != 'game name | ': # only generate if not the default prompt
32
+ # for _ in range(next_words):
33
+ # token_list = sp.encode_as_ids(generated_text)
34
+ # token_list = pad_sequences([token_list], maxlen=max_seq_len - 1, padding='pre')
35
+ # predicted = model.predict(token_list, verbose=0)[0]
36
+
37
+ # predicted = np.asarray(predicted).astype("float64")
38
+ # predicted = np.log(predicted + 1e-8) / temperature
39
+ # exp_preds = np.exp(predicted)
40
+ # predicted = exp_preds / np.sum(exp_preds)
41
+
42
+ # next_index = np.random.choice(len(predicted), p=predicted)
43
+ # next_token = sp.id_to_piece(next_index)
44
+ # generated_text += next_token
45
+
46
+ # if next_token.endswith('</s>') or next_token.endswith('<unk>'):
47
+ # break
48
+
49
+ # decoded = sp.decode_pieces(sp.encode_as_pieces(generated_text))
50
+ # decoded = decoded.replace("</s>", "").replace("<unk>", "").strip()
51
+
52
+ # if '|' in decoded:
53
+ # decoded = decoded.split('|', 1)[1].strip()
54
+
55
+ # if any(detect_profanity([decoded], language='All')) or (hate_speech_result := detect_hate_speech(decoded)) and hate_speech_result[0] in ['Hate Speech', 'Offensive Speech']:
56
+ # gr.Warning("Flagged potentially harmful output.")
57
+ # decoded = 'Flagged Output'
58
+
59
+ # return decoded
60
+
61
+ # demo = gr.Interface(
62
+ # fn=generate_text,
63
+ # inputs=[
64
+ # gr.Textbox(label="Prompt", value="a female character name", max_lines=1),
65
+ # gr.Slider(1, 100, 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. Dungen Dev is an experimental model, and may produce outputs that are inappropriate, biased, or potentially harmful and inaccurate. Caution is advised.',
71
+ # examples=[
72
+ # ["a male character name", 30, 0.5],
73
+ # ["a futuristic city name", 30, 0.5],
74
+ # ["an item name", 30, 0.5],
75
+ # ["a dark and mysterious forest name", 30, 0.5],
76
+ # ["an evil character name", 30, 0.5]
77
+ # ]
78
+ # )
79
+
80
+ # demo.launch()
81
  import sentencepiece as spm
82
  import numpy as np
83
  import tensorflow as tf
84
  from tensorflow.keras.preprocessing.sequence import pad_sequences
85
  from valx import detect_profanity, detect_hate_speech
86
  import gradio as gr
87
+ import csv
88
+ from datetime import datetime
89
 
90
  sp = spm.SentencePieceProcessor()
91
  sp.Load("dungen_dev_preview.model")
 
110
  seed_text += ' | '
111
 
112
  generated_text = seed_text
113
+ if generated_text != 'game name | ': # only generate if not the default prompt
114
  for _ in range(next_words):
115
  token_list = sp.encode_as_ids(generated_text)
116
  token_list = pad_sequences([token_list], maxlen=max_seq_len - 1, padding='pre')
 
140
 
141
  return decoded
142
 
143
+ flagged_outputs = []
144
+
145
+ def flag_output(prompt, generated_text, next_words, temperature):
146
+ if not generated_text.strip():
147
+ return "Cannot flag an empty output."
148
+
149
+ timestamp = datetime.now().isoformat()
150
+ flagged_outputs.append({
151
+ "Prompt": prompt,
152
+ "Generated Text": generated_text,
153
+ "Next Words": next_words,
154
+ "Temperature": temperature,
155
+ "Timestamp": timestamp
156
+ })
157
+
158
+ with open("flagged_outputs.csv", "a", newline="") as file:
159
+ writer = csv.DictWriter(file, fieldnames=["Prompt", "Generated Text", "Next Words", "Temperature", "Timestamp"])
160
+ if file.tell() == 0:
161
+ writer.writeheader()
162
+ writer.writerow({
163
+ "Prompt": prompt,
164
+ "Generated Text": generated_text,
165
+ "Next Words": next_words,
166
+ "Temperature": temperature,
167
+ "Timestamp": timestamp
168
+ })
169
+
170
+ return "Output flagged successfully."
171
+
172
+ def push_flagged_to_dataset():
173
+ import datasets
174
+ dataset = datasets.load_dataset("InfinitodeLTD/DungenDev-FlaggedOutputs", split="train")
175
+ dataset = dataset.add_item(flagged_outputs)
176
+ dataset.push_to_hub()
177
+ flagged_outputs.clear()
178
+ return "Flagged outputs pushed to public dataset."
179
+
180
+ def display_disclaimer():
181
+ return ('Dungen Dev is an experimental model and may produce outputs that are inappropriate, biased, or potentially harmful and inaccurate. '
182
+ 'Caution is advised.')
183
+
184
  demo = gr.Interface(
185
  fn=generate_text,
186
  inputs=[
187
  gr.Textbox(label="Prompt", value="a female character name", max_lines=1),
188
  gr.Slider(1, 100, step=1, label='Next Words', value=30),
189
+ gr.Slider(0.1, 1, value=0.5, label='Temperature', info='Controls randomness of generation, higher values = more creative, lower values = more probabilistic')
190
+ ],
191
+ outputs=[
192
+ gr.Textbox(label="Generated Names"),
193
+ gr.Button("Flag Output", elem_id="flag_button"),
194
+ gr.HTML(display_disclaimer())
195
  ],
196
+ live=True,
197
  title='Dungen Dev - Name Generator',
198
+ description=None,
199
  examples=[
200
  ["a male character name", 30, 0.5],
201
  ["a futuristic city name", 30, 0.5],