File size: 4,068 Bytes
3053308
45f7e5b
 
 
 
27b9386
45f7e5b
 
27b9386
 
 
 
45f7e5b
 
27b9386
45f7e5b
27b9386
 
45f7e5b
 
27b9386
 
45f7e5b
27b9386
 
 
 
 
 
 
45f7e5b
 
 
 
27b9386
45f7e5b
27b9386
45f7e5b
27b9386
 
45f7e5b
 
27b9386
45f7e5b
 
27b9386
 
45f7e5b
27b9386
45f7e5b
27b9386
 
 
45f7e5b
27b9386
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45f7e5b
27b9386
45f7e5b
27b9386
45f7e5b
27b9386
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import google.generativeai as genai
import os
from PIL import Image
import numpy as np
from deepface import DeepFace  # Replacing FER with DeepFace
from dotenv import load_dotenv

# Print out successful imports
print("DeepFace is installed and ready to use!")
print("Google Generative AI module is successfully imported!")

# Load API keys and environment variables
load_dotenv()
genai.configure(api_key="AIzaSyAEzZLb7R1CNTWwFXoUsWNrV47X9JgGu1o")

# gemini function for general content generation
def get_gemini_response(input):
    try:
        model = genai.GenerativeModel('gemini-pro')
        response = model.generate_content(input)
        return response
    except Exception as e:
        # Handle quota exceeded error
        if "RATE_LIMIT_EXCEEDED" in str(e):
            st.error("Quota exceeded for content generation. Please try again later.")
            return None
        else:
            st.error(f"Error: {e}")
            return None

# Function to analyze image for depression and emotion detection using DeepFace
def detect_emotions(image):
    try:
        # Convert PIL Image to NumPy array
        image_array = np.array(image)
        # Use DeepFace to analyze emotions
        analysis = DeepFace.analyze(image_array, actions=['emotion'], enforce_detection=False)
        # Return the dominant emotion and its score
        return analysis[0]['dominant_emotion'], analysis[0]['emotion']
    except Exception as e:
        st.error(f"Error during emotion detection: {e}")
        return None, None

# Function to analyze detected emotions with LLM
def analyze_emotions_with_llm(emotion, emotions):
    emotion_analysis = f"{emotion}: {emotions[emotion]:.2f}"
    analysis_prompt = f"""
    ### As a mental health and emotional well-being expert, analyze the following detected emotions.
    ### Detected Emotions:
    {emotion_analysis}
    ### Analysis Output:
    1. Identify any potential signs of depression based on the detected emotions.
    """
    response = get_gemini_response(analysis_prompt)
    return response

# Function to parse and display response content
def display_response_content(response):
    st.subheader("Response Output")
    if response and hasattr(response, 'candidates'):
        response_content = response.candidates[0].content.parts[0].text if response.candidates[0].content.parts else ""
        sections = response_content.split('###')
        for section in sections:
            if section.strip():
                section_lines = section.split('\n')
                section_title = section_lines[0].strip()
                section_body = '\n'.join(line.strip() for line in section_lines[1:] if line.strip())
                if section_title:
                    st.markdown(f"**{section_title}**")
                if section_body:
                    st.write(section_body)
    else:
        st.write("No response received from the model or quota exceeded.")

# Streamlit App
st.title("AI-Powered Depression and Emotion Detection System")
st.text("Use the AI system for detecting depression and emotions from images.")

# Tabs for different functionalities (only image analysis in this version)
with st.container():
    st.header("Image Analysis")
    uploaded_file = st.file_uploader("Upload an image for analysis", type=["jpg", "jpeg", "png"], help="Please upload an image file.")
    submit_image = st.button('Analyze Image')

    if submit_image:
        if uploaded_file is not None:
            image = Image.open(uploaded_file)  # Open the uploaded image
            emotion, emotions = detect_emotions(image)  # Detect emotions using DeepFace
            if emotion:  # If emotions are detected
                response = analyze_emotions_with_llm(emotion, emotions)  # Analyze detected emotions with LLM
                display_response_content(response)  # Display the analysis response
            else:
                st.write("No emotions detected in the image.")  # If no emotion is detected
        else:
            st.write("Please upload an image first.")  #