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
0b50c525
Unverified
Commit
0b50c525
authored
Mar 07, 2024
by
Bowen Liang
Committed by
GitHub
Mar 07, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: support error correction and border size in qrcode tool (#2731)
parent
8ba38e8e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
10 deletions
+94
-10
qrcode_generator.py
...e/tools/provider/builtin/qrcode/tools/qrcode_generator.py
+41
-7
qrcode_generator.yaml
...tools/provider/builtin/qrcode/tools/qrcode_generator.yaml
+53
-3
No files found.
api/core/tools/provider/builtin/qrcode/tools/qrcode_generator.py
View file @
0b50c525
...
@@ -2,14 +2,23 @@ import io
...
@@ -2,14 +2,23 @@ import io
import
logging
import
logging
from
typing
import
Any
,
Union
from
typing
import
Any
,
Union
import
qrcode
from
qrcode.constants
import
ERROR_CORRECT_H
,
ERROR_CORRECT_L
,
ERROR_CORRECT_M
,
ERROR_CORRECT_Q
from
qrcode.image.base
import
BaseImage
from
qrcode.image.pure
import
PyPNGImage
from
qrcode.image.pure
import
PyPNGImage
from
qrcode.main
import
QRCode
from
core.tools.entities.tool_entities
import
ToolInvokeMessage
from
core.tools.entities.tool_entities
import
ToolInvokeMessage
from
core.tools.tool.builtin_tool
import
BuiltinTool
from
core.tools.tool.builtin_tool
import
BuiltinTool
class
QRCodeGeneratorTool
(
BuiltinTool
):
class
QRCodeGeneratorTool
(
BuiltinTool
):
error_correction_levels
=
{
'L'
:
ERROR_CORRECT_L
,
# <=7%
'M'
:
ERROR_CORRECT_M
,
# <=15%
'Q'
:
ERROR_CORRECT_Q
,
# <=25%
'H'
:
ERROR_CORRECT_H
,
# <=30%
}
def
_invoke
(
self
,
def
_invoke
(
self
,
user_id
:
str
,
user_id
:
str
,
tool_parameters
:
dict
[
str
,
Any
],
tool_parameters
:
dict
[
str
,
Any
],
...
@@ -17,19 +26,44 @@ class QRCodeGeneratorTool(BuiltinTool):
...
@@ -17,19 +26,44 @@ class QRCodeGeneratorTool(BuiltinTool):
"""
"""
invoke tools
invoke tools
"""
"""
# get
expression
# get
text content
content
=
tool_parameters
.
get
(
'content'
,
''
)
content
=
tool_parameters
.
get
(
'content'
,
''
)
if
not
content
:
if
not
content
:
return
self
.
create_text_message
(
'Invalid parameter content'
)
return
self
.
create_text_message
(
'Invalid parameter content'
)
# get border size
border
=
tool_parameters
.
get
(
'border'
,
0
)
if
border
<
0
or
border
>
100
:
return
self
.
create_text_message
(
'Invalid parameter border'
)
# get error_correction
error_correction
=
tool_parameters
.
get
(
'error_correction'
,
''
)
if
error_correction
not
in
self
.
error_correction_levels
.
keys
():
return
self
.
create_text_message
(
'Invalid parameter error_correction'
)
try
:
try
:
img
=
qrcode
.
make
(
data
=
content
,
image_factory
=
PyPNGImage
)
image
=
self
.
_generate_qrcode
(
content
,
border
,
error_correction
)
byte_stream
=
io
.
BytesIO
()
image_bytes
=
self
.
_image_to_byte_array
(
image
)
img
.
save
(
byte_stream
)
return
self
.
create_blob_message
(
blob
=
image_bytes
,
byte_array
=
byte_stream
.
getvalue
()
return
self
.
create_blob_message
(
blob
=
byte_array
,
meta
=
{
'mime_type'
:
'image/png'
},
meta
=
{
'mime_type'
:
'image/png'
},
save_as
=
self
.
VARIABLE_KEY
.
IMAGE
.
value
)
save_as
=
self
.
VARIABLE_KEY
.
IMAGE
.
value
)
except
Exception
:
except
Exception
:
logging
.
exception
(
f
'Failed to generate QR code for content: {content}'
)
logging
.
exception
(
f
'Failed to generate QR code for content: {content}'
)
return
self
.
create_text_message
(
'Failed to generate QR code'
)
return
self
.
create_text_message
(
'Failed to generate QR code'
)
def
_generate_qrcode
(
self
,
content
:
str
,
border
:
int
,
error_correction
:
str
)
->
BaseImage
:
qr
=
QRCode
(
image_factory
=
PyPNGImage
,
error_correction
=
self
.
error_correction_levels
.
get
(
error_correction
),
border
=
border
,
)
qr
.
add_data
(
data
=
content
)
qr
.
make
(
fit
=
True
)
img
=
qr
.
make_image
()
return
img
@
staticmethod
def
_image_to_byte_array
(
image
:
BaseImage
)
->
bytes
:
byte_stream
=
io
.
BytesIO
()
image
.
save
(
byte_stream
)
return
byte_stream
.
getvalue
()
api/core/tools/provider/builtin/qrcode/tools/qrcode_generator.yaml
View file @
0b50c525
...
@@ -2,9 +2,9 @@ identity:
...
@@ -2,9 +2,9 @@ identity:
name
:
qrcode_generator
name
:
qrcode_generator
author
:
Bowen Liang
author
:
Bowen Liang
label
:
label
:
en_US
:
QR Code Generator
en_US
:
Generate QR Code
zh_Hans
:
二维码生成器
zh_Hans
:
生成二维码
pt_BR
:
QR Code Generator
pt_BR
:
Generate QR Code
description
:
description
:
human
:
human
:
en_US
:
A tool for generating QR code image
en_US
:
A tool for generating QR code image
...
@@ -24,3 +24,53 @@ parameters:
...
@@ -24,3 +24,53 @@ parameters:
zh_Hans
:
二维码文本内容
zh_Hans
:
二维码文本内容
pt_BR
:
二维码文本内容
pt_BR
:
二维码文本内容
form
:
llm
form
:
llm
-
name
:
error_correction
type
:
select
required
:
true
default
:
M
label
:
en_US
:
Error Correction
zh_Hans
:
容错等级
pt_BR
:
Error Correction
human_description
:
en_US
:
Error Correction in L, M, Q or H, from low to high, the bigger size of generated QR code with the better error correction effect
zh_Hans
:
容错等级,可设置为低、中、偏高或高,从低到高,生成的二维码越大且容错效果越好
pt_BR
:
Error Correction in L, M, Q or H, from low to high, the bigger size of generated QR code with the better error correction effect
options
:
-
value
:
L
label
:
en_US
:
Low
zh_Hans
:
低
pt_BR
:
Low
-
value
:
M
label
:
en_US
:
Medium
zh_Hans
:
中
pt_BR
:
Medium
-
value
:
Q
label
:
en_US
:
Quartile
zh_Hans
:
偏高
pt_BR
:
Quartile
-
value
:
H
label
:
en_US
:
High
zh_Hans
:
高
pt_BR
:
High
form
:
form
-
name
:
border
type
:
number
required
:
true
default
:
2
min
:
0
max
:
100
label
:
en_US
:
border size
zh_Hans
:
边框粗细
pt_BR
:
border size
human_description
:
en_US
:
border size(default to 2)
zh_Hans
:
边框粗细的格数(默认为2)
pt_BR
:
border size(default to 2)
llm
:
border size, default to 2
form
:
form
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