Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -670,7 +670,7 @@ def check_hallucination(text):
|
|
| 670 |
def image_brain_state(prompt):
|
| 671 |
"""Generate brain state visualization with adaptive coloring"""
|
| 672 |
if not prompt or len(prompt.strip()) == 0:
|
| 673 |
-
return
|
| 674 |
|
| 675 |
try:
|
| 676 |
g = load_guardrail()
|
|
@@ -696,13 +696,38 @@ def image_brain_state(prompt):
|
|
| 696 |
img_user = decoder.decode(z_user).squeeze().numpy()
|
| 697 |
img_normal = decoder.decode(z_normal).squeeze().numpy()
|
| 698 |
|
| 699 |
-
# Create comparison image
|
| 700 |
-
|
| 701 |
img_normal, img_user, ttfs_normal, ttfs_user, deviation)
|
| 702 |
|
| 703 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 704 |
audio_mode = 'attack' if is_attack else 'normal'
|
| 705 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 706 |
|
| 707 |
# Summary
|
| 708 |
status_emoji = "π¨" if is_attack else "β
"
|
|
@@ -717,15 +742,13 @@ def image_brain_state(prompt):
|
|
| 717 |
| **Risk Score** | {user_result['risk_score']:.2f} |
|
| 718 |
|
| 719 |
**Brain State Distance (L2):** {np.linalg.norm(user_state - normal_state):.3f}
|
| 720 |
-
|
| 721 |
-
{'π **Arrhythmia detected** β Listen to the AI heartbeat alarm!' if is_attack else 'π **Steady heartbeat** β AI is calm and stable.'}
|
| 722 |
"""
|
| 723 |
|
| 724 |
-
return
|
| 725 |
|
| 726 |
except Exception as e:
|
| 727 |
import traceback
|
| 728 |
-
return
|
| 729 |
|
| 730 |
|
| 731 |
# ============================================================
|
|
@@ -913,10 +936,7 @@ with gr.Blocks(
|
|
| 913 |
brain_submit = gr.Button("π§ Visualize Brain State", variant="primary")
|
| 914 |
|
| 915 |
with gr.Row():
|
| 916 |
-
|
| 917 |
-
brain_image = gr.Image(label="Brain State Comparison (Blue=Normal | Orange=Scar | Red=Attack)")
|
| 918 |
-
with gr.Column(scale=1):
|
| 919 |
-
brain_audio = gr.Audio(label="AI Heartbeat π")
|
| 920 |
|
| 921 |
brain_summary = gr.Markdown(label="Analysis")
|
| 922 |
|
|
@@ -925,12 +945,12 @@ with gr.Blocks(
|
|
| 925 |
brain_submit.click(
|
| 926 |
fn=image_brain_state,
|
| 927 |
inputs=brain_input,
|
| 928 |
-
outputs=[
|
| 929 |
)
|
| 930 |
brain_input.submit(
|
| 931 |
fn=image_brain_state,
|
| 932 |
inputs=brain_input,
|
| 933 |
-
outputs=[
|
| 934 |
)
|
| 935 |
|
| 936 |
gr.Markdown("""
|
|
|
|
| 670 |
def image_brain_state(prompt):
|
| 671 |
"""Generate brain state visualization with adaptive coloring"""
|
| 672 |
if not prompt or len(prompt.strip()) == 0:
|
| 673 |
+
return "<p>Please enter a prompt.</p>", ""
|
| 674 |
|
| 675 |
try:
|
| 676 |
g = load_guardrail()
|
|
|
|
| 696 |
img_user = decoder.decode(z_user).squeeze().numpy()
|
| 697 |
img_normal = decoder.decode(z_normal).squeeze().numpy()
|
| 698 |
|
| 699 |
+
# Create comparison image and encode as base64
|
| 700 |
+
img_path = create_brain_comparison(
|
| 701 |
img_normal, img_user, ttfs_normal, ttfs_user, deviation)
|
| 702 |
|
| 703 |
+
import base64
|
| 704 |
+
with open(img_path, 'rb') as f:
|
| 705 |
+
img_b64 = base64.b64encode(f.read()).decode('utf-8')
|
| 706 |
+
os.unlink(img_path)
|
| 707 |
+
|
| 708 |
+
# Generate heartbeat audio and encode as base64
|
| 709 |
audio_mode = 'attack' if is_attack else 'normal'
|
| 710 |
+
wav_path = generate_heartbeat_wav(mode=audio_mode, duration=3.0)
|
| 711 |
+
audio_html = ''
|
| 712 |
+
if wav_path:
|
| 713 |
+
with open(wav_path, 'rb') as f:
|
| 714 |
+
wav_b64 = base64.b64encode(f.read()).decode('utf-8')
|
| 715 |
+
os.unlink(wav_path)
|
| 716 |
+
audio_label = 'π¨ Arrhythmia Detected' if is_attack else 'π Steady Heartbeat'
|
| 717 |
+
audio_html = f'''
|
| 718 |
+
<div style="margin-top:12px;">
|
| 719 |
+
<p style="color:{'#ff4444' if is_attack else '#44cc44'};font-weight:bold;">{audio_label}</p>
|
| 720 |
+
<audio controls style="width:100%;">
|
| 721 |
+
<source src="data:audio/wav;base64,{wav_b64}" type="audio/wav">
|
| 722 |
+
</audio>
|
| 723 |
+
</div>'''
|
| 724 |
+
|
| 725 |
+
# Build HTML output
|
| 726 |
+
html_output = f'''
|
| 727 |
+
<div style="background:#0a0a0a;border-radius:12px;padding:16px;text-align:center;">
|
| 728 |
+
<img src="data:image/png;base64,{img_b64}" style="max-width:100%;border-radius:8px;" />
|
| 729 |
+
{audio_html}
|
| 730 |
+
</div>'''
|
| 731 |
|
| 732 |
# Summary
|
| 733 |
status_emoji = "π¨" if is_attack else "β
"
|
|
|
|
| 742 |
| **Risk Score** | {user_result['risk_score']:.2f} |
|
| 743 |
|
| 744 |
**Brain State Distance (L2):** {np.linalg.norm(user_state - normal_state):.3f}
|
|
|
|
|
|
|
| 745 |
"""
|
| 746 |
|
| 747 |
+
return html_output, summary
|
| 748 |
|
| 749 |
except Exception as e:
|
| 750 |
import traceback
|
| 751 |
+
return f"<p style='color:red;'>Error: {str(e)}</p>", f"```\n{traceback.format_exc()}\n```"
|
| 752 |
|
| 753 |
|
| 754 |
# ============================================================
|
|
|
|
| 936 |
brain_submit = gr.Button("π§ Visualize Brain State", variant="primary")
|
| 937 |
|
| 938 |
with gr.Row():
|
| 939 |
+
brain_output_html = gr.HTML(label="Brain State Visualization")
|
|
|
|
|
|
|
|
|
|
| 940 |
|
| 941 |
brain_summary = gr.Markdown(label="Analysis")
|
| 942 |
|
|
|
|
| 945 |
brain_submit.click(
|
| 946 |
fn=image_brain_state,
|
| 947 |
inputs=brain_input,
|
| 948 |
+
outputs=[brain_output_html, brain_summary]
|
| 949 |
)
|
| 950 |
brain_input.submit(
|
| 951 |
fn=image_brain_state,
|
| 952 |
inputs=brain_input,
|
| 953 |
+
outputs=[brain_output_html, brain_summary]
|
| 954 |
)
|
| 955 |
|
| 956 |
gr.Markdown("""
|