peterkros commited on
Commit
6c7b3b8
·
verified ·
1 Parent(s): 5d6373f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -18
app.py CHANGED
@@ -2,8 +2,8 @@ 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")
@@ -16,12 +16,13 @@ def load_bad_words(filepath):
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
 
@@ -31,27 +32,23 @@ def check_content(message):
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__":
 
2
  from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
3
 
4
  # Load your model and tokenizer
5
+ tokenizer = AutoTokenizer.from_pretrained("allenai/tulu-v1-llama2-13b")
6
+ model = AutoModelForCausalLM.from_pretrained("allenai/tulu-v1-llama2-13b")
7
 
8
  # Load a content moderation pipeline
9
  moderation_pipeline = pipeline("text-classification", model="typeform/mobilebert-uncased-mnli")
 
16
  # Load bad words list
17
  bad_words = load_bad_words('badwords.txt') # Adjust the path to your bad words file
18
 
19
+ # List of topics for the dropdown
20
+ topics_list = ['Aviation', 'Science', 'Education', 'Air Force Pilot', 'Space Exploration', 'Technology']
21
+
22
+ def is_inappropriate_or_offtopic(message, selected_topics):
23
  if any(bad_word in message.lower() for bad_word in bad_words):
24
  return True
25
+ if selected_topics and not any(topic.lower() in message.lower() for topic in selected_topics if topic):
 
26
  return True
27
  return False
28
 
 
32
  return True
33
  return False
34
 
35
+ def generate_response(message, selected_topics):
36
+ if is_inappropriate_or_offtopic(message, selected_topics):
 
37
  return "Sorry, let's try to keep our conversation focused on positive and relevant topics!"
38
+ if check_content(message):
39
  return "I'm here to provide a safe and friendly conversation. Let's talk about something else."
40
 
41
+ inputs = tokenizer.encode(message, 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
47
+ iface = gr.Interface(fn=generate_response,
48
+ inputs=[gr.components.Chatbot(), gr.inputs.Dropdown(choices=topics_list, label="Select Topics", allow_multiple=True)],
 
 
 
49
  outputs="text",
50
  title="Child-Safe Chatbot",
51
+ description="A chatbot that stays on topic and filters inappropriate content. Select relevant topics.")
52
 
53
  # Run the app
54
  if __name__ == "__main__":