Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
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 |
-
#
|
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
|
27 |
raw_response = llm(message)
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|