Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
0b928a0a
Commit
0b928a0a
authored
Jul 05, 2023
by
StyleZhang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
convert audio file to bytes
parent
66fa5537
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
23 additions
and
20 deletions
+23
-20
completion.py
api/controllers/console/explore/completion.py
+2
-1
audio_service.py
api/services/audio_service.py
+11
-7
index.tsx
web/app/components/app/chat/index.tsx
+1
-1
index.tsx
web/app/components/base/voice-input/index.tsx
+8
-10
share.ts
web/service/share.ts
+1
-1
No files found.
api/controllers/console/explore/completion.py
View file @
0b928a0a
...
@@ -142,7 +142,8 @@ class ChatStopApi(InstalledAppResource):
...
@@ -142,7 +142,8 @@ class ChatStopApi(InstalledAppResource):
return
{
'result'
:
'success'
},
200
return
{
'result'
:
'success'
},
200
class
AudioApi
(
InstalledAppResource
):
class
AudioApi
(
InstalledAppResource
):
def
post
(
self
,
app_model
,
end_user
):
def
post
(
self
,
installed_app
):
app_model
=
installed_app
.
app
if
app_model
.
mode
!=
'chat'
:
if
app_model
.
mode
!=
'chat'
:
raise
NotChatAppError
()
raise
NotChatAppError
()
...
...
api/services/audio_service.py
View file @
0b928a0a
import
openai
import
openai
import
io
from
werkzeug.datastructures
import
FileStorage
from
core.llm.llm_builder
import
LLMBuilder
from
core.llm.llm_builder
import
LLMBuilder
from
core.llm.provider.llm_provider_service
import
LLMProviderService
from
core.llm.provider.llm_provider_service
import
LLMProviderService
from
models.model
import
App
from
models.model
import
App
from
controllers.console.datasets.error
import
FileTooLargeError
,
\
from
controllers.console.datasets.error
import
FileTooLargeError
,
UnsupportedFileTypeError
UnsupportedFileTypeError
FILE_SIZE_LIMIT
=
25
*
1024
*
1024
# 25MB
FILE_SIZE_LIMIT
=
25
*
1024
*
1024
# 25MB
ALLOWED_EXTENSIONS
=
[
'mp3'
,
'mp4'
,
'mpeg'
,
'mpga'
,
'm4a'
,
'wav'
,
'webm'
]
ALLOWED_EXTENSIONS
=
[
'mp3'
,
'mp4'
,
'mpeg'
,
'mpga'
,
'm4a'
,
'wav'
,
'webm'
]
class
AudioService
:
class
AudioService
:
@
classmethod
@
classmethod
def
transcript
(
cls
,
app_model
:
App
,
file
,
**
params
):
def
transcript
(
cls
,
app_model
:
App
,
file
:
FileStorage
,
**
params
):
file_content
=
file
.
read
()
file_content
=
file
.
read
()
file_size
=
len
(
file_content
)
file_size
=
len
(
file_content
)
if
file_size
>
FILE_SIZE_LIMIT
:
if
file_size
>
FILE_SIZE_LIMIT
:
message
=
"({file_size} > {FILE_SIZE_LIMIT})"
message
=
f
"({file_size} > {FILE_SIZE_LIMIT})"
raise
FileTooLargeError
(
message
)
raise
FileTooLargeError
(
message
)
extension
=
file
.
filename
.
split
(
'.'
)[
-
1
]
extension
=
file
.
mimetype
if
extension
not
in
ALLOWED_EXTENSIONS
:
if
extension
not
in
[
f
'audio/{ext}'
for
ext
in
ALLOWED_EXTENSIONS
]
:
raise
UnsupportedFileTypeError
()
raise
UnsupportedFileTypeError
()
provider_name
=
LLMBuilder
.
get_default_provider
(
app_model
.
tenant_id
)
provider_name
=
LLMBuilder
.
get_default_provider
(
app_model
.
tenant_id
)
provider
=
LLMProviderService
(
app_model
.
tenant_id
,
provider_name
)
provider
=
LLMProviderService
(
app_model
.
tenant_id
,
provider_name
)
credentials
=
provider
.
get_credentials
(
provider_name
)
credentials
=
provider
.
get_credentials
(
provider_name
)
buffer
=
io
.
BytesIO
(
file_content
)
buffer
.
name
=
'temp.wav'
transcript
=
openai
.
Audio
.
transcribe
(
transcript
=
openai
.
Audio
.
transcribe
(
model
=
'whisper-1'
,
model
=
'whisper-1'
,
file
=
file
,
file
=
buffer
,
api_key
=
credentials
.
get
(
'openai_api_key'
),
api_key
=
credentials
.
get
(
'openai_api_key'
),
api_base
=
credentials
.
get
(
'openai_api_base'
),
api_base
=
credentials
.
get
(
'openai_api_base'
),
api_type
=
credentials
.
get
(
'openai_api_type'
),
api_type
=
credentials
.
get
(
'openai_api_type'
),
...
...
web/app/components/app/chat/index.tsx
View file @
0b928a0a
...
@@ -578,7 +578,7 @@ const Chat: FC<IChatProps> = ({
...
@@ -578,7 +578,7 @@ const Chat: FC<IChatProps> = ({
{
{
query
query
?
(
?
(
<
div
className=
'flex justify-center items-center w-8 h-8 cursor-pointer'
onClick=
{
()
=>
setQuery
(
''
)
}
>
<
div
className=
'flex justify-center items-center w-8 h-8 cursor-pointer
hover:bg-gray-100 rounded-lg
'
onClick=
{
()
=>
setQuery
(
''
)
}
>
<
XCircle
className=
'w-4 h-4 text-[#98A2B3]'
/>
<
XCircle
className=
'w-4 h-4 text-[#98A2B3]'
/>
</
div
>
</
div
>
)
)
...
...
web/app/components/base/voice-input/index.tsx
View file @
0b928a0a
...
@@ -26,6 +26,7 @@ const VoiceInput = ({
...
@@ -26,6 +26,7 @@ const VoiceInput = ({
const
ctxRef
=
useRef
<
CanvasRenderingContext2D
|
null
>
(
null
)
const
ctxRef
=
useRef
<
CanvasRenderingContext2D
|
null
>
(
null
)
const
drawRecordId
=
useRef
<
number
|
null
>
(
null
)
const
drawRecordId
=
useRef
<
number
|
null
>
(
null
)
const
[
duration
,
setDuration
]
=
useState
(
'00:00'
)
const
[
duration
,
setDuration
]
=
useState
(
'00:00'
)
const
[
originDuration
,
setOriginDuration
]
=
useState
(
0
)
const
[
startRecord
,
setStartRecord
]
=
useState
(
false
)
const
[
startRecord
,
setStartRecord
]
=
useState
(
false
)
const
[
startConvert
,
setStartConvert
]
=
useState
(
false
)
const
[
startConvert
,
setStartConvert
]
=
useState
(
false
)
const
drawRecord
=
useCallback
(()
=>
{
const
drawRecord
=
useCallback
(()
=>
{
...
@@ -66,17 +67,15 @@ const VoiceInput = ({
...
@@ -66,17 +67,15 @@ const VoiceInput = ({
const
wavBlob
=
recorder
.
current
.
getWAVBlob
()
const
wavBlob
=
recorder
.
current
.
getWAVBlob
()
const
wavFile
=
new
File
([
wavBlob
],
'a.wav'
,
{
type
:
'audio/wav'
})
const
wavFile
=
new
File
([
wavBlob
],
'a.wav'
,
{
type
:
'audio/wav'
})
const
formData
=
new
FormData
()
const
formData
=
new
FormData
()
formData
.
append
(
'file'
,
wav
Blob
)
formData
.
append
(
'file'
,
wav
File
)
try
{
try
{
const
audioResponse
=
await
audioToText
(
isInstalledApp
,
installedAppId
,
formData
)
const
audioResponse
=
await
audioToText
(
isInstalledApp
,
installedAppId
,
formData
)
const
audioData
=
await
audioResponse
.
json
(
)
onConverted
(
audioResponse
.
text
)
onC
onverted
(
audioData
.
text
)
onC
ancel
(
)
}
}
catch
(
e
)
{
catch
(
e
)
{
onConverted
(
''
)
onConverted
(
''
)
}
finally
{
onCancel
()
onCancel
()
}
}
},
[])
},
[])
...
@@ -86,10 +85,9 @@ const VoiceInput = ({
...
@@ -86,10 +85,9 @@ const VoiceInput = ({
recorder
.
current
.
start
()
recorder
.
current
.
start
()
recorder
.
current
.
onprogress
=
(
params
)
=>
{
recorder
.
current
.
onprogress
=
(
params
)
=>
{
const
originDuration
=
params
.
duration
const
originDuration
=
params
.
duration
if
(
originDuration
>
65
)
{
setOriginDuration
(
originDuration
)
console
.
log
(
'stop'
)
if
(
originDuration
>=
120
)
handleStopRecorder
()
handleStopRecorder
()
}
const
minutes
=
parseInt
(
`
${
parseInt
(
`
${
originDuration
}
`
)
/
60
}
`
)
const
minutes
=
parseInt
(
`
${
parseInt
(
`
${
originDuration
}
`
)
/
60
}
`
)
const
seconds
=
parseInt
(
`
${
originDuration
}
`
)
%
60
const
seconds
=
parseInt
(
`
${
originDuration
}
`
)
%
60
setDuration
(
`0
${
minutes
.
toFixed
(
0
)}
:
${
seconds
>=
10
?
seconds
:
`0
${
seconds
}
`
}
`
)
setDuration
(
`0
${
minutes
.
toFixed
(
0
)}
:
${
seconds
>=
10
?
seconds
:
`0
${
seconds
}
`
}
`
)
...
@@ -158,14 +156,14 @@ const VoiceInput = ({
...
@@ -158,14 +156,14 @@ const VoiceInput = ({
{
{
startConvert
&&
(
startConvert
&&
(
<
div
<
div
className=
'flex justify-center items-center mr-1 w-8 h-8 hover:bg-
primary-1
00 rounded-lg cursor-pointer'
className=
'flex justify-center items-center mr-1 w-8 h-8 hover:bg-
gray-2
00 rounded-lg cursor-pointer'
onClick=
{
onCancel
}
onClick=
{
onCancel
}
>
>
<
XClose
className=
'w-4 h-4 text-gray-500'
/>
<
XClose
className=
'w-4 h-4 text-gray-500'
/>
</
div
>
</
div
>
)
)
}
}
<
div
className=
'w-[45px] pl-1 text-xs font-medium text-gray-700'
>
{
duration
}
</
div
>
<
div
className=
{
`w-[45px] pl-1 text-xs font-medium ${originDuration > 110 ? 'text-[#F04438]' : 'text-gray-700'}`
}
>
{
duration
}
</
div
>
</
div
>
</
div
>
</
div
>
</
div
>
)
)
...
...
web/service/share.ts
View file @
0b928a0a
...
@@ -116,5 +116,5 @@ export const fetchSuggestedQuestions = (messageId: string, isInstalledApp: boole
...
@@ -116,5 +116,5 @@ export const fetchSuggestedQuestions = (messageId: string, isInstalledApp: boole
}
}
export
const
audioToText
=
(
isInstalledApp
:
boolean
,
installedAppId
:
string
,
body
:
FormData
)
=>
{
export
const
audioToText
=
(
isInstalledApp
:
boolean
,
installedAppId
:
string
,
body
:
FormData
)
=>
{
return
(
getAction
(
'post'
,
isInstalledApp
))(
getUrl
(
'/audio-to-text'
,
isInstalledApp
,
installedAppId
),
{
body
},
{
bodyStringify
:
false
,
deleteContentType
:
true
})
return
(
getAction
(
'post'
,
isInstalledApp
))(
getUrl
(
'/audio-to-text'
,
isInstalledApp
,
installedAppId
),
{
body
},
{
bodyStringify
:
false
,
deleteContentType
:
true
})
as
Promise
<
{
text
:
string
}
>
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment