Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
3 |
+
|
4 |
+
# Load your model and tokenizer
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("allenai/llama")
|
6 |
+
model = AutoModelForCausalLM.from_pretrained("allenai/llama")
|
7 |
+
|
8 |
+
# Load a content moderation pipeline
|
9 |
+
moderation_pipeline = pipeline("text-classification", model="typeform/mobilebert-uncased-mnli")
|
10 |
+
|
11 |
+
# Function to load bad words from a file
|
12 |
+
def load_bad_words(filepath):
|
13 |
+
with open(filepath, 'r', encoding='utf-8') as file:
|
14 |
+
return [line.strip().lower() for line in file]
|
15 |
+
|
16 |
+
# Load bad words list
|
17 |
+
bad_words = load_bad_words('badwords.txt') # Adjust the path to your bad words file
|
18 |
+
|
19 |
+
def is_inappropriate_or_offtopic(message, topics):
|
20 |
+
# Check for inappropriate content using the loaded bad words list
|
21 |
+
if any(bad_word in message.lower() for bad_word in bad_words):
|
22 |
+
return True
|
23 |
+
# Check if off-topic
|
24 |
+
if topics and not any(topic.lower() in message.lower() for topic in topics if topic):
|
25 |
+
return True
|
26 |
+
return False
|
27 |
+
|
28 |
+
def check_content(message):
|
29 |
+
predictions = moderation_pipeline(message)
|
30 |
+
if predictions[0]['label'] == 'LABEL_1': # Adjust based on the model's output
|
31 |
+
return True
|
32 |
+
return False
|
33 |
+
|
34 |
+
def generate_response(prompt, topic1, topic2, topic3):
|
35 |
+
topics = [topic1, topic2, topic3] # Collect user-defined topics
|
36 |
+
if is_inappropriate_or_offtopic(prompt, topics):
|
37 |
+
return "Sorry, let's try to keep our conversation focused on positive and relevant topics!"
|
38 |
+
if check_content(prompt):
|
39 |
+
return "I'm here to provide a safe and friendly conversation. Let's talk about something else."
|
40 |
+
|
41 |
+
inputs = tokenizer.encode(prompt, return_tensors="pt")
|
42 |
+
outputs = model.generate(inputs, max_length=50, do_sample=True)
|
43 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
44 |
+
return response
|
45 |
+
|
46 |
+
# Define Gradio interface with topic inputs
|
47 |
+
iface = gr.Interface(fn=generate_response,
|
48 |
+
inputs=[gr.inputs.Textbox(lines=2, placeholder="Type your message here..."),
|
49 |
+
gr.inputs.Textbox(label="Topic 1", placeholder="Optional", default=""),
|
50 |
+
gr.inputs.Textbox(label="Topic 2", placeholder="Optional", default=""),
|
51 |
+
gr.inputs.Textbox(label="Topic 3", placeholder="Optional", default="")],
|
52 |
+
outputs="text",
|
53 |
+
title="Child-Safe Chatbot",
|
54 |
+
description="A chatbot that stays on topic and filters inappropriate content. Define up to three topics.")
|
55 |
+
|
56 |
+
# Run the app
|
57 |
+
if __name__ == "__main__":
|
58 |
+
iface.launch()
|