|
import streamlit as st |
|
import google.generativeai as genai |
|
import os |
|
from PIL import Image |
|
import numpy as np |
|
from deepface import DeepFace |
|
from dotenv import load_dotenv |
|
|
|
|
|
print("DeepFace is installed and ready to use!") |
|
print("Google Generative AI module is successfully imported!") |
|
|
|
|
|
load_dotenv() |
|
genai.configure(api_key="AIzaSyAEzZLb7R1CNTWwFXoUsWNrV47X9JgGu1o") |
|
|
|
|
|
def get_gemini_response(input): |
|
try: |
|
model = genai.GenerativeModel('gemini-pro') |
|
response = model.generate_content(input) |
|
return response |
|
except Exception as e: |
|
|
|
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 |
|
|
|
|
|
def detect_emotions(image): |
|
try: |
|
|
|
image_array = np.array(image) |
|
|
|
analysis = DeepFace.analyze(image_array, actions=['emotion'], enforce_detection=False) |
|
|
|
return analysis[0]['dominant_emotion'], analysis[0]['emotion'] |
|
except Exception as e: |
|
st.error(f"Error during emotion detection: {e}") |
|
return None, None |
|
|
|
|
|
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 |
|
|
|
|
|
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.") |
|
|
|
|
|
st.title("AI-Powered Depression and Emotion Detection System") |
|
st.text("Use the AI system for detecting depression and emotions from images.") |
|
|
|
|
|
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) |
|
emotion, emotions = detect_emotions(image) |
|
if emotion: |
|
response = analyze_emotions_with_llm(emotion, emotions) |
|
display_response_content(response) |
|
else: |
|
st.write("No emotions detected in the image.") |
|
else: |
|
st.write("Please upload an image first.") |