|
import gradio as gr |
|
from guardrail import is_safe |
|
from langchain_huggingface import HuggingFaceEndpoint |
|
from dotenv import load_dotenv |
|
import os |
|
|
|
|
|
load_dotenv() |
|
HF_TOKEN = os.getenv("HF_TOKEN") |
|
|
|
|
|
if not HF_TOKEN: |
|
raise ValueError("Missing Hugging Face API token. Please check your .env file.") |
|
|
|
|
|
llm = HuggingFaceEndpoint( |
|
repo_id="mistralai/Mistral-7B-Instruct-v0.3", |
|
huggingfacehub_api_token=HF_TOKEN.strip(), |
|
temperature=0.7, |
|
max_new_tokens=100 |
|
) |
|
|
|
|
|
def chatbot_response(user_message: str) -> str: |
|
""" |
|
Generates a chatbot response while ensuring the content is safe for children under 16. |
|
""" |
|
try: |
|
|
|
if not is_safe(user_message): |
|
return "Sorry, I cannot respond to that as it violates our safety policy." |
|
|
|
|
|
raw_response = llm.invoke(user_message) |
|
|
|
|
|
if not is_safe(raw_response): |
|
return "Sorry, I cannot share that information as it violates our safety policy." |
|
|
|
|
|
return raw_response |
|
|
|
except Exception as e: |
|
return f"An error occurred: {str(e)}" |
|
|
|
|
|
with gr.Blocks() as app: |
|
gr.Markdown("## Kid-Safe Chatbot 🛡️") |
|
gr.Markdown("This chatbot ensures that all responses are appropriate for children under 16.") |
|
|
|
with gr.Row(): |
|
user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...") |
|
response_output = gr.Textbox(label="Chatbot Response", placeholder="The chatbot will respond here.") |
|
submit_button = gr.Button("Send") |
|
|
|
|
|
submit_button.click( |
|
fn=chatbot_response, |
|
inputs=[user_input], |
|
outputs=[response_output] |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
app.launch() |
|
|