Skip to content

hashimaawan/AI_Emotional_Support

Repository files navigation

🎭 AI Emotion Companion

Python Streamlit Llama License

Multimodal AI system for emotion detection and mental wellness support

Powered by Llama 3.2, DeepFace, and DistilRoBERTa

DemoFeaturesInstallationUsageArchitecture


📝 Description

An intelligent emotional support companion that uses voice, facial expression, and text analysis to detect emotions and provide personalized empathetic responses powered by Llama 3.2. Built for mental wellness with real-time AI-driven coping strategies.


✨ Features

🎯 Core Capabilities

  • 🎤 Voice Emotion Detection - Speak naturally and get instant emotion analysis
  • 📸 Facial Expression Analysis - Upload photos or use webcam for emotion recognition
  • 💬 Text-based Chat - Type your feelings and receive empathetic AI responses
  • 🤖 Llama 3.2 Integration - Dynamic, personalized responses (not pre-written templates!)
  • 📊 Emotion Tracking - Monitor your emotional journey with analytics
  • 🧘 Smart Coping Strategies - AI-generated wellness techniques tailored to your emotion

🔬 AI Models Used

Component Technology Purpose
LLM Llama 3.2 Empathetic response generation
Text Emotion DistilRoBERTa Sentiment analysis from text
Facial Recognition DeepFace Emotion detection from images
Speech-to-Text Google Speech API Voice transcription
Text-to-Speech pyttsx3 Voice output

🎥 Demo

Text Chat

User: "I'm feeling really stressed about my presentation tomorrow"

AI: I can sense the pressure you're under. Presenting can be nerve-wracking, 
    but remember that feeling anxious shows you care about doing well. 
    You've got this! 💙

Detected: fear (87% confidence)

Coping Strategy: Try the 5-4-3-2-1 grounding technique:
                 Notice 5 things you see, 4 you touch, 3 you hear,
                 2 you smell, and 1 you taste.

Voice Input

  • 🎤 Click record → Speak your feelings
  • 🧠 AI detects emotion from your words
  • 💬 Receive empathetic response
  • 🔊 Hear AI speak back to you (optional)

Facial Analysis

  • 📸 Upload photo or use webcam
  • 👁️ AI analyzes facial expression
  • 😊 Detects: happy, sad, angry, fear, surprise, disgust, neutral
  • 🎯 Provides emotion-specific support

🚀 Installation

Prerequisites

  • Python 3.8+
  • Ollama (for Llama 3.2)
  • Microphone (for voice input)
  • Webcam (optional, for facial capture)

Quick Start

# 1. Clone repository
git clone https://github.com/yourusername/ai-emotion-companion.git
cd ai-emotion-companion

# 2. Create virtual environment
python -m venv emotion_env
source emotion_env/bin/activate  # On Windows: emotion_env\Scripts\activate

# 3. Install dependencies
pip install -r requirements.txt

# 4. Install Llama 3.2
# Download Ollama from https://ollama.ai
ollama pull llama3.2

# 5. Run the app
streamlit run app.py

Platform-Specific Setup

🍎 macOS

brew install portaudio
pip install pyaudio

🐧 Linux

sudo apt-get install python3-pyaudio portaudio19-dev
pip install pyaudio

🪟 Windows

pip install pyaudio

💻 Usage

Starting the Application

# Terminal 1: Start Ollama server
ollama serve

# Terminal 2: Run Streamlit app
streamlit run app.py

Open your browser at http://localhost:8501

Using Different Modes

Voice Input

  1. Navigate to 🎤 Voice Input tab
  2. Click "Start Recording"
  3. Speak for 3-10 seconds
  4. View emotion analysis and AI response
  5. (Optional) Enable voice response to hear AI speak

Facial Analysis

  1. Navigate to 📸 Facial Analysis tab
  2. Choose "Upload Image" or "Use Webcam"
  3. Upload/capture your photo
  4. View detected emotion and personalized support

Text Chat

  1. Navigate to 💬 Text Chat tab
  2. Type your message in the chat input
  3. Receive emotion analysis and empathetic response
  4. Continue the conversation naturally

View Statistics

  1. Navigate to 📊 Your Stats tab
  2. See emotion distribution charts
  3. Track your emotional journey over time

🏗️ Architecture

Project Structure

emotion-ai-companion/
│
├── app.py                          # Main Streamlit application
├── emotion_detector.py             # Emotion detection (text/voice/facial)
├── llm_response_generator.py      # Llama 3.2 integration
├── utils.py                        # Helper utilities
├── requirements.txt                # Python dependencies
└── README.md                       # Documentation

Component Diagram

┌─────────────────────────────────────────────────────────┐
│                      USER INTERFACE                      │
│                     (Streamlit - app.py)                 │
└────────────┬────────────────────────────┬────────────────┘
             │                            │
             v                            v
┌────────────────────────┐    ┌──────────────────────────┐
│  EMOTION DETECTION     │    │  LLM RESPONSE GENERATOR  │
│  emotion_detector.py   │    │  llm_response_generator  │
│                        │    │                          │
│  • Text Analysis       │───→│  • Empathy Generation    │
│  • Voice Recognition   │    │  • Coping Strategies     │
│  • Facial Detection    │    │  • Llama 3.2 API         │
└────────────┬───────────┘    └──────────┬───────────────┘
             │                           │
             v                           v
