Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,12 +6,13 @@ 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 |
client = 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"]
|
|
@@ -26,27 +27,36 @@ 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 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# Progress bar
|
| 52 |
completed_tasks = sum(
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
import os
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
+
import time
|
| 10 |
|
| 11 |
# Load OpenAI API key
|
| 12 |
load_dotenv()
|
| 13 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 14 |
|
| 15 |
+
# Initialize session state for tasks
|
| 16 |
if 'tasks' not in st.session_state:
|
| 17 |
st.session_state.tasks = {
|
| 18 |
day: [] for day in ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
|
|
|
|
| 27 |
current_day = datetime.today().strftime('%A')
|
| 28 |
voice_note = mic_recorder(start_prompt="🎤 Add Task", stop_prompt="⏹️ Stop", key='recorder')
|
| 29 |
|
| 30 |
+
# Process voice input with retry logic
|
| 31 |
if voice_note and 'bytes' in voice_note:
|
| 32 |
+
max_retries = 3 # Number of retries
|
| 33 |
+
retry_delay = 2 # Delay between retries (in seconds)
|
| 34 |
+
|
| 35 |
+
for attempt in range(max_retries):
|
| 36 |
+
try:
|
| 37 |
+
# Save the recorded audio to a temporary file
|
| 38 |
+
audio_file = open("temp_audio.wav", "wb")
|
| 39 |
+
audio_file.write(voice_note['bytes'])
|
| 40 |
+
audio_file.close()
|
| 41 |
+
|
| 42 |
+
# Transcribe using OpenAI Whisper
|
| 43 |
+
with open("temp_audio.wav", "rb") as audio_file:
|
| 44 |
+
transcript = client.audio.transcriptions.create(
|
| 45 |
+
model="whisper-1",
|
| 46 |
+
file=audio_file
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Add transcribed task to current day
|
| 50 |
+
if transcript.text:
|
| 51 |
+
st.session_state.tasks[current_day].append({'text': transcript.text, 'checked': False})
|
| 52 |
+
st.sidebar.success(f"Added: {transcript.text}")
|
| 53 |
+
break # Exit the retry loop if successful
|
| 54 |
+
except Exception as e:
|
| 55 |
+
if attempt < max_retries - 1: # Don't wait on the last attempt
|
| 56 |
+
st.sidebar.warning(f"Attempt {attempt + 1} failed. Retrying in {retry_delay} seconds...")
|
| 57 |
+
time.sleep(retry_delay)
|
| 58 |
+
else:
|
| 59 |
+
st.sidebar.error(f"Error: {str(e)}")
|
| 60 |
|
| 61 |
# Progress bar
|
| 62 |
completed_tasks = sum(
|