JohanBeytell commited on
Commit
d46ffba
·
verified ·
1 Parent(s): 261d6fa

Testing logging

Browse files
Files changed (1) hide show
  1. app.py +129 -15
app.py CHANGED
@@ -1,17 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
@@ -28,7 +121,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,23 +151,44 @@ 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],
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()
 
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
+
82
  import sentencepiece as spm
83
  import numpy as np
84
  import tensorflow as tf
85
  from tensorflow.keras.preprocessing.sequence import pad_sequences
86
  from valx import detect_profanity, detect_hate_speech
87
  import gradio as gr
88
+ import logging
89
+ import csv
90
+ import os
91
+ from datetime import datetime
92
 
93
+ # Model and SentencePiece loading
94
  sp = spm.SentencePieceProcessor()
95
  sp.Load("dungen_dev_preview.model")
 
96
  model = tf.keras.models.load_model("dungen_dev_preview_model.keras")
 
97
  max_seq_len = 25
98
 
99
+ # Logging setup
100
+ CSV_FILE = "flagged_outputs.csv"
101
+ if not os.path.exists(CSV_FILE):
102
+ with open(CSV_FILE, "w", newline="", encoding="utf-8") as csvfile: #added encoding
103
+ writer = csv.writer(csvfile)
104
+ writer.writerow(["Timestamp", "Flagged Text"])
105
+
106
+ logging.basicConfig(filename="app.log", level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
107
+
108
  def generate_text(seed_text, next_words=30, temperature=0.5):
109
  seed_text = seed_text.strip().lower()
110
 
 
121
  seed_text += ' | '
122
 
123
  generated_text = seed_text
124
+ if generated_text != 'game name | ':
125
  for _ in range(next_words):
126
  token_list = sp.encode_as_ids(generated_text)
127
  token_list = pad_sequences([token_list], maxlen=max_seq_len - 1, padding='pre')
 
151
 
152
  return decoded
153
 
154
+ def flag_output(text):
155
+ logging.info(f"Output flagged: {text}")
156
+ timestamp = datetime.now().isoformat()
157
+ with open(CSV_FILE, "a", newline="", encoding="utf-8") as csvfile: #added encoding
158
+ writer = csv.writer(csvfile)
159
+ writer.writerow([timestamp, text])
160
+ return "Output Flagged. Thank you for your feedback."
161
+
162
+ with gr.Blocks() as demo:
163
+ gr.Markdown("""# Dungen Dev - Name Generator
164
+ A prompt-based name generator for game developers.""")
165
+
166
+ with gr.Row():
167
+ with gr.Column():
168
+ prompt = gr.Textbox(label="Prompt", value="a female character name", max_lines=1)
169
+ with gr.Row():
170
+ next_words_slider = gr.Slider(1, 100, step=1, label='Next Words', value=30)
171
+ temperature_slider = gr.Slider(0.1, 1, value=0.5, label='Temperature', info='Controls randomness of generation, higher values = more creative, lower values = more probalistic')
172
+ generate_button = gr.Button("Generate")
173
+ with gr.Column():
174
+ output_text = gr.Textbox(label="Generated Names", interactive=False)
175
+ flag_button = gr.Button("Flag Output")
176
+
177
+ gr.Markdown("""Dungen Dev is an experimental model, and may produce outputs that are inappropriate, biased, or potentially harmful and inaccurate. Caution is advised.""")
178
+
179
+ generate_button.click(
180
+ fn=generate_text,
181
+ inputs=[prompt, next_words_slider, temperature_slider],
182
+ outputs=output_text
183
+ )
184
+ flag_button.click(flag_output, inputs=output_text, outputs=gr.Textbox(label="Flag Status", interactive=False))
185
+
186
+ demo.examples=[
187
  ["a male character name", 30, 0.5],
188
  ["a futuristic city name", 30, 0.5],
189
  ["an item name", 30, 0.5],
190
  ["a dark and mysterious forest name", 30, 0.5],
191
  ["an evil character name", 30, 0.5]
192
  ]
 
193
 
194
  demo.launch()