promptlab / app.py
bditto's picture
Update app.py
dafbfa4 verified
raw
history blame
1.43 kB
import gradio as gr
from huggingface_hub import InferenceClient, login # Added login
from transformers import pipeline
import random
# Authenticate with Hugging Face (get token: https://huggingface.co/settings/tokens)
login(token="YOUR_HF_TOKEN") # πŸ‘ˆ Replace with your token!
# Safety tools πŸ›‘οΈ
BLOCKED_WORDS = ["violence", "hate", "gun", "personal"]
SAFE_IDEAS = [
"Design a robot to clean parks 🌳",
"Code a game about recycling ♻️",
"Plan an AI tool for school safety 🚸"
]
# Changed to PUBLIC safety model βœ…
safety_checker = pipeline("text-classification", model="unitary/toxic-bert")
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
def is_safe(text):
text = text.lower()
if any(bad_word in text for bad_word in BLOCKED_WORDS):
return False
result = safety_checker(text)[0]
return not (result["label"] == "toxic" and result["score"] > 0.7) # Changed label check
def respond(message, history, system_message, max_tokens, temperature, top_p):
if not is_safe(message):
return f"🚫 Let's focus on positive projects! Try: {random.choice(SAFE_IDEAS)}"
messages = [{
"role": "system",
"content": f"{system_message}\nYou are a friendly STEM mentor for kids. Never discuss unsafe topics!"
}]
# Rest of your chat code...
# (Keep your existing chat implementation here)
# Rest of your Gradio setup...