-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_app.py
More file actions
591 lines (503 loc) Β· 24 KB
/
demo_app.py
File metadata and controls
591 lines (503 loc) Β· 24 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
"""
GemmaSOS Demo App - Interactive Crisis Response Testing
A simplified demo to showcase Gemma on-device crisis response capabilities
"""
import gradio as gr
import torch
import logging
import json
import random
from typing import Dict, List, Any, Optional
from datetime import datetime
import os
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class GemmaSOSDemo:
"""
Demo application for testing Gemma on-device crisis response
"""
def __init__(self):
"""Initialize the demo application"""
self.model = None
self.tokenizer = None
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.model_loaded = False
# Demo scenarios for testing
self.demo_scenarios = self._load_demo_scenarios()
# Crisis detection using Gemma safety features (simplified for demo)
self.safety_categories = {
"self_harm": "Content expressing intent to harm oneself",
"violence": "Content expressing intent to harm others",
"harassment": "Content that is abusive or threatening",
"dangerous_content": "Content promoting dangerous activities"
}
# Crisis resources
self.crisis_resources = {
"general": [
{"name": "National Suicide Prevention Lifeline", "number": "988"},
{"name": "Crisis Text Line", "text": "Text HOME to 741741"},
{"name": "Emergency Services", "number": "911"}
],
"self_harm": [
{"name": "Self-Injury Outreach & Support", "website": "sioutreach.org"}
],
"suicide": [
{"name": "National Suicide Prevention Lifeline", "number": "988"}
],
"violence": [
{"name": "National Domestic Violence Hotline", "number": "1-800-799-7233"}
],
"abuse": [
{"name": "National Domestic Violence Hotline", "number": "1-800-799-7233"}
],
"overdose": [
{"name": "National Poison Control Center", "number": "1-800-222-1222"}
]
}
def _load_demo_scenarios(self) -> List[Dict[str, Any]]:
"""Load demo scenarios for testing"""
return [
{
"id": "scenario_1",
"name": "General Sadness",
"description": "Someone feeling down but not in crisis",
"text": "I'm feeling really sad today. Nothing seems to be going right.",
"expected_crisis": False,
"category": "general"
},
{
"id": "scenario_2",
"name": "Self-Harm Crisis",
"description": "Someone expressing self-harm thoughts",
"text": "I want to cut myself tonight. I can't take this pain anymore.",
"expected_crisis": True,
"category": "self_harm"
},
{
"id": "scenario_3",
"name": "Suicidal Ideation",
"description": "Someone expressing suicidal thoughts",
"text": "I don't want to live anymore. Everyone would be better off without me.",
"expected_crisis": True,
"category": "suicide"
},
{
"id": "scenario_4",
"name": "Violence Threat",
"description": "Someone threatening violence",
"text": "I'm going to hurt someone who deserves it. They'll pay for what they did.",
"expected_crisis": True,
"category": "violence"
},
{
"id": "scenario_5",
"name": "Abuse Situation",
"description": "Someone reporting abuse",
"text": "He hits me every day and I'm scared. I don't know what to do.",
"expected_crisis": True,
"category": "abuse"
},
{
"id": "scenario_6",
"name": "Overdose Situation",
"description": "Someone reporting overdose",
"text": "I took too many pills and I feel really sick. I think I need help.",
"expected_crisis": True,
"category": "overdose"
}
]
def load_gemma_model(self) -> str:
"""Load the Gemma model for demo purposes"""
try:
if self.model_loaded:
return "β
Model already loaded!"
logger.info("Loading Gemma model for demo...")
# For demo purposes, we'll simulate model loading
# In a real implementation, this would load the actual Gemma model
self.model_loaded = True
return f"β
Gemma model loaded successfully on {self.device}!\n\n" \
"Note: This is a demo simulation. In the full system, this would load the actual Gemma-2B model."
except Exception as e:
logger.error(f"Error loading model: {e}")
return f"β Error loading model: {str(e)}"
def detect_crisis_simple(self, text: str) -> Dict[str, Any]:
"""
Simple crisis detection using Gemma safety features for demo purposes
Args:
text: Input text to analyze
Returns:
Crisis detection results
"""
if not text or not text.strip():
return {
"crisis_detected": False,
"categories": [],
"confidence": 0.0,
"message": "No text provided for analysis."
}
# Simulate Gemma safety analysis
safety_analysis = self._simulate_gemma_safety_analysis(text)
# Map safety categories to crisis categories
detected_crises = []
max_confidence = 0.0
for safety_category, safety_data in safety_analysis.items():
if safety_data.get("detected", False) and safety_data.get("confidence", 0) > 0.3:
# Map to crisis category
crisis_category = self._map_safety_to_crisis(safety_category)
if crisis_category:
detected_crises.append({
"category": crisis_category,
"confidence": safety_data["confidence"],
"safety_category": safety_category,
"severity": safety_data.get("severity", "medium"),
"reasoning": safety_data.get("reasoning", "")
})
max_confidence = max(max_confidence, safety_data["confidence"])
return {
"crisis_detected": len(detected_crises) > 0,
"categories": detected_crises,
"confidence": max_confidence,
"safety_analysis": safety_analysis,
"immediate_risk": any(cat.get("severity") == "high" for cat in detected_crises),
"message": f"Safety analysis complete. Found {len(detected_crises)} crisis indicators."
}
def _simulate_gemma_safety_analysis(self, text: str) -> Dict[str, Any]:
"""Simulate Gemma safety analysis for demo"""
text_lower = text.lower()
# Simulate safety analysis for each category
safety_analysis = {}
# Self-harm analysis
self_harm_indicators = ["hurt myself", "cut myself", "kill myself", "end it all", "not worth living", "suicide"]
self_harm_detected = any(indicator in text_lower for indicator in self_harm_indicators)
safety_analysis["self_harm"] = {
"detected": self_harm_detected,
"confidence": 0.8 if self_harm_detected else 0.1,
"severity": "high" if self_harm_detected else "low",
"reasoning": "Self-harm indicators detected" if self_harm_detected else "No self-harm indicators"
}
# Violence analysis
violence_indicators = ["hurt someone", "attack", "fight", "violence", "threaten", "revenge"]
violence_detected = any(indicator in text_lower for indicator in violence_indicators)
safety_analysis["violence"] = {
"detected": violence_detected,
"confidence": 0.7 if violence_detected else 0.1,
"severity": "medium" if violence_detected else "low",
"reasoning": "Violence indicators detected" if violence_detected else "No violence indicators"
}
# Harassment analysis
harassment_indicators = ["abuse", "hit me", "scared", "threaten", "intimidate", "unsafe"]
harassment_detected = any(indicator in text_lower for indicator in harassment_indicators)
safety_analysis["harassment"] = {
"detected": harassment_detected,
"confidence": 0.8 if harassment_detected else 0.1,
"severity": "high" if harassment_detected else "low",
"reasoning": "Harassment indicators detected" if harassment_detected else "No harassment indicators"
}
# Dangerous content analysis
dangerous_indicators = ["overdose", "too many pills", "drugs", "poison", "unconscious", "sick"]
dangerous_detected = any(indicator in text_lower for indicator in dangerous_indicators)
safety_analysis["dangerous_content"] = {
"detected": dangerous_detected,
"confidence": 0.9 if dangerous_detected else 0.1,
"severity": "high" if dangerous_detected else "low",
"reasoning": "Dangerous content indicators detected" if dangerous_detected else "No dangerous content indicators"
}
return safety_analysis
def _map_safety_to_crisis(self, safety_category: str) -> str:
"""Map safety category to crisis category"""
mapping = {
"self_harm": "self_harm",
"violence": "violence",
"harassment": "abuse",
"dangerous_content": "overdose"
}
return mapping.get(safety_category, "general")
def generate_response(self, crisis_result: Dict[str, Any], user_text: str) -> str:
"""Generate empathetic response based on safety-aware crisis detection"""
if not crisis_result["crisis_detected"]:
return self._generate_general_response()
# Get primary crisis category
primary_category = crisis_result["categories"][0]["category"] if crisis_result["categories"] else "general"
immediate_risk = crisis_result.get("immediate_risk", False)
safety_analysis = crisis_result.get("safety_analysis", {})
# Generate response based on category
response_templates = {
"self_harm": {
"immediate": "I can hear that you're in a lot of pain right now. You don't have to go through this alone. Your life has value, even when it doesn't feel that way.",
"supportive": "It takes courage to reach out when you're struggling. I'm glad you did. You're not alone in this."
},
"suicide": {
"immediate": "I'm very concerned about you right now. Your life matters, and I want to help you stay safe. Please know that you don't have to face this alone.",
"supportive": "Thank you for sharing this with me. It takes incredible strength to be so honest. I'm here with you."
},
"violence": {
"immediate": "I'm concerned about your safety. Violence is never the answer, and there are better ways to handle this situation.",
"supportive": "It's okay to feel angry, but we need to find safe ways to express these feelings."
},
"abuse": {
"immediate": "I'm so sorry this is happening to you. You don't deserve to be treated this way. Your safety is the most important thing right now.",
"supportive": "It took courage to share this with me. You're not to blame for what happened. You deserve to be treated with respect."
},
"overdose": {
"immediate": "I'm very concerned about your safety right now. If you've already taken something, please call emergency services immediately.",
"supportive": "I'm glad you're reaching out. You don't have to face this alone. There are people who care about you."
}
}
response_type = "immediate" if immediate_risk else "supportive"
base_response = response_templates.get(primary_category, {}).get(response_type,
"I'm here to listen and support you. You don't have to face this alone.")
# Add resources
resources = self.crisis_resources.get(primary_category, self.crisis_resources["general"])
resource_text = "\n\n**Resources that might help:**\n"
for resource in resources[:3]: # Show top 3 resources
if resource.get("number"):
resource_text += f"β’ {resource['name']}: {resource['number']}\n"
elif resource.get("text"):
resource_text += f"β’ {resource['name']}: {resource['text']}\n"
elif resource.get("website"):
resource_text += f"β’ {resource['name']}: {resource['website']}\n"
# Add emergency notice if immediate risk
if immediate_risk:
emergency_notice = "\n\nπ¨ **If you're in immediate danger, please call 911 or 988 right now.**"
else:
emergency_notice = ""
return f"{base_response}{resource_text}{emergency_notice}"
def _generate_general_response(self) -> str:
"""Generate general supportive response for non-crisis situations"""
return """No crisis indicators detected. This appears to be a general message.
If you're experiencing distress or need someone to talk to, please don't hesitate to reach out for support. Your feelings are valid and important.
**General Resources:**
β’ National Suicide Prevention Lifeline: 988
β’ Crisis Text Line: Text HOME to 741741
β’ Emergency Services: 911"""
def load_demo_scenario(self, scenario_id: str) -> str:
"""Load a demo scenario for testing"""
scenario = next((s for s in self.demo_scenarios if s["id"] == scenario_id), None)
if scenario:
return scenario["text"]
return "Scenario not found."
def get_demo_scenarios(self) -> List[Dict[str, str]]:
"""Get list of available demo scenarios"""
return [
{"id": scenario["id"], "name": scenario["name"], "description": scenario["description"]}
for scenario in self.demo_scenarios
]
def create_demo_interface():
"""Create the demo interface"""
demo = GemmaSOSDemo()
# Custom CSS for demo styling
css = """
.demo-container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.crisis-alert {
background-color: #ffebee;
border-left: 4px solid #f44336;
padding: 15px;
margin: 10px 0;
border-radius: 4px;
}
.no-crisis {
background-color: #e8f5e8;
border-left: 4px solid #4caf50;
padding: 15px;
margin: 10px 0;
border-radius: 4px;
}
.demo-section {
background-color: #f5f5f5;
padding: 20px;
margin: 20px 0;
border-radius: 8px;
}
.resource-box {
background-color: #e3f2fd;
border: 1px solid #2196f3;
padding: 10px;
margin: 10px 0;
border-radius: 4px;
}
"""
with gr.Blocks(css=css, title="GemmaSOS Demo - Crisis Response Testing") as interface:
gr.Markdown("""
# π GemmaSOS Demo - On-Device Crisis Response Testing
**Test Google's Gemma model for crisis detection and response generation**
This demo showcases how the Gemma lightweight model can detect crisis situations and generate empathetic responses entirely on your device.
""")
with gr.Row():
with gr.Column(scale=2):
# Model Loading Section
with gr.Group():
gr.Markdown("### π€ Model Loading")
load_model_btn = gr.Button("Load Gemma Model", variant="primary", size="lg")
model_status = gr.Textbox(
label="Model Status",
value="Model not loaded. Click 'Load Gemma Model' to start.",
interactive=False,
lines=4
)
# Text Input Section
with gr.Group():
gr.Markdown("### π Test Crisis Detection")
# Demo scenarios dropdown
scenario_dropdown = gr.Dropdown(
choices=[(s["name"], s["id"]) for s in demo.get_demo_scenarios()],
label="Load Demo Scenario",
value=None
)
load_scenario_btn = gr.Button("Load Scenario", variant="secondary")
# Text input
text_input = gr.Textbox(
label="Enter text to analyze",
placeholder="Type your message here or load a demo scenario...",
lines=4
)
# Analysis buttons
with gr.Row():
analyze_btn = gr.Button("Analyze for Crisis", variant="primary")
clear_btn = gr.Button("Clear All", variant="secondary")
# Results Section
with gr.Group():
gr.Markdown("### π Safety Analysis Results")
crisis_detection = gr.JSON(
label="Crisis Detection Results",
value={}
)
safety_analysis = gr.JSON(
label="Gemma Safety Analysis",
value={}
)
# Response Section
with gr.Group():
gr.Markdown("### π¬ Generated Response")
response_output = gr.Textbox(
label="Empathetic Response",
lines=8,
interactive=False
)
with gr.Column(scale=1):
# Demo Information
with gr.Group():
gr.Markdown("### π Demo Information")
gr.Markdown("""
**What this demo shows:**
1. **Safety Analysis**: Uses Gemma's built-in safety features
2. **Crisis Detection**: Maps safety categories to crisis types
3. **Response Generation**: Creates safety-aware empathetic responses
4. **Resource Matching**: Provides relevant help resources
**Safety Categories Analyzed:**
- Self-harm content
- Violence threats
- Harassment/abuse
- Dangerous content
**Crisis Types Mapped:**
- Self-harm & suicide risk
- Violence threats
- Abuse situations
- Overdose scenarios
""")
# Privacy Notice
with gr.Group():
gr.Markdown("### π Privacy & Security")
gr.Markdown("""
**On-Device Processing:**
- All analysis happens locally
- No data sent to external servers
- Complete privacy protection
- Temporary processing only
""")
# Emergency Resources
with gr.Group():
gr.Markdown("### π¨ Emergency Resources")
gr.Markdown("""
**If you're in immediate danger:**
- Call 911
- Call 988 (Suicide & Crisis Lifeline)
- Text HOME to 741741
**24/7 Support:**
- National Suicide Prevention Lifeline: 988
- Crisis Text Line: Text HOME to 741741
- National Domestic Violence Hotline: 1-800-799-7233
""")
# Event handlers
def load_model():
return demo.load_gemma_model()
def load_scenario(scenario_id):
if scenario_id:
return demo.load_demo_scenario(scenario_id)
return ""
def analyze_text(text):
if not text.strip():
return {}, {}, "Please enter some text to analyze."
# Detect crisis using safety analysis
crisis_result = demo.detect_crisis_simple(text)
# Only generate response if crisis is detected
if crisis_result["crisis_detected"]:
response = demo.generate_response(crisis_result, text)
else:
response = "No crisis indicators detected. This appears to be a general message. If you're experiencing distress, please don't hesitate to reach out for support."
# Extract safety analysis
safety_analysis = crisis_result.get("safety_analysis", {})
return crisis_result, safety_analysis, response
def clear_and_analyze(text):
"""Clear response and analyze new text"""
if not text.strip():
return {}, {}, "Please enter some text to analyze."
# Detect crisis using safety analysis
crisis_result = demo.detect_crisis_simple(text)
# Only generate response if crisis is detected
if crisis_result["crisis_detected"]:
response = demo.generate_response(crisis_result, text)
else:
response = "No crisis indicators detected. This appears to be a general message. If you're experiencing distress, please don't hesitate to reach out for support."
# Extract safety analysis
safety_analysis = crisis_result.get("safety_analysis", {})
return crisis_result, safety_analysis, response
def clear_all():
"""Clear all outputs"""
return {}, {}, "", ""
# Connect events
load_model_btn.click(
load_model,
outputs=[model_status]
)
load_scenario_btn.click(
load_scenario,
inputs=[scenario_dropdown],
outputs=[text_input]
)
analyze_btn.click(
clear_and_analyze,
inputs=[text_input],
outputs=[crisis_detection, safety_analysis, response_output]
)
text_input.submit(
clear_and_analyze,
inputs=[text_input],
outputs=[crisis_detection, safety_analysis, response_output]
)
clear_btn.click(
clear_all,
outputs=[crisis_detection, safety_analysis, response_output, text_input]
)
return interface
def main():
"""Main function to run the demo"""
try:
interface = create_demo_interface()
# Launch the demo
interface.launch(
server_name="0.0.0.0",
server_port=7861, # Different port from main app
share=False,
show_error=True
)
except KeyboardInterrupt:
logger.info("Demo interrupted by user")
except Exception as e:
logger.error(f"Demo error: {e}")
if __name__ == "__main__":
main()