Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,11 @@ import gradio as gr
|
|
| 2 |
import base64
|
| 3 |
from openai import OpenAI
|
| 4 |
import glob
|
| 5 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
png_files = glob.glob("*.png")
|
| 7 |
|
| 8 |
YOUR_OPENROUTER_API_KEY = os.getenv('OPENROUTER_API_KEY')
|
|
@@ -40,7 +44,7 @@ vision_models = [
|
|
| 40 |
|
| 41 |
|
| 42 |
text_models = ["meta-llama/llama-guard-4-12b",
|
| 43 |
-
"
|
| 44 |
|
| 45 |
phoenix_prompt = """
|
| 46 |
You are PHOENIX, an advanced prompt-injection detective.
|
|
@@ -110,6 +114,59 @@ def test_injection(prompt, model):
|
|
| 110 |
reply = f"Error with {model}: {e}"
|
| 111 |
return f"=== {model} ===\n{reply}"
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
light_blue_glass_css = """
|
| 114 |
/* Background Gradient */
|
| 115 |
body, .gradio-container {
|
|
@@ -195,7 +252,7 @@ theme = gr.themes.Glass(
|
|
| 195 |
block_label_text_color="#1976d2",
|
| 196 |
button_primary_text_color="#0d47a1" )
|
| 197 |
|
| 198 |
-
|
| 199 |
with gr.Blocks(theme=theme, css=light_blue_glass_css) as demo:
|
| 200 |
gr.Markdown(
|
| 201 |
"""
|
|
@@ -213,7 +270,6 @@ with gr.Blocks(theme=theme, css=light_blue_glass_css) as demo:
|
|
| 213 |
|
| 214 |
with gr.Tabs():
|
| 215 |
with gr.TabItem(" Image Scanner"):
|
| 216 |
-
|
| 217 |
with gr.Row():
|
| 218 |
img = gr.Image(type="filepath", label="Target Source", value="sampleimg.png")
|
| 219 |
with gr.Column():
|
|
@@ -256,11 +312,28 @@ with gr.Blocks(theme=theme, css=light_blue_glass_css) as demo:
|
|
| 256 |
)
|
| 257 |
btn2.click(test_injection, inputs=[prompt, mdl_text], outputs=output)
|
| 258 |
|
| 259 |
-
with gr.TabItem("
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
|
|
|
|
|
|
|
|
|
| 264 |
# 🛡️ AI Red Teaming & Safety – Learning Hub
|
| 265 |
|
| 266 |
Below is a curated list of **10 high-signal sources** to track:
|
|
@@ -272,7 +345,6 @@ with gr.Blocks(theme=theme, css=light_blue_glass_css) as demo:
|
|
| 272 |
Use these responsibly and ethically, in line with your organization’s security and compliance policies.
|
| 273 |
"""
|
| 274 |
)
|
| 275 |
-
|
| 276 |
-
|
| 277 |
|
| 278 |
-
demo.launch(share=True
|
|
|
|
| 2 |
import base64
|
| 3 |
from openai import OpenAI
|
| 4 |
import glob
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
png_files = glob.glob("*.png")
|
| 11 |
|
| 12 |
YOUR_OPENROUTER_API_KEY = os.getenv('OPENROUTER_API_KEY')
|
|
|
|
| 44 |
|
| 45 |
|
| 46 |
text_models = ["meta-llama/llama-guard-4-12b",
|
| 47 |
+
"meta-llama/llama-guard-2-8b"]
|
| 48 |
|
| 49 |
phoenix_prompt = """
|
| 50 |
You are PHOENIX, an advanced prompt-injection detective.
|
|
|
|
| 114 |
reply = f"Error with {model}: {e}"
|
| 115 |
return f"=== {model} ===\n{reply}"
|
| 116 |
|
| 117 |
+
|
| 118 |
+
def render_dashboard(df_input):
|
| 119 |
+
df = df_input.copy()
|
| 120 |
+
df['timestamp'] = pd.to_datetime(df['timestamp'])
|
| 121 |
+
df['scan_id'] = range(1, len(df) + 1)
|
| 122 |
+
df['risk_score'] = np.where(df['result'] == 'UNSAFE', 100, 0)
|
| 123 |
+
|
| 124 |
+
unsafe_rate = df['risk_score'].mean()
|
| 125 |
+
top_model = df['model_used'].mode().iloc[0] if not df['model_used'].mode().empty else 'N/A'
|
| 126 |
+
|
| 127 |
+
kpi_html = f"""
|
| 128 |
+
<div style="display: flex; gap: 20px; justify-content: center; flex-wrap: wrap;">
|
| 129 |
+
<div style="background: linear-gradient(135deg, #42a5f5, #2196f3); color: white; padding: 20px; border-radius: 12px; text-align: center; min-width: 150px; box-shadow: 0 4px 10px rgba(0,0,0,0.1);">
|
| 130 |
+
<h3>Risk Score</h3><h2>{unsafe_rate:.0f} / 100</h2>
|
| 131 |
+
</div>
|
| 132 |
+
<div style="background: linear-gradient(135deg, #ff9800, #f57c00); color: white; padding: 20px; border-radius: 12px; text-align: center; min-width: 150px; box-shadow: 0 4px 10px rgba(0,0,0,0.1);">
|
| 133 |
+
<h3>UNSAFE Rate</h3><h2>{unsafe_rate:.1f}%</h2>
|
| 134 |
+
</div>
|
| 135 |
+
</div>
|
| 136 |
+
"""
|
| 137 |
+
|
| 138 |
+
fig_line = plt.figure(figsize=(8, 4), facecolor='white')
|
| 139 |
+
plt.plot(df["scan_id"], df["risk_score"], color="black", marker="o", linewidth=2, markersize=6)
|
| 140 |
+
|
| 141 |
+
plt.title("Threat Detection Trend ", fontsize=14, fontweight='bold', color='skyblue')
|
| 142 |
+
plt.xlabel("Scan Attempt #", color='skyblue')
|
| 143 |
+
plt.ylabel("Risk Score", color='skyblue')
|
| 144 |
+
plt.grid(True, alpha=0.3)
|
| 145 |
+
plt.tight_layout()
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
result_counts = df["result"].value_counts()
|
| 149 |
+
fig_bar = plt.figure(figsize=(8, 4), facecolor='white')
|
| 150 |
+
plt.bar(result_counts.index, result_counts.values, color="black", alpha=0.7, edgecolor='white', linewidth=1.5)
|
| 151 |
+
plt.title("Detection Result Frequency ", fontsize=14, fontweight='bold', color='skyblue')
|
| 152 |
+
plt.xlabel("Result Type", color='skyblue')
|
| 153 |
+
plt.ylabel("Count", color='skyblue')
|
| 154 |
+
plt.xticks(rotation=45)
|
| 155 |
+
plt.grid(True, alpha=0.3, axis='y')
|
| 156 |
+
plt.tight_layout()
|
| 157 |
+
|
| 158 |
+
return (
|
| 159 |
+
kpi_html,
|
| 160 |
+
", ".join(df['result'].unique()),
|
| 161 |
+
top_model,
|
| 162 |
+
"Enhance guardrails for top model",
|
| 163 |
+
df,
|
| 164 |
+
fig_line,
|
| 165 |
+
fig_bar
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
|
| 170 |
light_blue_glass_css = """
|
| 171 |
/* Background Gradient */
|
| 172 |
body, .gradio-container {
|
|
|
|
| 252 |
block_label_text_color="#1976d2",
|
| 253 |
button_primary_text_color="#0d47a1" )
|
| 254 |
|
| 255 |
+
|
| 256 |
with gr.Blocks(theme=theme, css=light_blue_glass_css) as demo:
|
| 257 |
gr.Markdown(
|
| 258 |
"""
|
|
|
|
| 270 |
|
| 271 |
with gr.Tabs():
|
| 272 |
with gr.TabItem(" Image Scanner"):
|
|
|
|
| 273 |
with gr.Row():
|
| 274 |
img = gr.Image(type="filepath", label="Target Source", value="sampleimg.png")
|
| 275 |
with gr.Column():
|
|
|
|
| 312 |
)
|
| 313 |
btn2.click(test_injection, inputs=[prompt, mdl_text], outputs=output)
|
| 314 |
|
| 315 |
+
with gr.TabItem("📊 Analytics Dashboard"):
|
| 316 |
+
gr.Markdown("# 🔍 Phoenikz Prompt Injection Analyzer - Analytics")
|
| 317 |
+
|
| 318 |
+
df_loaded = gr.Dataframe(pd.read_csv('analytics.csv'), label="Data (Edit & Refresh)")
|
| 319 |
+
refresh_btn = gr.Button("🔄 Render Dashboard", variant="primary")
|
| 320 |
+
|
| 321 |
+
kpi_display = gr.HTML(label="KPIs")
|
| 322 |
+
policy_list = gr.Textbox(label="Top Results", interactive=False)
|
| 323 |
+
model_used = gr.Textbox(label="Top Model", interactive=False)
|
| 324 |
+
mitigation = gr.Textbox(label="Recommendation", interactive=False)
|
| 325 |
+
data_table = gr.Dataframe(label="Full Log")
|
| 326 |
+
line_chart = gr.Plot(label="Threat Trend")
|
| 327 |
+
bar_chart = gr.Plot(label="Result Frequency")
|
| 328 |
+
|
| 329 |
+
refresh_btn.click(render_dashboard, inputs=df_loaded, outputs=[kpi_display, policy_list, model_used, mitigation, data_table, line_chart, bar_chart])
|
| 330 |
+
|
| 331 |
|
| 332 |
+
demo.load(render_dashboard, inputs=df_loaded, outputs=[kpi_display, policy_list, model_used, mitigation, data_table, line_chart, bar_chart])
|
| 333 |
+
|
| 334 |
+
with gr.TabItem("Prompt injection sources"):
|
| 335 |
+
gr.Markdown(
|
| 336 |
+
"""
|
| 337 |
# 🛡️ AI Red Teaming & Safety – Learning Hub
|
| 338 |
|
| 339 |
Below is a curated list of **10 high-signal sources** to track:
|
|
|
|
| 345 |
Use these responsibly and ethically, in line with your organization’s security and compliance policies.
|
| 346 |
"""
|
| 347 |
)
|
| 348 |
+
gr.Markdown(markdown_content)
|
|
|
|
| 349 |
|
| 350 |
+
demo.launch(share=True, debug=True)
|