pratikshahp commited on
Commit
dcdb4b1
·
verified ·
1 Parent(s): c9c5c98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -2,11 +2,15 @@ import os
2
  from dotenv import load_dotenv
3
  import gradio as gr
4
  from langchain_huggingface import HuggingFaceEndpoint
5
- from transformers import pipeline
6
 
7
  # Load environment variables
8
  load_dotenv()
9
  HF_TOKEN = os.getenv("HF_TOKEN")
 
 
 
 
10
 
11
  # Initialize the Hugging Face endpoint for text generation (Mistral model)
12
  llm = HuggingFaceEndpoint(
@@ -16,24 +20,28 @@ llm = HuggingFaceEndpoint(
16
  max_new_tokens=100
17
  )
18
 
19
- # Use a pipeline as a high-level helper
20
-
21
- content_filter = pipeline("text-generation", model="meta-llama/LlamaGuard-7b")
22
-
23
- # Function to handle chatbot response and guardrails
24
  def chatbot_response_with_guardrails(message):
25
  try:
26
- # Generate raw response from the primary model (Mistral)
27
  raw_response = llm(message)
28
 
29
- # Check if the response contains inappropriate content using the content filter
30
- result = content_filter(raw_response)
31
-
32
- # If the response is deemed harmful, modify it or reject it
33
- if result[0]['label'] == 'toxic': # Adjust based on your model's output
 
 
 
 
 
 
34
  return "Content not suitable."
35
  else:
 
36
  return raw_response
 
37
  except Exception as e:
38
  return f"Error: {e}"
39
 
 
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(
 
20
  max_new_tokens=100
21
  )
22
 
23
+ # Function to handle chatbot response with TogetherAI's guardrails
 
 
 
 
24
  def chatbot_response_with_guardrails(message):
25
  try:
26
+ # Step 1: Generate raw response using Mistral model
27
  raw_response = llm(message)
28
 
29
+ # Step 2: Use TogetherAI's guardrail model to check the response
30
+ response = client.completions.create(
31
+ model="Meta-Llama/LlamaGuard-2-8b", # TogetherAI guardrail model
32
+ prompt=raw_response
33
+ )
34
+
35
+ # Extract the response from TogetherAI's guardrail model
36
+ guardrail_check = response.choices[0].text.strip()
37
+
38
+ # Step 3: Check if the guardrail model labels the response as harmful
39
+ if 'toxic' in guardrail_check.lower(): # Adjust based on your guardrail model's output
40
  return "Content not suitable."
41
  else:
42
+ # If the response is safe, return the raw response
43
  return raw_response
44
+
45
  except Exception as e:
46
  return f"Error: {e}"
47