MonsterBoyTabs commited on
Commit
33f4398
·
verified ·
1 Parent(s): 788015e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -7
app.py CHANGED
@@ -1,6 +1,6 @@
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
@@ -9,7 +9,7 @@ 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:
@@ -29,18 +29,22 @@ voice_note = mic_recorder(start_prompt="🎤 Add Task", stop_prompt="⏹️ Stop
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
 
 
1
  import streamlit as st
2
  from streamlit_mic_recorder import mic_recorder
3
+ from openai import OpenAI
4
  import json
5
  from pathlib import Path
6
  from datetime import datetime
 
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:
 
29
  # Process voice input
30
  if voice_note and 'bytes' in voice_note:
31
  try:
32
+ # Save the recorded audio to a temporary file
33
  audio_file = open("temp_audio.wav", "wb")
34
  audio_file.write(voice_note['bytes'])
35
  audio_file.close()
36
 
37
  # Transcribe using OpenAI Whisper
38
+ with open("temp_audio.wav", "rb") as audio_file:
39
+ transcript = client.audio.transcriptions.create(
40
+ model="whisper-1",
41
+ file=audio_file
42
+ )
43
 
44
  # Add transcribed task to current day
45
+ if transcript.text:
46
+ st.session_state.tasks[current_day].append({'text': transcript.text, 'checked': False})
47
+ st.sidebar.success(f"Added: {transcript.text}")
48
  except Exception as e:
49
  st.sidebar.error(f"Error: {str(e)}")
50