Update app.py
Browse files
app.py
CHANGED
@@ -1,174 +1,96 @@
|
|
1 |
-
import os
|
2 |
-
import json
|
3 |
-
import sqlite3
|
4 |
-
from datetime import datetime
|
5 |
import streamlit as st
|
6 |
-
|
7 |
-
|
8 |
-
from
|
9 |
-
|
10 |
-
from
|
11 |
-
from
|
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 |
-
# Function to
|
37 |
-
def
|
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 |
-
st.
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
#
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
st.subheader(f"Hello {username}, start your query below!")
|
102 |
-
|
103 |
-
# Language selection for translation
|
104 |
-
selected_language = st.selectbox("Select the output language", languages, index=languages.index("English"))
|
105 |
-
|
106 |
-
# Input options for the user to type or use voice input
|
107 |
-
input_option = st.radio("Choose Input Method", ("Type your question",))
|
108 |
-
|
109 |
-
# Container to hold the chat interface (for scrolling)
|
110 |
-
chat_container = st.container()
|
111 |
-
|
112 |
-
with chat_container:
|
113 |
-
if "chat_history" in st.session_state:
|
114 |
-
for message in st.session_state.chat_history:
|
115 |
-
if message['role'] == 'user':
|
116 |
-
with st.chat_message("user"):
|
117 |
-
st.markdown(message["content"])
|
118 |
-
elif message['role'] == 'assistant':
|
119 |
-
with st.chat_message("assistant"):
|
120 |
-
st.markdown(message["content"])
|
121 |
-
|
122 |
-
# User input section for typing
|
123 |
-
user_query = None # Initialize user_query as None
|
124 |
-
|
125 |
-
if input_option == "Type your question":
|
126 |
-
user_query = st.chat_input("Ask AI about Bhagavad Gita or Yoga Sutras:") # Chat input for typing
|
127 |
-
|
128 |
-
# If user input is provided, process the query
|
129 |
-
if user_query:
|
130 |
-
with st.spinner("Processing your query... Please wait."):
|
131 |
-
|
132 |
-
# Save user input to chat history in memory
|
133 |
-
st.session_state.chat_history.append({"role": "user", "content": user_query})
|
134 |
-
|
135 |
-
# Display user's message in chatbot (for UI display)
|
136 |
-
with st.chat_message("user"):
|
137 |
-
st.markdown(user_query)
|
138 |
-
|
139 |
-
# Get assistant's response from the chain
|
140 |
-
with st.chat_message("assistant"):
|
141 |
-
response = st.session_state.conversational_chain({"question": user_query})
|
142 |
-
assistant_response = response["answer"]
|
143 |
-
|
144 |
-
# Save assistant's response to chat history in memory
|
145 |
-
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
146 |
-
|
147 |
-
# Format output in JSON
|
148 |
-
formatted_output = {
|
149 |
-
"book": "Bhagavad Gita", # or "PYS" for Yoga Sutras
|
150 |
-
"chapter_number": "2", # Example, replace with actual value from response
|
151 |
-
"verse_number": "47", # Example, replace with actual value from response
|
152 |
-
"shloka": "Yoga karmasu kaushalam", # Example, replace with actual shloka from response
|
153 |
-
"translation": assistant_response,
|
154 |
-
"commentary": "This is a commentary on the shloka.", # Replace with actual commentary
|
155 |
-
"summary": "This is a summary of the chapter." # Replace with actual summary
|
156 |
-
}
|
157 |
-
|
158 |
-
# Save the chat history to the database (SQLite)
|
159 |
-
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
160 |
-
day = datetime.now().strftime("%A") # Get the day of the week (e.g., Monday)
|
161 |
-
save_chat_history(st.session_state.conn, username, timestamp, day, user_query, assistant_response)
|
162 |
-
|
163 |
-
# Translate the assistant's response based on selected language
|
164 |
-
translator = GoogleTranslator(source="en", target=selected_language.lower())
|
165 |
-
translated_response = translator.translate(assistant_response)
|
166 |
-
|
167 |
-
# Display translated response
|
168 |
-
st.markdown(f"**Translated Answer ({selected_language}):** {translated_response}")
|
169 |
-
|
170 |
-
# Display the formatted output
|
171 |
-
st.json(formatted_output)
|
172 |
-
|
173 |
-
# Clear the input field after the query is processed
|
174 |
-
st.session_state.user_input = "" # Reset the input field for next use
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import google.generativeai as genai
|
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.") # Prompt for image upload if none is uploaded
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|