# app.py
import gradio as gr
from tabs.speech_stress_analysis import create_voice_stress_tab
from tabs.speech_emotion_recognition import create_emotion_recognition_tab
from tabs.FACS_analysis import create_facs_analysis_tab
from tabs.heart_rate_variability import create_heart_rate_variability_tab
from tabs.deception_detection import create_deception_detection_tab, load_models
import logging
import torch
from typing import Dict
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Custom CSS for better styling
CUSTOM_CSS = """
/* Global styles */
.gradio-container {
font-family: 'Arial', sans-serif;
max-width: 1200px;
margin: auto;
padding: 20px;
background-color: #f8f9fa;
}
/* Header styling */
h1 {
color: #2c3e50;
text-align: center;
padding: 20px 0;
margin-bottom: 30px;
border-bottom: 2px solid #3498db;
}
/* Tab navigation styling */
.gradio-tabs-nav {
background-color: #ffffff;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
margin-bottom: 20px;
}
/* Content areas */
.content-area {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
margin-top: 20px;
}
/* Results area */
.results-area {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
margin-top: 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
/* Disclaimer styling */
.disclaimer {
background-color: #f8f9fa;
border-left: 4px solid #3498db;
padding: 15px;
margin-top: 30px;
font-size: 0.9em;
color: #666;
}
"""
# HTML content
HEADER_HTML = """
AI-Driven Multimodal Emotional State Analysis
Comprehensive analysis of stress, emotion, and truthfulness through facial expressions,
heart rate variability, and speech patterns.
"""
DISCLAIMER_HTML = """
Important Notice
This application provides AI-driven analysis for:
- Stress and emotion detection
- Heart rate variability analysis
- Speech pattern analysis
- Truth/deception indication
Disclaimer: This tool is for research and informational purposes only.
It should not be used as a substitute for professional medical advice, diagnosis, or treatment.
The deception detection feature is experimental and should not be used as definitive proof
of truthfulness or deception.
"""
# Tab structure
TAB_STRUCTURE = [
("Visual Analysis", [
("FACS Analysis", create_facs_analysis_tab),
("Heart Rate Variability", create_heart_rate_variability_tab),
("Truth/Deception Detection", create_deception_detection_tab) # Pass models here
]),
("Speech Analysis", [
("Speech Stress", create_voice_stress_tab),
("Speech Emotion", create_emotion_recognition_tab)
])
]
def create_demo(models: Dict[str, torch.nn.Module]):
"""Create and configure the Gradio interface."""
with gr.Blocks(css=CUSTOM_CSS, title="Multimodal Emotional State Analysis") as demo:
# Header
gr.HTML(HEADER_HTML)
# Main content area with Tabs
with gr.Tabs():
for main_tab, sub_tabs in TAB_STRUCTURE:
with gr.Tab(main_tab):
with gr.Column():
with gr.Tabs():
for sub_tab, create_fn in sub_tabs:
with gr.Tab(sub_tab):
if main_tab == "Visual Analysis" and sub_tab == "Truth/Deception Detection":
# Pass loaded models to the deception detection tab
create_fn(models)
else:
create_fn()
# Add help information below sub-tabs
if main_tab == "Visual Analysis":
gr.Markdown("""
### Visual Analysis Features
- **FACS Analysis**: Facial Action Coding System for emotion detection
- **Heart Rate Variability**: Stress and wellness indicators
- **Truth/Deception Detection**: Physiological response analysis
**For best results:**
1. Use good lighting
2. Face the camera directly
3. Minimize movement during recording
""")
elif main_tab == "Speech Analysis":
gr.Markdown("""
### Speech Analysis Features
- **Speech Stress**: Voice stress analysis
- **Speech Emotion**: Emotional content detection
**For best results:**
1. Use a quiet environment
2. Speak clearly
3. Avoid background noise
""")
# Disclaimer
gr.HTML(DISCLAIMER_HTML)
return demo
def main():
"""Main function to run the application."""
# Load models once and pass them to the deception detection tab
models_loaded = load_models()
if not models_loaded:
logger.error("No models loaded. Exiting application.")
return
# Initialize Gradio interface
demo = create_demo(models_loaded)
# Configure and launch the interface
demo.queue() # Enable queuing without specific concurrency count
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
debug=True,
show_error=True
)
if __name__ == "__main__":
main()