FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
aider/aider/voice.py at main · tapas4java/aider · GitHub
tapas4java
aider
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
aider
/
aider
/
voice.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
101 lines (73 loc) · 2.59 KB
Breadcrumbs
aider
/
aider
/
voice.py
Copy path
File metadata and controls
101 lines (73 loc) · 2.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import
os
import
queue
import
tempfile
import
time
import
numpy
as
np
import
openai
try
:
import
soundfile
as
sf
except
(
OSError
,
ModuleNotFoundError
):
sf
=
None
from
prompt_toolkit
.
shortcuts
import
prompt
from
.
dump
import
dump
# noqa: F401
class
SoundDeviceError
(
Exception
):
pass
class
Voice
:
max_rms
=
0
min_rms
=
1e5
pct
=
0
threshold
=
0.15
def
__init__
(
self
):
if
sf
is
None
:
raise
SoundDeviceError
try
:
print
(
"Initializing sound device..."
)
import
sounddevice
as
sd
self
.
sd
=
sd
except
(
OSError
,
ModuleNotFoundError
):
raise
SoundDeviceError
def
callback
(
self
,
indata
,
frames
,
time
,
status
):
"""This is called (from a separate thread) for each audio block."""
rms
=
np
.
sqrt
(
np
.
mean
(
indata
**
2
))
self
.
max_rms
=
max
(
self
.
max_rms
,
rms
)
self
.
min_rms
=
min
(
self
.
min_rms
,
rms
)
rng
=
self
.
max_rms
-
self
.
min_rms
if
rng
>
0.001
:
self
.
pct
=
(
rms
-
self
.
min_rms
)
/
rng
else
:
self
.
pct
=
0.5
self
.
q
.
put
(
indata
.
copy
())
def
get_prompt
(
self
):
num
=
10
if
np
.
isnan
(
self
.
pct
)
or
self
.
pct
<
self
.
threshold
:
cnt
=
0
else
:
cnt
=
int
(
self
.
pct
*
10
)
bar
=
"░"
*
cnt
+
"█"
*
(
num
-
cnt
)
bar
=
bar
[:
num
]
dur
=
time
.
time
()
-
self
.
start_time
return
f"Recording, press ENTER when done...
{
dur
:.1f
}
sec
{
bar
}
"
def
record_and_transcribe
(
self
,
history
=
None
,
language
=
None
):
try
:
return
self
.
raw_record_and_transcribe
(
history
,
language
)
except
KeyboardInterrupt
:
return
def
raw_record_and_transcribe
(
self
,
history
,
language
):
self
.
q
=
queue
.
Queue
()
filename
=
tempfile
.
mktemp
(
suffix
=
".wav"
)
sample_rate
=
16000
# 16kHz
self
.
start_time
=
time
.
time
()
with
self
.
sd
.
InputStream
(
samplerate
=
sample_rate
,
channels
=
1
,
callback
=
self
.
callback
):
prompt
(
self
.
get_prompt
,
refresh_interval
=
0.1
)
with
sf
.
SoundFile
(
filename
,
mode
=
"x"
,
samplerate
=
sample_rate
,
channels
=
1
)
as
file
:
while
not
self
.
q
.
empty
():
file
.
write
(
self
.
q
.
get
())
with
open
(
filename
,
"rb"
)
as
fh
:
transcript
=
openai
.
Audio
.
transcribe
(
"whisper-1"
,
fh
,
prompt
=
history
,
language
=
language
)
text
=
transcript
[
"text"
]
return
text
if
__name__
==
"__main__"
:
api_key
=
os
.
getenv
(
"OPENAI_API_KEY"
)
if
not
api_key
:
raise
ValueError
(
"Please set the OPENAI_API_KEY environment variable."
)
print
(
Voice
().
record_and_transcribe
())
Back
|
FazBrowse Home
|
New Git URL