dlflannery commited on
Commit
f6b67f5
·
verified ·
1 Parent(s): 03891dd

Update app.py

Browse files

test: simple add 2 nums

Files changed (1) hide show
  1. app.py +8 -419
app.py CHANGED
@@ -1,424 +1,13 @@
1
  import os
2
  import gradio as gr
3
- # import openai
4
- from numpy._core.defchararray import endswith, isdecimal
5
- from openai import OpenAI
6
- from dotenv import load_dotenv
7
- from pathlib import Path
8
- from time import sleep
9
- import audioread
10
- import queue
11
- import threading
12
- from glob import glob
13
- import copy
14
-
15
- load_dotenv(override=True)
16
- key = os.getenv('OPENAI_API_KEY')
17
- users = os.getenv('LOGNAME')
18
- unames = users.split(',')
19
- pwds = os.getenv('PASSWORD')
20
- pwdList = pwds.split(',')
21
-
22
- site = os.getenv('SITE')
23
- if site == 'local':
24
- dp = Path('./data')
25
- dp.mkdir(exist_ok=True)
26
- dataDir = './data/'
27
- else:
28
- dp = Path('/data')
29
- dp.mkdir(exist_ok=True)
30
- dataDir = '/data/'
31
-
32
- speak_file = dataDir + "speek.wav"
33
-
34
- client = OpenAI(api_key = key)
35
-
36
- #digits = ['zero: ','one: ','two: ','three: ','four: ','five: ','six: ','seven: ','eight: ','nine: ']
37
-
38
- abbrevs = {'St. ' : 'Saint ', 'Mr. ': 'mister ', 'Mrs. ':'mussus ', 'Mr. ':'mister ', 'Ms. ':'mizz '}
39
-
40
- def genUsageStats(do_reset=False):
41
- result = []
42
- ttotal4o_in = 0
43
- ttotal4o_out = 0
44
- ttotal4mini_in = 0
45
- ttotal4mini_out = 0
46
- totalAudio = 0
47
- totalSpeech = 0
48
- for user in unames:
49
- tokens4o_in = 0
50
- tokens4o_out = 0
51
- tokens4mini_in = 0
52
- tokens4mini_out = 0
53
- fp = dataDir + user + '_log.txt'
54
- if os.path.exists(fp):
55
- accessOk = False
56
- for i in range(3):
57
- try:
58
- with open(fp) as f:
59
- dataList = f.readlines()
60
- if do_reset:
61
- os.remove(fp)
62
- else:
63
- for line in dataList:
64
- (u, t) = line.split(':')
65
- (t, m) = t.split('-')
66
- (tin, tout) = t.split('/')
67
- incount = int(tin)
68
- outcount = int(tout)
69
- if 'mini' in m:
70
- tokens4mini_in += incount
71
- tokens4mini_out += outcount
72
- ttotal4mini_in += incount
73
- ttotal4mini_out += outcount
74
- else:
75
- tokens4o_in += incount
76
- tokens4o_out += outcount
77
- ttotal4o_in += incount
78
- ttotal4o_out += outcount
79
- accessOk = True
80
- break
81
- except:
82
- sleep(3)
83
- if not accessOk:
84
- return f'File access failed reading stats for user: {user}'
85
- userAudio = 0
86
- fp = dataDir + user + '_audio.txt'
87
- if os.path.exists(fp):
88
- accessOk = False
89
- for i in range(3):
90
- try:
91
- with open(fp) as f:
92
- dataList = f.readlines()
93
- if do_reset:
94
- os.remove(fp)
95
- else:
96
- for line in dataList:
97
- (dud, len) = line.split(':')
98
- userAudio += int(len)
99
- totalAudio += int(userAudio)
100
- accessOk = True
101
- break
102
- except:
103
- sleep(3)
104
- if not accessOk:
105
- return f'File access failed reading audio stats for user: {user}'
106
- userSpeech = 0
107
- fp = dataDir + user + '_speech.txt'
108
- if os.path.exists(fp):
109
- accessOk = False
110
- for i in range(3):
111
- try:
112
- with open(fp) as f:
113
- dataList = f.readlines()
114
- if do_reset:
115
- os.remove(fp)
116
- else:
117
- for line in dataList:
118
- (dud, len) = line.split(':')
119
- userSpeech += int(len)
120
- totalSpeech += int(userSpeech)
121
- accessOk = True
122
- break
123
- except:
124
- sleep(3)
125
- if not accessOk:
126
- return f'File access failed reading speech stats for user: {user}'
127
- result.append([user, f'{tokens4mini_in}/{tokens4mini_out}', f'{tokens4o_in}/{tokens4o_out}', f'audio:{userAudio}',f'speech:{userSpeech}'])
128
- result.append(['totals', f'{ttotal4mini_in}/{ttotal4mini_out}', f'{ttotal4o_in}/{ttotal4o_out}', f'audio:{totalAudio}',f'speech:{totalSpeech}'])
129
- return result
130
-
131
- def new_conversation(user):
132
- clean_up(user)
133
- return [None, [], None, []]
134
-
135
- def updatePassword(txt):
136
- return [gr.State(value=txt.lower().strip()), "*********"]
137
-
138
- # def setModel(val):
139
- # return val
140
-
141
- def chat(prompt, user_window, pwd_window, past, response, gptModel):
142
- user_window = user_window.lower().strip()
143
- isBoss = False
144
- if user_window == unames[0] and pwd_window == pwdList[0]:
145
- isBoss = True
146
- if prompt == 'stats':
147
- response = genUsageStats()
148
- list_permanent_files()
149
- return [past, response, None, gptModel]
150
- if prompt == 'reset':
151
- response = genUsageStats(True)
152
- return [past, response, None, gptModel]
153
- if prompt.startswith('gpt4'):
154
- gptModel = 'gpt-4o'
155
- prompt = prompt[5:]
156
- if prompt.startswith("clean"):
157
- user = prompt[6:]
158
- response = f'cleaned all .wav files for {user}'
159
- final_clean_up(user)
160
- return [past, response, None, gptModel]
161
- if prompt.startswith('files'):
162
- (log_cnt, wav_cnt, other_cnt, others) = list_permanent_files()
163
- response = f'{log_cnt} log files\n{wav_cnt} .wav files\n{other_cnt} Other files:\n{others}'
164
- return [past, response, None, gptModel]
165
- if user_window in unames and pwd_window == pwdList[unames.index(user_window)]:
166
- past.append({"role":"user", "content":prompt})
167
- completion = client.chat.completions.create(model=gptModel,
168
- messages=past)
169
- reply = completion.choices[0].message.content
170
- tokens_in = completion.usage.prompt_tokens
171
- tokens_out = completion.usage.completion_tokens
172
- tokens = completion.usage.total_tokens
173
- response += "\n\nYOU: " + prompt + "\nGPT: " + reply
174
- if isBoss:
175
- response += f"\n{gptModel}: tokens in/out = {tokens_in}/{tokens_out}"
176
- if tokens > 40000:
177
- response += "\n\nTHIS DIALOG IS GETTING TOO LONG. PLEASE RESTART CONVERSATION SOON."
178
- past.append({"role":"assistant", "content": reply})
179
- accessOk = False
180
- for i in range(3):
181
- try:
182
- dataFile = new_func(user_window)
183
- with open(dataFile, 'a') as f:
184
- m = '4o'
185
- if 'mini' in gptModel:
186
- m = '4omini'
187
- f.write(f'{user_window}:{tokens_in}/{tokens_out}-{m}\n')
188
- accessOk = True
189
- break
190
- except Exception as e:
191
- sleep(3)
192
- if not accessOk:
193
- response += f"\nDATA LOG FAILED, path = {dataFile}"
194
- return [past, response , None, gptModel]
195
- else:
196
- return [[], "User name and/or password are incorrect", prompt, gptModel]
197
-
198
- def new_func(user):
199
- dataFile = dataDir + user + '_log.txt'
200
- return dataFile
201
-
202
- def transcribe(user, pwd, fpath):
203
- user = user.lower().strip()
204
- pwd = pwd.lower().strip()
205
- if not (user in unames and pwd in pwdList):
206
- return 'Bad credentials'
207
- with audioread.audio_open(fpath) as audio:
208
- duration = int(audio.duration)
209
- if duration > 0:
210
- with open(dataDir + user + '_audio.txt','a') as f:
211
- f.write(f'audio:{str(duration)}\n')
212
- with open(fpath,'rb') as audio_file:
213
- transcript = client.audio.transcriptions.create(
214
- model='whisper-1', file = audio_file ,response_format = 'text' )
215
- reply = transcript
216
- return str(reply)
217
-
218
- def pause_message():
219
- return "Audio input is paused. Resume or Stop as desired"
220
-
221
- # def gen_output_audio(txt):
222
- # if len(txt) < 10:
223
- # txt = "This dialog is too short to mess with!"
224
- # response = client.audio.speech.create(model="tts-1", voice="fable", input=txt)
225
- # with open(speak_file, 'wb') as fp:
226
- # fp.write(response.content)
227
- # return speak_file
228
-
229
-
230
- def set_speak_button(txt):
231
- vis = False
232
- if len(txt) > 2:
233
- vis = True
234
- return gr.Button(visible=vis)
235
-
236
- def update_user(user_win):
237
- user_win = user_win.lower().strip()
238
- user = 'unknown'
239
- for s in unames:
240
- if user_win == s:
241
- user = s
242
- break
243
- return gr.Textbox(value=user)
244
-
245
- def speech_worker(chunks=[],q=[]):
246
- for chunk in chunks:
247
- fpath = q.pop(0)
248
- response = client.audio.speech.create(model="tts-1", voice="fable", input=chunk, speed=0.85, response_format='wav')
249
- with open(fpath, 'wb') as fp:
250
- fp.write(response.content)
251
-
252
- def gen_speech_file_names(user, cnt):
253
- rv = []
254
- for i in range(0, cnt):
255
- rv.append(dataDir + f'{user}_speech{i}.wav')
256
- return rv
257
-
258
- def final_clean_up(user):
259
- if user.strip().lower() == 'all':
260
- flist = glob(dataDir + '*_speech*.wav')
261
- else:
262
- flist = glob(dataDir + f'{user}_speech*.wav')
263
- for fpath in flist:
264
- try:
265
- os.remove(fpath)
266
- except:
267
- continue
268
-
269
-
270
- def list_permanent_files():
271
- flist = os.listdir(dataDir)
272
- others = []
273
- log_cnt = 0
274
- wav_cnt = 0
275
- other_cnt = 0
276
- for fpath in flist:
277
- if fpath.endswith('.txt'):
278
- log_cnt += 1
279
- elif fpath.endswith('.wav'):
280
- wav_cnt += 1
281
- else:
282
- others.append(fpath)
283
- other_cnt = len(others)
284
- return (str(log_cnt), str(wav_cnt), str(other_cnt), str(others))
285
 
 
 
