arpita-23 commited on
Commit
27b9386
·
verified ·
1 Parent(s): af77eaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -43
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=os.getenv("GEMINI_API_KEY")) # Use environment variable for security
12
 
13
- # Gemini function for content generation
14
- def get_gemini_response(input_text):
15
  try:
16
  model = genai.GenerativeModel('gemini-pro')
17
- response = model.generate_content(input_text)
18
- return response.text if response else "No response received."
19
  except Exception as e:
20
- st.error(f"Error with Gemini AI: {e}")
21
- return "Error processing request."
 
 
 
 
 
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
- if isinstance(analysis, list) and len(analysis) > 0:
29
- emotions = analysis[0]['emotion']
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
- return None, None
35
 
36
  # Function to analyze detected emotions with LLM
37
- def analyze_emotions_with_llm(dominant_emotion, emotions):
38
- emotions_text = "\n".join([f"{key}: {value:.2f}%" for key, value in emotions.items()])
39
  analysis_prompt = f"""
40
- You are an expert in mental health. Analyze the following detected emotions and provide insights.
41
-
42
  ### Detected Emotions:
43
- {emotions_text}
44
-
45
- ### Questions:
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
- return get_gemini_response(analysis_prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- # Streamlit App UI
53
  st.title("AI-Powered Depression and Emotion Detection System")
54
- st.text("Upload an image to analyze emotions and receive AI-based insights.")
55
 
56
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
57
- if uploaded_file and st.button("Analyze Image"):
58
- image = Image.open(uploaded_file)
59
- st.image(image, caption="Uploaded Image", use_column_width=True)
60
-
61
- emotion, emotions = detect_emotions(image)
62
- if emotion:
63
- st.subheader("Detected Emotions:")
64
- for key, value in emotions.items():
65
- st.write(f"{key.capitalize()}: {value:.2f}%")
66
-
67
- # Get recommendations from AI
68
- response = analyze_emotions_with_llm(emotion, emotions)
69
- st.subheader("AI Analysis & Recommendations:")
70
- st.write(response)
71
- else:
72
- st.error("No emotions detected. Try another image.")
 
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.") #