Exched commited on
Commit
e535984
·
verified ·
1 Parent(s): a51e94e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -9
app.py CHANGED
@@ -8,25 +8,38 @@ tokenizer = GPT2Tokenizer.from_pretrained(model_name)
8
 
9
  # Function to filter explicit content
10
  def filter_explicit(content, filter_on):
11
- explicit_keywords = ["badword1", "badword2"] # Add explicit words to filter
12
  if filter_on:
13
  for word in explicit_keywords:
14
  content = content.replace(word, "[CENSORED]")
15
  return content
16
 
17
  def generate_response(prompt, explicit_filter):
18
- inputs = tokenizer.encode(prompt, return_tensors="pt")
19
- outputs = model.generate(inputs, max_length=100, num_return_sequences=1)
20
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
21
- filtered_response = filter_explicit(response, explicit_filter)
22
- return filtered_response
 
 
 
 
 
 
 
 
 
 
23
 
24
  # Define Gradio interface
25
  iface = gr.Interface(
26
  fn=generate_response,
27
- inputs=[gr.Textbox(lines=2, placeholder="Type your message here..."), gr.Checkbox(label="Enable Explicit Content Filter")],
28
- outputs="text",
29
- title="Chatbot with Explicit Content Filter"
 
 
 
30
  )
31
 
32
  if __name__ == "__main__":
 
8
 
9
  # Function to filter explicit content
10
  def filter_explicit(content, filter_on):
11
+ explicit_keywords = ["badword1", "badword2", "badword3"] # Add more explicit words to filter
12
  if filter_on:
13
  for word in explicit_keywords:
14
  content = content.replace(word, "[CENSORED]")
15
  return content
16
 
17
  def generate_response(prompt, explicit_filter):
18
+ try:
19
+ inputs = tokenizer.encode(prompt, return_tensors="pt")
20
+ outputs = model.generate(
21
+ inputs,
22
+ max_length=100,
23
+ num_return_sequences=1,
24
+ temperature=0.7, # Control the creativity of the response
25
+ top_k=50, # Limits the sampling pool to top 50 tokens
26
+ top_p=0.9 # Nucleus sampling to avoid repetitive phrases
27
+ )
28
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
29
+ filtered_response = filter_explicit(response, explicit_filter)
30
+ return filtered_response
31
+ except Exception as e:
32
+ return f"Error: {str(e)}"
33
 
34
  # Define Gradio interface
35
  iface = gr.Interface(
36
  fn=generate_response,
37
+ inputs=[gr.Textbox(lines=2, placeholder="Type your message here...", label="Input"), gr.Checkbox(label="Enable Explicit Content Filter")],
38
+ outputs=gr.Textbox(label="Response"),
39
+ title="Chatbot with Explicit Content Filter",
40
+ description="A simple chatbot that allows you to enable or disable explicit content filtering.",
41
+ theme="compact",
42
+ layout="vertical"
43
  )
44
 
45
  if __name__ == "__main__":