286
 
287
  with gr.Blocks() as demo:
288
- history = gr.State([])
289
- # password = gr.State("")
290
- # user = gr.State("unknown")
291
- model = gr.State("gpt-4o-mini")
292
- q = gr.State([])
293
- qsave = gr.State([])
294
-
295
- def clean_up(user):
296
- flist = glob(dataDir + f'{user}_speech*.wav')
297
- for fpath in flist:
298
- try:
299
- os.remove(fpath)
300
- except:
301
- continue
302
-
303
- def initial_audio_output(txt, user):
304
- global digits
305
- global abbrevs
306
- if not user in unames:
307
- return [gr.Audio(sources=None), []]
308
- clean_up(user)
309
- q = []
310
- if len(txt.strip()) < 5:
311
- return ['None', q]
312
- for s,x in abbrevs.items():
313
- txt = txt.replace(s, x)
314
- words_in = txt.replace('**', '').splitlines(False)
315
- words_out = []
316
- for s in words_in:
317
- s = s.lstrip('- *@#$%^&_=+-')
318
- if len(s) > 0:
319
- loc = s.index(' ')
320
- if loc > 1:
321
- val = s[0:loc]
322
- isnum = val.replace('.','0').isdecimal()
323
- if isnum:
324
- if val.endswith('.'):
325
- val = val[:-1].replace('.',' point ') + '., '
326
- else:
327
- val = val.replace('.', ' point ') + ', '
328
- s = 'num'+ val + s[loc:]
329
- words_out.append(s)
330
- chunklist = []
331
- for chunk in words_out:
332
- if chunk.strip() == '':
333
- continue
334
- isnumbered = chunk.startswith('num')
335
- number = ''
336
- loc = 0
337
- if isnumbered:
338
- chunk = chunk[3:]
339
- loc = chunk.index(',')
340
- number = chunk[0:loc]
341
- chunk = chunk[loc:]
342
- locs = []
343
- for i in range(1,len(chunk)-1):
344
- (a, b, c) = chunk[i-1:i+2]
345
- if a.isdecimal() and b == '.' and c.isdecimal():
346
- locs.append(i)
347
- for i in locs:
348
- chunk = chunk[:i] + ' point ' + chunk[i+1:]
349
- if len(chunk) > 50:
350
- finechunks = chunk.split('.')
351
- for fchunk in finechunks:
352
- if isnumbered:
353
- fchunk = number + fchunk
354
- isnumbered = False
355
- if len(fchunk) > 0:
356
- if fchunk != '"':
357
- chunklist.append(fchunk)
358
- else:
359
- line = number + chunk
360
- if line != '"':
361
- chunklist.append(line)
362
- total_speech = 0
363
- for chunk in chunklist:
364
- total_speech += len(chunk)
365
- with open(dataDir + user + '_speech.txt','a') as f:
366
- f.write(f'speech:{str(total_speech)}\n')
367
- chunk = chunklist[0]
368
- if chunk.strip() == '':
369
- return gr.Audio(sources=None)
370
- fname_list = gen_speech_file_names(user, len(chunklist))
371
- q = fname_list.copy()
372
- qsave = fname_list.copy()
373
- fname = q.pop(0)
374
- if len(chunklist) > 0:
375
- threading.Thread(target=speech_worker, daemon=True, args=(chunklist[1:],fname_list[1:])).start()
376
- response = client.audio.speech.create(model="tts-1", voice="fable", input=chunk, speed=0.85, response_format='wav')
377
- with open(fname, 'wb') as fp:
378
- fp.write(response.content)
379
- return [fname, q]
380
-
381
- def gen_output_audio(q, user):
382
- try:
383
- fname = q.pop(0)
384
- except:
385
- final_clean_up(user)
386
- return [None, gr.Audio(sources=None)]
387
- return [fname, q]
388
-
389
-
390
- gr.Markdown('# GPT Chat')
391
- gr.Markdown('Enter user name & password then enter prompt and click submit button. Restart conversation if topic changes. ' +
392
- 'You can enter prompts by voice. Tap "Record", speak, then tap "Stop". ' +
393
- 'Tap "Reset Voice Entry" to enter more voice. Tap "Speak Dialog" to hear dialog. ' +
394
- 'Note: first voice response may take a longer time.')
395
- with gr.Row():
396
- user_window = gr.Textbox(label = "User Name")
397
- # user_window.blur(fn=update_user, inputs=user_window, outputs=user_window)
398
- pwd_window = gr.Textbox(label = "Password")
399
- # pwd_window.blur(updatePassword, pwd_window, [password, pwd_window])
400
- with gr.Row():
401
- audio_widget = gr.Audio(type='filepath', format='wav',waveform_options=gr.WaveformOptions(
402
- show_recording_waveform=True), sources=['microphone'], scale = 3, label="Prompt/Question Voice Entry", max_length=120)
403
- reset_button = gr.ClearButton(value="Reset Voice Entry", scale=1) #new_func1()
404
- with gr.Row():
405
- clear_button = gr.Button(value="Restart Conversation")
406
- # gpt_chooser=gr.Radio(choices=[("GPT-3.5","gpt-3.5-turbo"),("GPT-4o","gpt-4o-mini")],
407
- # value="gpt-3.5-turbo", label="GPT Model", interactive=True)
408
- submit_button = gr.Button(value="Submit Prompt/Question")
409
- speak_output = gr.Button(value="Speak Dialog", visible=False)
410
- prompt_window = gr.Textbox(label = "Prompt or Question")
411
- output_window = gr.Textbox(label = "Dialog")
412
- submit_button.click(chat, inputs=[prompt_window, user_window, pwd_window, history, output_window, model],
413
- outputs=[history, output_window, prompt_window, model])
414
- clear_button.click(fn=new_conversation, inputs=user_window, outputs=[prompt_window, history, output_window])
415
- audio_widget.stop_recording(fn=transcribe, inputs=[user_window, pwd_window, audio_widget],
416
- outputs=[prompt_window])
417
- audio_widget.pause_recording(fn=pause_message, outputs=[prompt_window])
418
- reset_button.add(audio_widget)
419
- audio_out = gr.Audio(autoplay=True, visible=False)
420
- audio_out.stop(fn=gen_output_audio, inputs=[q, user_window], outputs = [audio_out, q])
421
- speak_output.click(fn=initial_audio_output, inputs=[output_window, user_window], outputs=[audio_out, q])
422
- output_window.change(fn=set_speak_button, inputs=output_window,outputs=speak_output)
423
- # demo.unload(final_clean_up(user))
424
- demo.launch(share=True)
 
1
  import os
2
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ def sum(a, b):
5
+ return str(float(a) + float(b))
6
 
7
  with gr.Blocks() as demo:
8
+ input_a = gr.Textbox(label = 'Value 1', value = '0')
9
+ input_b = gr.Textbox(label = 'Value 2', value = '0')
10
+ result_box = gr.Textbox(label = 'Result', value = '0')
11
+ button_go = gr.Button(value = 'GO')
12
+ button_go.click(fn=sum, inputs = [input_a, input_b], outputs = result_box)
13
+ demo.launch(share = True)