┌────────────────────────────────────────────────────────┐
│                    UTILITIES                           │
│                   (utils.py)                           │
│                                                        │
│  • Voice Output (TTS)                                 │
│  • Webcam Capture                                     │
│  • Session Management                                 │
│  • Data Formatting                                    │
└────────────────────────────────────────────────────────┘

Data Flow

User Input (Voice/Text/Image)
         ↓
Emotion Detection (DistilRoBERTa / DeepFace)
         ↓
Emotion Classification (angry/sad/happy/etc.)
         ↓
LLM Processing (Llama 3.2)
         ↓
Personalized Response + Coping Strategy
         ↓
Display to User + Save to History

🧠 How It Works

Text Emotion Detection

# Uses DistilRoBERTa fine-tuned on emotion classification
text = "I'm feeling anxious"
emotion, confidence = detector.detect_text_emotion(text)
# Output: ('fear', 0.89)

Facial Emotion Detection

# Uses DeepFace CNN model
image = load_image("photo.jpg")
emotion, confidence = detector.detect_facial_emotion(image)
# Output: ('sad', 0.76)

Llama 3.2 Response Generation

# Sends context-aware prompt to Llama
prompt = f"""
User said: "{user_message}"
Detected emotion: {emotion} (confidence: {confidence})

Provide a compassionate, supportive response.
"""

response = llama.generate(prompt)
# Output: Personalized empathetic message

🛠️ Configuration

Customizing Llama Responses

Edit llm_response_generator.py:

# Adjust temperature for response creativity
"options": {
    "temperature": 0.7,  # 0.0 = deterministic, 1.0 = creative
    "max_tokens": 150    # Response length
}

Adding New Emotions

Edit emotion_detector.py:

EMOTION_MAPPING = {
    'angry': 'angry',
    'happy': 'happy',
}

☁️ Deployment

Docker (Coming Soon)

docker build -t emotion-companion .
docker run -p 8501:8501 emotion-companion

🧪 Testing

Test Individual Components

# Test emotion detection
python -c "from emotion_detector import EmotionDetector; d = EmotionDetector(); print(d.detect_text_emotion('I am happy'))"

# Test LLM connection
python -c "from llm_response_generator import LLMResponseGenerator; l = LLMResponseGenerator(); print(l.check_llm_available())"

# Test microphone
python -c "import speech_recognition as sr; print(sr.Microphone.list_microphone_names())"

# Test webcam
python -c "import cv2; cap = cv2.VideoCapture(0); print('OK' if cap.isOpened() else 'Failed')"

🐛 Troubleshooting

Llama Not Connecting

# Check Ollama is running
curl http://localhost:11434/api/tags

# Restart Ollama
ollama serve

# Verify model is installed
ollama list

Microphone Issues

# List available microphones
python -c "import speech_recognition as sr; print(sr.Microphone.list_microphone_names())"

# Grant microphone permissions in system settings

DeepFace Errors

# Reinstall with specific backend
pip uninstall deepface tf-keras tensorflow
pip install deepface tf-keras tensorflow

Import Errors

# Force reinstall all dependencies
pip install --force-reinstall -r requirements.txt

📊 Performance

Component Response Time Accuracy
Text Emotion ~0.1s 85-92%
Facial Emotion ~1-2s 78-88%
Voice Transcription ~2-3s 80-95%
Llama Response ~2-5s N/A (generative)

🤝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit changes (git commit -m 'Add AmazingFeature')
  4. Push to branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Development Setup

# Install dev dependencies
pip install black flake8 pytest

# Run formatter
black *.py

# Run linter
flake8 *.py

# Run tests
pytest tests/

⚠️ Important Disclaimers

This AI is NOT a substitute for professional mental health care.

When to Seek Professional Help

If you're experiencing:

  • 🚨 Thoughts of self-harm or suicide
  • 🚨 Severe depression or anxiety
  • 🚨 Mental health crisis
  • 🚨 Trauma or abuse

Crisis Resources

  • 🌍 International Association for Suicide Prevention: iasp.info
  • 🌐 Befrienders Worldwide: befrienders.org
  • 🏥 Emergency Services: Call your local emergency number

This tool is designed for emotional wellness support only, not medical diagnosis or treatment.


📜 License

This project is licensed under the MIT License.

🙏 Acknowledgments

  • Hugging Face - DistilRoBERTa emotion model
  • Meta AI - Llama 3.2 language model
  • DeepFace - Facial emotion recognition
  • Streamlit - Web application framework
  • Ollama - Local LLM inference

📧 Contact

Project Maintainer: Muhammad Hashim Awan


🌟 Star History

If this project helped you, please consider giving it a ⭐!

🎓 Citation

If you use this project in your research, please cite:

About

AI-powered emotional support system combining Llama 3.2, DeepFace facial recognition, and DistilRoBERTa for multimodal emotion detection and personalized wellness guidance.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages