dlflannery commited on
Commit
4caa187
·
verified ·
1 Parent(s): c10d943

Update app.py

Browse files

add audio duration stuff

Files changed (1) hide show
  1. app.py +30 -2
app.py CHANGED
@@ -5,6 +5,7 @@ from openai import OpenAI
5
  from dotenv import load_dotenv
6
  from pathlib import Path
7
  from time import sleep
 
8
 
9
  load_dotenv(override=True)
10
  key = os.getenv('OPENAI_API_KEY')
@@ -31,6 +32,7 @@ def genUsageStats(do_reset=False):
31
  ttotal4o_out = 0
32
  ttotal4mini_in = 0
33
  ttotal4mini_out = 0
 
34
  for user in unames:
35
  tokens4o_in = 0
36
  tokens4o_out = 0
@@ -68,8 +70,29 @@ def genUsageStats(do_reset=False):
68
  sleep(3)
69
  if not accessOk:
70
  return f'File access failed reading stats for user: {user}'
71
- result.append([user, f'{tokens4mini_in}/{tokens4mini_out}', f'{tokens4o_in}/{tokens4o_out}'])
72
- result.append(['totals', f'{ttotal4mini_in}/{ttotal4mini_out}', f'{ttotal4o_in}/{ttotal4o_out}'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  return result
74
 
75
  def clear():
@@ -141,6 +164,11 @@ def transcribe(user, pwd, fpath):
141
  pwd = pwd.lower().strip()
142
  if not (user in unames and pwd in pwdList):
143
  return 'Bad credentials'
 
 
 
 
 
144
  with open(fpath,'rb') as audio_file:
145
  transcript = client.audio.transcriptions.create(
146
  model='whisper-1', file = audio_file ,response_format = 'text' )
 
5
  from dotenv import load_dotenv
6
  from pathlib import Path
7
  from time import sleep
8
+ import audioread
9
 
10
  load_dotenv(override=True)
11
  key = os.getenv('OPENAI_API_KEY')
 
32
  ttotal4o_out = 0
33
  ttotal4mini_in = 0
34
  ttotal4mini_out = 0
35
+ totalAudio = 0
36
  for user in unames:
37
  tokens4o_in = 0
38
  tokens4o_out = 0
 
70
  sleep(3)
71
  if not accessOk:
72
  return f'File access failed reading stats for user: {user}'
73
+ userAudio = 0
74
+ fp = dataDir + user + '_audio.txt'
75
+ if os.path.exists(fp):
76
+ accessOk = False
77
+ for i in range(3):
78
+ try:
79
+ with open(fp) as f:
80
+ dataList = f.readlines()
81
+ if do_reset:
82
+ os.remove(fp)
83
+ else:
84
+ for line in dataList:
85
+ (dud, len) = line.split(':')
86
+ userAudio += int(len)
87
+ totalAudio += int(userAudio)
88
+ accessOk = True
89
+ break
90
+ except:
91
+ sleep(3)
92
+ if not accessOk:
93
+ return f'File access failed reading audio stats for user: {user}'
94
+ result.append([user, f'{tokens4mini_in}/{tokens4mini_out}', f'{tokens4o_in}/{tokens4o_out}', f'audio:{userAudio}'])
95
+ result.append(['totals', f'{ttotal4mini_in}/{ttotal4mini_out}', f'{ttotal4o_in}/{ttotal4o_out}', f'audio:{totalAudio}'])
96
  return result
97
 
98
  def clear():
 
164
  pwd = pwd.lower().strip()
165
  if not (user in unames and pwd in pwdList):
166
  return 'Bad credentials'
167
+ with audioread.audio_open(fpath) as audio:
168
+ duration = int(audio.duration)
169
+ if duration > 0:
170
+ with open(dataDir + user + '_audio.txt','a') as f:
171
+ f.write(f'audio:{str(duration)}\n')
172
  with open(fpath,'rb') as audio_file:
173
  transcript = client.audio.transcriptions.create(
174
  model='whisper-1', file = audio_file ,response_format = 'text' )