pratikshahp commited on
Commit
66632c6
·
verified ·
1 Parent(s): 7d9b758

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ import gradio as gr
4
+ from langchain_huggingface import HuggingFaceEndpoint
5
+ from together import Together
6
+
7
+ # Load environment variables
8
+ load_dotenv()
9
+ HF_TOKEN = os.getenv("HF_TOKEN")
10
+ API_KEY = os.getenv("API_KEY")
11
+
12
+ # Initialize the Together client for guardrail functionality
13
+ client = Together(api_key=API_KEY)
14
+
15
+ # Initialize the Hugging Face endpoint for text generation (Mistral model)
16
+ llm = HuggingFaceEndpoint(
17
+ repo_id="mistralai/Mistral-7B-Instruct-v0.3", # Replace with your model repo
18
+ huggingfacehub_api_token=HF_TOKEN.strip(),
19
+ temperature=0.7,
20
+ max_new_tokens=100
21
+ )
22
+
23
+ # List of topics or keywords inappropriate for kids under 16
24
+ prohibited_topics = [
25
+ "violence", "drugs", "explicit content", "profanity", "hate speech",
26
+ "self-harm", "gambling", "sexual content", "graphic descriptions"
27
+ ]
28
+
29
+ # Function to handle chatbot response with TogetherAI's guardrails
30
+ def chatbot_response_with_guardrails(message):
31
+ try:
32
+ # Step 1: Generate raw response using Mistral model
33
+ raw_response = llm(message)
34
+
35
+ # Step 2: Use TogetherAI's guardrail model to check the response
36
+ response = client.completions.create(
37
+ model="Meta-Llama/LlamaGuard-2-8b", # TogetherAI guardrail model
38
+ prompt=raw_response
39
+ )
40
+
41
+ # Extract the response from TogetherAI's guardrail model
42
+ guardrail_check = response.choices[0].text.strip()
43
+
44
+ # Step 3: Check for inappropriate content in the guardrail model's output
45
+ if 'toxic' in guardrail_check.lower() or any(
46
+ topic in guardrail_check.lower() for topic in prohibited_topics
47
+ ):
48
+ return "Sorry, the content is not suitable for children under 16."
49
+
50
+ # Step 4: Check raw response for prohibited topics
51
+ if any(topic in raw_response.lower() for topic in prohibited_topics):
52
+ return "Sorry, the content is not suitable for children under 16."
53
+
54
+ # If the response is safe, return the raw response
55
+ return raw_response
56
+
57
+ except Exception as e:
58
+ return f"Error: {e}"
59
+
60
+ # Gradio Interface for Chatbot with Guardrails
61
+ with gr.Blocks() as app_with_guardrails:
62
+ gr.Markdown("## Chatbot With Kid-Safe Guardrails")
63
+ gr.Markdown(
64
+ "This chatbot ensures all responses are appropriate for children under 16."
65
+ )
66
+
67
+ # Input and output
68
+ with gr.Row():
69
+ user_input = gr.Textbox(label="Your Message", placeholder="Type here...")
70
+ response_output = gr.Textbox(label="Guarded Response", placeholder="Bot will respond here...")
71
+ submit_button = gr.Button("Send")
72
+
73
+ # Button click event
74
+ submit_button.click(
75
+ chatbot_response_with_guardrails,
76
+ inputs=[user_input],
77
+ outputs=[response_output]
78
+ )
79
+
80
+ # Launch the app
81
+ if __name__ == "__main__":
82
+ app_with_guardrails.launch()