File size: 3,809 Bytes
0e7f9cb
 
 
18ee0d5
30338ee
2af4de1
0e7f9cb
30338ee
 
 
 
0e7f9cb
 
 
 
04a9ec7
 
 
 
 
0e7f9cb
 
30338ee
 
 
 
 
0e7f9cb
 
30338ee
 
 
 
 
 
 
 
 
 
0e7f9cb
30338ee
 
 
 
 
 
 
 
0e7f9cb
30338ee
0e7f9cb
 
 
 
30338ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import gradio as gr
from transformers import pipeline
import torch

# Model ID for Microsoft's Phi-3 (Ungated, high-efficiency SLM)
model_id = "microsoft/Phi-3-mini-4k-instruct"

print("Initializing Digital Vaccine Pipeline...")

# Initialize the pipeline
# We use trust_remote_code=True because Phi-3 uses a custom architecture
pipe = pipeline(
    "text-generation",
    model=model_id,
    device_map="auto",
    model_kwargs={
        "torch_dtype": "auto",
        "trust_remote_code": True,
        "attn_implementation": "eager"  # <--- THIS IS THE FIX
    }
)
def digital_wellness_intervention(user_input, scroll_velocity):
    """
    Determines if the user is in a 'Zombiescroll' state and 
    generates a clinical AI intervention.
    """
    # Threshold for intervention (Simulated Telemetry)
    is_zombiescroll = scroll_velocity > 70 
    
    # Building the Phi-3 prompt template
    if is_zombiescroll:
        system_msg = "You are a clinical AI specialist focused on digital wellness."
        user_msg = f"The user is currently 'doomscrolling' with a high velocity of {scroll_velocity}. Their last stated intent was: '{user_input}'. Provide a 2-sentence Micro-CBT intervention to help them regain consciousness and stop scrolling."
    else:
        system_msg = "You are a supportive digital wellness assistant."
        user_msg = f"The user is browsing intentionally. Their intent is: '{user_input}'. Give a very brief (1 sentence) encouragement to stay focused."

    # Phi-3 specific formatting
    prompt = f"<|system|>\n{system_msg}<|end|>\n<|user|>\n{user_msg}<|end|>\n<|assistant|>\n"
    
    # Generate response
    outputs = pipe(
        prompt, 
        max_new_tokens=100, 
        do_sample=True, 
        temperature=0.7,
        return_full_text=False
    )
    
    intervention_text = outputs[0]["generated_text"].strip()
    
    status = "⚠️ INTERVENTION TRIGGERED" if is_zombiescroll else "βœ… FOCUS MAINTAINED"
    return status, intervention_text

# --- Gradio UI Setup ---
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🧬 The Digital Vaccine: AI-in-the-Loop")
    gr.Markdown(
        "### A Technical Prototype for Behavioral Intervention\n"
        "This system uses a **Small Language Model (Phi-3)** to act as a 'Circuit Breaker' for digital addiction. "
        "By monitoring simulated scroll telemetry, the AI provides real-time clinical nudges."
    )
    
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### User Telemetry")
            input_text = gr.Textbox(
                label="What is your current goal?", 
                placeholder="e.g., Researching LLM quantization..."
            )
            velocity = gr.Slider(
                minimum=0, 
                maximum=100, 
                value=20, 
                label="Scroll Velocity (Dopamine Intensity)"
            )
            btn = gr.Button("Analyze Mental State", variant="primary")
            
        with gr.Column(scale=1):
            gr.Markdown("### AI Diagnostic")
            status_output = gr.Label(label="Current State")
            intervention_output = gr.Textbox(label="MedGemma-style Intervention", lines=4)

    btn.click(
        digital_wellness_intervention, 
        inputs=[input_text, velocity], 
        outputs=[status_output, intervention_output]
    )
    
    gr.Examples(
        examples=[
            ["I'm looking for a recipe for dinner", 25],
            ["Just killing time, feeling bored", 85],
            ["Checking work emails", 40]
        ],
        inputs=[input_text, velocity]
    )

    gr.Markdown("---")
    gr.Markdown("*Technical Note: This prototype utilizes Microsoft's Phi-3-mini (3.8B) for edge-compatible inference.*")

if __name__ == "__main__":
    demo.launch()