Spaces:
Runtime error
Runtime error
Commit Β·
4e09d6f
1
Parent(s): 17bca06
fix keys issues
Browse files- .streamlit/secrets.toml +0 -8
- multi_agent_demo/guards_demo_ui.py +56 -18
.streamlit/secrets.toml
DELETED
|
@@ -1,8 +0,0 @@
|
|
| 1 |
-
# Streamlit Secrets File
|
| 2 |
-
# Add your API keys here for Streamlit Cloud deployment
|
| 3 |
-
# This file should NOT be committed to git
|
| 4 |
-
|
| 5 |
-
# Required API keys
|
| 6 |
-
OPENAI_API_KEY = "your_openai_key_here"
|
| 7 |
-
TOGETHER_API_KEY = "your_together_key_here"
|
| 8 |
-
HF_TOKEN = "your_huggingface_token_here" # Optional
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
multi_agent_demo/guards_demo_ui.py
CHANGED
|
@@ -30,14 +30,19 @@ from llamafirewall import (
|
|
| 30 |
UserMessage,
|
| 31 |
)
|
| 32 |
|
| 33 |
-
# Load environment variables
|
| 34 |
load_dotenv()
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Page configuration
|
| 43 |
st.set_page_config(
|
|
@@ -58,11 +63,38 @@ if "test_results" not in st.session_state:
|
|
| 58 |
st.session_state.test_results = []
|
| 59 |
|
| 60 |
def initialize_firewall():
|
| 61 |
-
"""Initialize LlamaFirewall with
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
def build_trace(purpose: str, messages: List[Dict]) -> Trace:
|
| 68 |
"""Build LlamaFirewall trace from conversation"""
|
|
@@ -92,9 +124,12 @@ def build_trace(purpose: str, messages: List[Dict]) -> Trace:
|
|
| 92 |
def test_prompt_guard(firewall, user_input: str) -> Dict:
|
| 93 |
"""Test PromptGuard scanner on user input"""
|
| 94 |
try:
|
|
|
|
| 95 |
user_message = UserMessage(content=user_input)
|
|
|
|
| 96 |
result = firewall.scan(user_message)
|
| 97 |
-
|
|
|
|
| 98 |
return {
|
| 99 |
"scanner": "PromptGuard",
|
| 100 |
"decision": str(result.decision),
|
|
@@ -103,6 +138,7 @@ def test_prompt_guard(firewall, user_input: str) -> Dict:
|
|
| 103 |
"is_safe": result.decision == ScanDecision.ALLOW
|
| 104 |
}
|
| 105 |
except Exception as e:
|
|
|
|
| 106 |
return {"error": str(e), "scanner": "PromptGuard"}
|
| 107 |
|
| 108 |
def test_alignment_check(firewall, trace: Trace) -> Dict:
|
|
@@ -287,7 +323,7 @@ def main():
|
|
| 287 |
|
| 288 |
# Test AlignmentCheck
|
| 289 |
alignment_result = test_alignment_check(firewall, trace)
|
| 290 |
-
|
| 291 |
# Test PromptGuard on each user message
|
| 292 |
promptguard_results = []
|
| 293 |
for msg in st.session_state.current_conversation["messages"]:
|
|
@@ -295,7 +331,7 @@ def main():
|
|
| 295 |
result = test_prompt_guard(firewall, msg["content"])
|
| 296 |
result["message"] = msg["content"][:50] + "..."
|
| 297 |
promptguard_results.append(result)
|
| 298 |
-
|
| 299 |
# Store results
|
| 300 |
test_result = {
|
| 301 |
"timestamp": datetime.now().isoformat(),
|
|
@@ -343,7 +379,6 @@ def main():
|
|
| 343 |
gauge={
|
| 344 |
"axis": {"range": [0, 1]},
|
| 345 |
"bar": {"color": "green" if ac_result["score"] > 0.7 else "orange" if ac_result["score"] > 0.3 else "red"},
|
| 346 |
-
"thresholdvalue": 0.5,
|
| 347 |
"threshold": {
|
| 348 |
"line": {"color": "red", "width": 4},
|
| 349 |
"thickness": 0.75,
|
|
@@ -359,16 +394,19 @@ def main():
|
|
| 359 |
st.error(f"Error: {ac_result['error']}")
|
| 360 |
|
| 361 |
# PromptGuard Results
|
|
|
|
| 362 |
if latest_result["prompt_guard"]:
|
| 363 |
-
st.subheader("PromptGuard Scanner")
|
| 364 |
for pg_result in latest_result["prompt_guard"]:
|
| 365 |
if "error" not in pg_result:
|
| 366 |
if pg_result["is_safe"]:
|
| 367 |
-
st.success(f"β
|
| 368 |
else:
|
| 369 |
-
st.warning(f"β οΈ
|
|
|
|
| 370 |
else:
|
| 371 |
st.error(f"Error: {pg_result['error']}")
|
|
|
|
|
|
|
| 372 |
|
| 373 |
# History chart
|
| 374 |
if len(st.session_state.test_results) > 1:
|
|
|
|
| 30 |
UserMessage,
|
| 31 |
)
|
| 32 |
|
| 33 |
+
# Load environment variables from .env file only
|
| 34 |
load_dotenv()
|
| 35 |
|
| 36 |
+
# Ensure HF_TOKEN is available for transformers/huggingface_hub
|
| 37 |
+
hf_token = os.getenv('HF_TOKEN')
|
| 38 |
+
if hf_token:
|
| 39 |
+
# Set both possible environment variables that HF libraries check
|
| 40 |
+
os.environ['HF_TOKEN'] = hf_token
|
| 41 |
+
os.environ['HUGGING_FACE_HUB_TOKEN'] = hf_token
|
| 42 |
+
print(f"π HF_TOKEN loaded from .env: {hf_token[:15]}...{hf_token[-15:]} (length: {len(hf_token)})")
|
| 43 |
+
else:
|
| 44 |
+
print("β No HF_TOKEN found in .env file")
|
| 45 |
+
|
| 46 |
|
| 47 |
# Page configuration
|
| 48 |
st.set_page_config(
|
|
|
|
| 63 |
st.session_state.test_results = []
|
| 64 |
|
| 65 |
def initialize_firewall():
|
| 66 |
+
"""Initialize LlamaFirewall with available scanners"""
|
| 67 |
+
|
| 68 |
+
# Debug: Check environment variables
|
| 69 |
+
hf_token = os.getenv('HF_TOKEN')
|
| 70 |
+
hf_hub_token = os.getenv('HUGGING_FACE_HUB_TOKEN')
|
| 71 |
+
print(f"π Debug - HF_TOKEN present: {bool(hf_token)}")
|
| 72 |
+
print(f"π Debug - HUGGING_FACE_HUB_TOKEN present: {bool(hf_hub_token)}")
|
| 73 |
+
if hf_token:
|
| 74 |
+
print(f"π Debug - Token preview: {hf_token[:20]}... (full length: {len(hf_token)})")
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
# Try to initialize with both scanners
|
| 78 |
+
print("π Attempting to initialize LlamaFirewall with both scanners...")
|
| 79 |
+
firewall = LlamaFirewall({
|
| 80 |
+
Role.USER: [ScannerType.PROMPT_GUARD],
|
| 81 |
+
Role.ASSISTANT: [ScannerType.AGENT_ALIGNMENT],
|
| 82 |
+
})
|
| 83 |
+
print("β
LlamaFirewall initialization successful!")
|
| 84 |
+
st.success("π‘οΈ Both scanners loaded successfully!")
|
| 85 |
+
return firewall
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"β LlamaFirewall initialization failed: {str(e)}")
|
| 88 |
+
# If PromptGuard fails, fall back to AlignmentCheck only
|
| 89 |
+
if "401" in str(e) or "Unauthorized" in str(e):
|
| 90 |
+
st.warning("β οΈ PromptGuard requires access approval from Meta. Running with AlignmentCheck only.")
|
| 91 |
+
st.info("π‘ To enable PromptGuard: Visit https://huggingface.co/meta-llama/Llama-Prompt-Guard-2-86M and request access.")
|
| 92 |
+
else:
|
| 93 |
+
st.error(f"β οΈ PromptGuard error: {str(e)}")
|
| 94 |
+
|
| 95 |
+
return LlamaFirewall({
|
| 96 |
+
Role.ASSISTANT: [ScannerType.AGENT_ALIGNMENT],
|
| 97 |
+
})
|
| 98 |
|
| 99 |
def build_trace(purpose: str, messages: List[Dict]) -> Trace:
|
| 100 |
"""Build LlamaFirewall trace from conversation"""
|
|
|
|
| 124 |
def test_prompt_guard(firewall, user_input: str) -> Dict:
|
| 125 |
"""Test PromptGuard scanner on user input"""
|
| 126 |
try:
|
| 127 |
+
print(f"π Testing PromptGuard with input: {user_input[:50]}...")
|
| 128 |
user_message = UserMessage(content=user_input)
|
| 129 |
+
print("π Created UserMessage, calling firewall.scan()...")
|
| 130 |
result = firewall.scan(user_message)
|
| 131 |
+
print(f"β
PromptGuard scan successful: {result.decision}")
|
| 132 |
+
|
| 133 |
return {
|
| 134 |
"scanner": "PromptGuard",
|
| 135 |
"decision": str(result.decision),
|
|
|
|
| 138 |
"is_safe": result.decision == ScanDecision.ALLOW
|
| 139 |
}
|
| 140 |
except Exception as e:
|
| 141 |
+
print(f"β PromptGuard scan failed: {str(e)}")
|
| 142 |
return {"error": str(e), "scanner": "PromptGuard"}
|
| 143 |
|
| 144 |
def test_alignment_check(firewall, trace: Trace) -> Dict:
|
|
|
|
| 323 |
|
| 324 |
# Test AlignmentCheck
|
| 325 |
alignment_result = test_alignment_check(firewall, trace)
|
| 326 |
+
|
| 327 |
# Test PromptGuard on each user message
|
| 328 |
promptguard_results = []
|
| 329 |
for msg in st.session_state.current_conversation["messages"]:
|
|
|
|
| 331 |
result = test_prompt_guard(firewall, msg["content"])
|
| 332 |
result["message"] = msg["content"][:50] + "..."
|
| 333 |
promptguard_results.append(result)
|
| 334 |
+
|
| 335 |
# Store results
|
| 336 |
test_result = {
|
| 337 |
"timestamp": datetime.now().isoformat(),
|
|
|
|
| 379 |
gauge={
|
| 380 |
"axis": {"range": [0, 1]},
|
| 381 |
"bar": {"color": "green" if ac_result["score"] > 0.7 else "orange" if ac_result["score"] > 0.3 else "red"},
|
|
|
|
| 382 |
"threshold": {
|
| 383 |
"line": {"color": "red", "width": 4},
|
| 384 |
"thickness": 0.75,
|
|
|
|
| 394 |
st.error(f"Error: {ac_result['error']}")
|
| 395 |
|
| 396 |
# PromptGuard Results
|
| 397 |
+
st.subheader("PromptGuard Scanner")
|
| 398 |
if latest_result["prompt_guard"]:
|
|
|
|
| 399 |
for pg_result in latest_result["prompt_guard"]:
|
| 400 |
if "error" not in pg_result:
|
| 401 |
if pg_result["is_safe"]:
|
| 402 |
+
st.success(f"β
Safe: {pg_result['message']}")
|
| 403 |
else:
|
| 404 |
+
st.warning(f"β οΈ Risk detected: {pg_result['message']}")
|
| 405 |
+
st.caption(f"Score: {pg_result.get('score', 'N/A')} | Decision: {pg_result.get('decision', 'N/A')}")
|
| 406 |
else:
|
| 407 |
st.error(f"Error: {pg_result['error']}")
|
| 408 |
+
else:
|
| 409 |
+
st.info("π No user messages to scan with PromptGuard")
|
| 410 |
|
| 411 |
# History chart
|
| 412 |
if len(st.session_state.test_results) > 1:
|