Spaces:
Sleeping
Sleeping
Update src/app.py
Browse files- src/app.py +28 -15
src/app.py
CHANGED
|
@@ -22,6 +22,10 @@ st.set_page_config(
|
|
| 22 |
layout="wide"
|
| 23 |
)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# Title and description
|
| 26 |
st.title("🤖 Empathetic AI Assistant")
|
| 27 |
st.markdown("""
|
|
@@ -40,18 +44,26 @@ col1, col2 = st.columns([1, 1])
|
|
| 40 |
|
| 41 |
with col1:
|
| 42 |
st.subheader("📸 1. Capture Your Expression")
|
| 43 |
-
camera_image = st.camera_input("Take a snapshot")
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
st.
|
| 52 |
-
st.
|
| 53 |
-
# --- END: ADDED DEBUG BLOCK ---
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
with col2:
|
| 56 |
st.subheader("💭 2. Your Query")
|
| 57 |
user_query = st.text_area(
|
|
@@ -71,18 +83,19 @@ st.divider()
|
|
| 71 |
# Main action button
|
| 72 |
if st.button("🧠 Analyze My Emotion & Answer", type="primary", use_container_width=True):
|
| 73 |
|
| 74 |
-
# Validation
|
| 75 |
-
if
|
| 76 |
st.error("❌ Please take a snapshot using the camera first!")
|
| 77 |
elif not audio_bytes:
|
| 78 |
st.error("❌ Please record your voice first!")
|
| 79 |
elif not user_query.strip():
|
| 80 |
st.error("❌ Please enter your query!")
|
| 81 |
else:
|
| 82 |
-
# Step 1: Process camera image
|
| 83 |
with st.spinner("📸 Processing facial expression..."):
|
| 84 |
try:
|
| 85 |
-
|
|
|
|
| 86 |
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 87 |
|
| 88 |
with tempfile.NamedTemporaryFile(delete=True, suffix=".jpg") as temp_img:
|
|
@@ -155,7 +168,7 @@ if st.button("🧠 Analyze My Emotion & Answer", type="primary", use_container_w
|
|
| 155 |
with st.sidebar:
|
| 156 |
st.header("ℹ️ How to Use")
|
| 157 |
st.markdown("""
|
| 158 |
-
1. **Take a snapshot**
|
| 159 |
2. **Type your query**
|
| 160 |
3. **Click the mic** to record (click again to stop)
|
| 161 |
4. **Click the 'Analyze' button**
|
|
|
|
| 22 |
layout="wide"
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# Initialize session state for the camera image if it doesn't exist
|
| 26 |
+
if 'captured_image_bytes' not in st.session_state:
|
| 27 |
+
st.session_state.captured_image_bytes = None
|
| 28 |
+
|
| 29 |
# Title and description
|
| 30 |
st.title("🤖 Empathetic AI Assistant")
|
| 31 |
st.markdown("""
|
|
|
|
| 44 |
|
| 45 |
with col1:
|
| 46 |
st.subheader("📸 1. Capture Your Expression")
|
|
|
|
| 47 |
|
| 48 |
+
# Use a key to give the camera_input a unique identity
|
| 49 |
+
# The output of the camera_input is stored in 'camera_input_data'
|
| 50 |
+
camera_input_data = st.camera_input("Take a snapshot", key="my_camera_input")
|
| 51 |
+
|
| 52 |
+
# If new data comes from camera_input, update session state
|
| 53 |
+
if camera_input_data is not None and camera_input_data != st.session_state.captured_image_bytes:
|
| 54 |
+
st.session_state.captured_image_bytes = camera_input_data
|
| 55 |
+
st.experimental_rerun() # Rerun to display the stored image immediately
|
|
|
|
| 56 |
|
| 57 |
+
# Display the stored image if available
|
| 58 |
+
if st.session_state.captured_image_bytes:
|
| 59 |
+
st.image(st.session_state.captured_image_bytes, caption="Captured Image")
|
| 60 |
+
st.success("DEBUG: Image captured and stored in session state!")
|
| 61 |
+
if st.button("Clear Photo", key="clear_camera_btn"):
|
| 62 |
+
st.session_state.captured_image_bytes = None
|
| 63 |
+
st.experimental_rerun() # Rerun to clear the display
|
| 64 |
+
else:
|
| 65 |
+
st.warning("DEBUG: 'captured_image_bytes' is currently None. Waiting for user to take photo...")
|
| 66 |
+
|
| 67 |
with col2:
|
| 68 |
st.subheader("💭 2. Your Query")
|
| 69 |
user_query = st.text_area(
|
|
|
|
| 83 |
# Main action button
|
| 84 |
if st.button("🧠 Analyze My Emotion & Answer", type="primary", use_container_width=True):
|
| 85 |
|
| 86 |
+
# Validation - now check against session state
|
| 87 |
+
if st.session_state.captured_image_bytes is None: # <--- CHECK SESSION STATE HERE
|
| 88 |
st.error("❌ Please take a snapshot using the camera first!")
|
| 89 |
elif not audio_bytes:
|
| 90 |
st.error("❌ Please record your voice first!")
|
| 91 |
elif not user_query.strip():
|
| 92 |
st.error("❌ Please enter your query!")
|
| 93 |
else:
|
| 94 |
+
# Step 1: Process camera image from session state
|
| 95 |
with st.spinner("📸 Processing facial expression..."):
|
| 96 |
try:
|
| 97 |
+
# Read from session state
|
| 98 |
+
file_bytes = np.asarray(bytearray(st.session_state.captured_image_bytes.read()), dtype=np.uint8)
|
| 99 |
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 100 |
|
| 101 |
with tempfile.NamedTemporaryFile(delete=True, suffix=".jpg") as temp_img:
|
|
|
|
| 168 |
with st.sidebar:
|
| 169 |
st.header("ℹ️ How to Use")
|
| 170 |
st.markdown("""
|
| 171 |
+
1. **Take a snapshot** (the image will stay until you clear it)
|
| 172 |
2. **Type your query**
|
| 173 |
3. **Click the mic** to record (click again to stop)
|
| 174 |
4. **Click the 'Analyze' button**
|