Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,89 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from streamlit_mic_recorder import mic_recorder
|
|
|
|
| 3 |
import json
|
| 4 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
if 'tasks' not in st.session_state:
|
| 8 |
st.session_state.tasks = {
|
| 9 |
day: [] for day in ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
|
| 10 |
}
|
| 11 |
|
| 12 |
-
#
|
| 13 |
st.title("π Student Study Tracker")
|
| 14 |
days = list(st.session_state.tasks.keys())
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Sidebar for voice input (placeholder)
|
| 21 |
-
st.sidebar.header("π€ Add Tasks with Voice")
|
| 22 |
-
voice_note = mic_recorder(start_prompt="π€ Record Task", stop_prompt="βΉοΈ Stop", key='voice_recorder')
|
| 23 |
|
|
|
|
| 24 |
if voice_note and 'bytes' in voice_note:
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
tabs = st.tabs(days)
|
| 29 |
|
| 30 |
for i, day in enumerate(days):
|
| 31 |
with tabs[i]:
|
| 32 |
-
# Display
|
| 33 |
for task_idx, task in enumerate(st.session_state.tasks[day]):
|
|
|
|
|
|
|
|
|
|
| 34 |
col1, col2 = st.columns([0.9, 0.1])
|
| 35 |
with col1:
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
with col2:
|
| 44 |
-
# Checkbox
|
| 45 |
is_checked = st.checkbox(
|
| 46 |
"Done",
|
| 47 |
-
value=
|
| 48 |
-
key=f"
|
| 49 |
)
|
| 50 |
st.session_state.tasks[day][task_idx]['checked'] = is_checked
|
| 51 |
|
|
@@ -53,8 +91,8 @@ for i, day in enumerate(days):
|
|
| 53 |
if st.button(f"β Add Task for {day}"):
|
| 54 |
st.session_state.tasks[day].append({'text': '', 'checked': False})
|
| 55 |
|
| 56 |
-
# Save
|
| 57 |
if st.button("πΎ Save All Tasks"):
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
st.success("
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from streamlit_mic_recorder import mic_recorder
|
| 3 |
+
import openai
|
| 4 |
import json
|
| 5 |
from pathlib import Path
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
import os
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
|
| 10 |
+
# Load OpenAI API key
|
| 11 |
+
load_dotenv()
|
| 12 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 13 |
+
|
| 14 |
+
# Initialize session state
|
| 15 |
if 'tasks' not in st.session_state:
|
| 16 |
st.session_state.tasks = {
|
| 17 |
day: [] for day in ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
|
| 18 |
}
|
| 19 |
|
| 20 |
+
# App title
|
| 21 |
st.title("π Student Study Tracker")
|
| 22 |
days = list(st.session_state.tasks.keys())
|
| 23 |
|
| 24 |
+
# Voice input sidebar
|
| 25 |
+
st.sidebar.header("π€ Voice Input")
|
| 26 |
+
current_day = datetime.today().strftime('%A')
|
| 27 |
+
voice_note = mic_recorder(start_prompt="π€ Add Task", stop_prompt="βΉοΈ Stop", key='recorder')
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Process voice input
|
| 30 |
if voice_note and 'bytes' in voice_note:
|
| 31 |
+
try:
|
| 32 |
+
audio_file = open("temp_audio.wav", "wb")
|
| 33 |
+
audio_file.write(voice_note['bytes'])
|
| 34 |
+
audio_file.close()
|
| 35 |
+
|
| 36 |
+
# Transcribe using OpenAI Whisper
|
| 37 |
+
audio_file = open("temp_audio.wav", "rb")
|
| 38 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
| 39 |
+
|
| 40 |
+
# Add transcribed task to current day
|
| 41 |
+
if transcript['text']:
|
| 42 |
+
st.session_state.tasks[current_day].append({'text': transcript['text'], 'checked': False})
|
| 43 |
+
st.sidebar.success(f"Added: {transcript['text']}")
|
| 44 |
+
except Exception as e:
|
| 45 |
+
st.sidebar.error(f"Error: {str(e)}")
|
| 46 |
|
| 47 |
+
# Progress bar
|
| 48 |
+
completed_tasks = sum(
|
| 49 |
+
sum(1 for task in st.session_state.tasks[day] if task['checked'])
|
| 50 |
+
for day in days
|
| 51 |
+
)
|
| 52 |
+
st.progress(completed_tasks / max(1, sum(len(tasks) for tasks in st.session_state.tasks.values())))
|
| 53 |
+
|
| 54 |
+
# Create day tabs
|
| 55 |
tabs = st.tabs(days)
|
| 56 |
|
| 57 |
for i, day in enumerate(days):
|
| 58 |
with tabs[i]:
|
| 59 |
+
# Display tasks with strikethrough
|
| 60 |
for task_idx, task in enumerate(st.session_state.tasks[day]):
|
| 61 |
+
checked = task['checked']
|
| 62 |
+
text = task['text']
|
| 63 |
+
|
| 64 |
col1, col2 = st.columns([0.9, 0.1])
|
| 65 |
with col1:
|
| 66 |
+
# Strikethrough styling for completed tasks
|
| 67 |
+
if checked:
|
| 68 |
+
st.markdown(
|
| 69 |
+
f"<div style='color: #888; text-decoration: line-through; margin: 5px 0;'>"
|
| 70 |
+
f"{text}"
|
| 71 |
+
f"</div>",
|
| 72 |
+
unsafe_allow_html=True
|
| 73 |
+
)
|
| 74 |
+
else:
|
| 75 |
+
new_text = st.text_input(
|
| 76 |
+
f"Task {task_idx + 1}",
|
| 77 |
+
value=text,
|
| 78 |
+
key=f"task_{day}_{task_idx}"
|
| 79 |
+
)
|
| 80 |
+
st.session_state.tasks[day][task_idx]['text'] = new_text
|
| 81 |
with col2:
|
| 82 |
+
# Checkbox with updated state
|
| 83 |
is_checked = st.checkbox(
|
| 84 |
"Done",
|
| 85 |
+
value=checked,
|
| 86 |
+
key=f"check_{day}_{task_idx}"
|
| 87 |
)
|
| 88 |
st.session_state.tasks[day][task_idx]['checked'] = is_checked
|
| 89 |
|
|
|
|
| 91 |
if st.button(f"β Add Task for {day}"):
|
| 92 |
st.session_state.tasks[day].append({'text': '', 'checked': False})
|
| 93 |
|
| 94 |
+
# Save all tasks
|
| 95 |
if st.button("πΎ Save All Tasks"):
|
| 96 |
+
with open("tasks.json", "w") as f:
|
| 97 |
+
json.dump(st.session_state.tasks, f)
|
| 98 |
+
st.success("All tasks saved!")
|