File size: 1,211 Bytes
94fff06
517a85f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94fff06
 
 
517a85f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import gradio as gr
from transformers import GPT2LMHeadModel, GPT2Tokenizer

# Load model and tokenizer
model_name = "gpt2"
model = GPT2LMHeadModel.from_pretrained(model_name)
tokenizer = GPT2Tokenizer.from_pretrained(model_name)

# Function to filter explicit content
def filter_explicit(content, filter_on):
    explicit_keywords = ["badword1", "badword2"]  # Add explicit words to filter
    if filter_on:
        for word in explicit_keywords:
            content = content.replace(word, "[CENSORED]")
    return content

def generate_response(prompt, explicit_filter):
    inputs = tokenizer.encode(prompt, return_tensors="pt")
    outputs = model.generate(inputs, max_length=100, num_return_sequences=1)
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    filtered_response = filter_explicit(response, explicit_filter)
    return filtered_response

# Define Gradio interface
iface = gr.Interface(
    fn=generate_response,
    inputs=[gr.inputs.Textbox(lines=2, placeholder="Type your message here..."), gr.inputs.Checkbox(label="Enable Explicit Content Filter")],
    outputs="text",
    title="Chatbot with Explicit Content Filter"
)

if __name__ == "__main__":
    iface.launch()