Update app.py
Browse files
app.py
CHANGED
@@ -3,70 +3,94 @@ import google.generativeai as genai
|
|
3 |
import os
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
-
from deepface import DeepFace
|
7 |
from dotenv import load_dotenv
|
8 |
|
|
|
|
|
|
|
|
|
9 |
# Load API keys and environment variables
|
10 |
load_dotenv()
|
11 |
-
genai.configure(api_key=
|
12 |
|
13 |
-
#
|
14 |
-
def get_gemini_response(
|
15 |
try:
|
16 |
model = genai.GenerativeModel('gemini-pro')
|
17 |
-
response = model.generate_content(
|
18 |
-
return response
|
19 |
except Exception as e:
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# Function to analyze image for depression and emotion detection using DeepFace
|
24 |
def detect_emotions(image):
|
25 |
try:
|
|
|
26 |
image_array = np.array(image)
|
|
|
27 |
analysis = DeepFace.analyze(image_array, actions=['emotion'], enforce_detection=False)
|
28 |
-
|
29 |
-
|
30 |
-
dominant_emotion = max(emotions, key=emotions.get) # Get emotion with highest percentage
|
31 |
-
return dominant_emotion, emotions
|
32 |
except Exception as e:
|
33 |
st.error(f"Error during emotion detection: {e}")
|
34 |
-
|
35 |
|
36 |
# Function to analyze detected emotions with LLM
|
37 |
-
def analyze_emotions_with_llm(
|
38 |
-
|
39 |
analysis_prompt = f"""
|
40 |
-
|
41 |
-
|
42 |
### Detected Emotions:
|
43 |
-
{
|
44 |
-
|
45 |
-
|
46 |
-
1. What does the dominant emotion '{dominant_emotion}' indicate?
|
47 |
-
2. Could these emotions be potential signs of depression?
|
48 |
-
3. What general recommendations would you give based on these emotions?
|
49 |
"""
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
# Streamlit App
|
53 |
st.title("AI-Powered Depression and Emotion Detection System")
|
54 |
-
st.text("
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
st.
|
60 |
-
|
61 |
-
|
62 |
-
if
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
3 |
import os
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
+
from deepface import DeepFace # Replacing FER with DeepFace
|
7 |
from dotenv import load_dotenv
|
8 |
|
9 |
+
# Print out successful imports
|
10 |
+
print("DeepFace is installed and ready to use!")
|
11 |
+
print("Google Generative AI module is successfully imported!")
|
12 |
+
|
13 |
# Load API keys and environment variables
|
14 |
load_dotenv()
|
15 |
+
genai.configure(api_key="AIzaSyAEzZLb7R1CNTWwFXoUsWNrV47X9JgGu1o")
|
16 |
|
17 |
+
# gemini function for general content generation
|
18 |
+
def get_gemini_response(input):
|
19 |
try:
|
20 |
model = genai.GenerativeModel('gemini-pro')
|
21 |
+
response = model.generate_content(input)
|
22 |
+
return response
|
23 |
except Exception as e:
|
24 |
+
# Handle quota exceeded error
|
25 |
+
if "RATE_LIMIT_EXCEEDED" in str(e):
|
26 |
+
st.error("Quota exceeded for content generation. Please try again later.")
|
27 |
+
return None
|
28 |
+
else:
|
29 |
+
st.error(f"Error: {e}")
|
30 |
+
return None
|
31 |
|
32 |
# Function to analyze image for depression and emotion detection using DeepFace
|
33 |
def detect_emotions(image):
|
34 |
try:
|
35 |
+
# Convert PIL Image to NumPy array
|
36 |
image_array = np.array(image)
|
37 |
+
# Use DeepFace to analyze emotions
|
38 |
analysis = DeepFace.analyze(image_array, actions=['emotion'], enforce_detection=False)
|
39 |
+
# Return the dominant emotion and its score
|
40 |
+
return analysis[0]['dominant_emotion'], analysis[0]['emotion']
|
|
|
|
|
41 |
except Exception as e:
|
42 |
st.error(f"Error during emotion detection: {e}")
|
43 |
+
return None, None
|
44 |
|
45 |
# Function to analyze detected emotions with LLM
|
46 |
+
def analyze_emotions_with_llm(emotion, emotions):
|
47 |
+
emotion_analysis = f"{emotion}: {emotions[emotion]:.2f}"
|
48 |
analysis_prompt = f"""
|
49 |
+
### As a mental health and emotional well-being expert, analyze the following detected emotions.
|
|
|
50 |
### Detected Emotions:
|
51 |
+
{emotion_analysis}
|
52 |
+
### Analysis Output:
|
53 |
+
1. Identify any potential signs of depression based on the detected emotions.
|
|
|
|
|
|
|
54 |
"""
|
55 |
+
response = get_gemini_response(analysis_prompt)
|
56 |
+
return response
|
57 |
+
|
58 |
+
# Function to parse and display response content
|
59 |
+
def display_response_content(response):
|
60 |
+
st.subheader("Response Output")
|
61 |
+
if response and hasattr(response, 'candidates'):
|
62 |
+
response_content = response.candidates[0].content.parts[0].text if response.candidates[0].content.parts else ""
|
63 |
+
sections = response_content.split('###')
|
64 |
+
for section in sections:
|
65 |
+
if section.strip():
|
66 |
+
section_lines = section.split('\n')
|
67 |
+
section_title = section_lines[0].strip()
|
68 |
+
section_body = '\n'.join(line.strip() for line in section_lines[1:] if line.strip())
|
69 |
+
if section_title:
|
70 |
+
st.markdown(f"**{section_title}**")
|
71 |
+
if section_body:
|
72 |
+
st.write(section_body)
|
73 |
+
else:
|
74 |
+
st.write("No response received from the model or quota exceeded.")
|
75 |
|
76 |
+
# Streamlit App
|
77 |
st.title("AI-Powered Depression and Emotion Detection System")
|
78 |
+
st.text("Use the AI system for detecting depression and emotions from images.")
|
79 |
|
80 |
+
# Tabs for different functionalities (only image analysis in this version)
|
81 |
+
with st.container():
|
82 |
+
st.header("Image Analysis")
|
83 |
+
uploaded_file = st.file_uploader("Upload an image for analysis", type=["jpg", "jpeg", "png"], help="Please upload an image file.")
|
84 |
+
submit_image = st.button('Analyze Image')
|
85 |
+
|
86 |
+
if submit_image:
|
87 |
+
if uploaded_file is not None:
|
88 |
+
image = Image.open(uploaded_file) # Open the uploaded image
|
89 |
+
emotion, emotions = detect_emotions(image) # Detect emotions using DeepFace
|
90 |
+
if emotion: # If emotions are detected
|
91 |
+
response = analyze_emotions_with_llm(emotion, emotions) # Analyze detected emotions with LLM
|
92 |
+
display_response_content(response) # Display the analysis response
|
93 |
+
else:
|
94 |
+
st.write("No emotions detected in the image.") # If no emotion is detected
|
95 |
+
else:
|
96 |
+
st.write("Please upload an image first.") #
|