Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
emoUS-public
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
general
dsml
emoUS-public
Commits
17229c70
Commit
17229c70
authored
2 years ago
by
zqwerty
Browse files
Options
Downloads
Patches
Plain Diff
fix bug in t5dst, t5nlg, and scgpt interface
parent
a959c56a
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
convlab/base_models/t5/dst/dst.py
+25
-7
25 additions, 7 deletions
convlab/base_models/t5/dst/dst.py
convlab/base_models/t5/nlg/nlg.py
+18
-27
18 additions, 27 deletions
convlab/base_models/t5/nlg/nlg.py
convlab/nlg/scgpt/scgpt.py
+11
-0
11 additions, 0 deletions
convlab/nlg/scgpt/scgpt.py
with
54 additions
and
34 deletions
convlab/base_models/t5/dst/dst.py
+
25
−
7
View file @
17229c70
import
logging
import
logging
import
os
import
torch
import
torch
from
copy
import
deepcopy
from
transformers
import
AutoTokenizer
,
AutoModelForSeq2SeqLM
,
AutoConfig
from
transformers
import
AutoTokenizer
,
AutoModelForSeq2SeqLM
,
AutoConfig
from
convlab.dst.dst
import
DST
from
convlab.dst.dst
import
DST
from
convlab.base_models.t5.dst.serialization
import
deserialize_dialogue_state
from
convlab.base_models.t5.dst.serialization
import
deserialize_dialogue_state
from
convlab.util
.custom_util
import
model_downloader
from
convlab.util
import
load_ontology
class
T5DST
(
DST
):
class
T5DST
(
DST
):
def
__init__
(
self
,
speaker
,
context_window_size
,
model_name_or_path
,
device
=
'
cuda
'
):
def
__init__
(
self
,
dataset_name
,
speaker
,
context_window_size
,
model_name_or_path
,
device
=
'
cuda
'
):
assert
speaker
in
[
'
user
'
,
'
system
'
]
assert
speaker
in
[
'
user
'
,
'
system
'
]
assert
context_window_size
>
0
assert
context_window_size
>
0
self
.
ontology
=
load_ontology
(
dataset_name
)
self
.
speaker
=
speaker
self
.
speaker
=
speaker
self
.
opponent
=
'
system
'
if
speaker
==
'
user
'
else
'
user
'
self
.
opponent
=
'
system
'
if
speaker
==
'
user
'
else
'
user
'
self
.
context_window_size
=
context_window_size
self
.
context_window_size
=
context_window_size
...
@@ -24,7 +25,12 @@ class T5DST(DST):
...
@@ -24,7 +25,12 @@ class T5DST(DST):
logging
.
info
(
"
T5DST loaded
"
)
logging
.
info
(
"
T5DST loaded
"
)
def
update
(
self
,
context
):
def
update
(
self
,
user_action
=
None
):
if
self
.
state
[
'
history
'
][
0
][
1
]
==
'
null
'
:
# skip first dummy turn
context
=
self
.
state
[
'
history
'
][
1
:]
else
:
context
=
self
.
state
[
'
history
'
]
if
len
(
context
)
>
0
and
type
(
context
[
0
])
is
list
and
len
(
context
[
0
])
>
1
:
if
len
(
context
)
>
0
and
type
(
context
[
0
])
is
list
and
len
(
context
[
0
])
>
1
:
context
=
[
item
[
1
]
for
item
in
context
]
context
=
[
item
[
1
]
for
item
in
context
]
context
=
context
[
-
self
.
context_window_size
:]
context
=
context
[
-
self
.
context_window_size
:]
...
@@ -37,7 +43,17 @@ class T5DST(DST):
...
@@ -37,7 +43,17 @@ class T5DST(DST):
output_seq
=
self
.
tokenizer
.
decode
(
output_seq
[
0
],
skip_special_tokens
=
True
)
output_seq
=
self
.
tokenizer
.
decode
(
output_seq
[
0
],
skip_special_tokens
=
True
)
# print(output_seq)
# print(output_seq)
state
=
deserialize_dialogue_state
(
output_seq
.
strip
())
state
=
deserialize_dialogue_state
(
output_seq
.
strip
())
return
state
self
.
state
[
'
belief_state
'
]
=
state
return
self
.
state
def
init_session
(
self
):
self
.
state
=
dict
()
self
.
state
[
'
belief_state
'
]
=
deepcopy
(
self
.
ontology
[
'
state
'
])
self
.
state
[
'
booked
'
]
=
dict
()
self
.
state
[
'
history
'
]
=
[]
self
.
state
[
'
system_action
'
]
=
[]
self
.
state
[
'
user_action
'
]
=
[]
self
.
state
[
'
terminated
'
]
=
False
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
...
@@ -59,7 +75,9 @@ if __name__ == '__main__':
...
@@ -59,7 +75,9 @@ if __name__ == '__main__':
"
You are welcome. Is there anything else I can help you with today?
"
,
"
You are welcome. Is there anything else I can help you with today?
"
,
"
No, I am all set. Have a nice day. Bye.
"
],
"
No, I am all set. Have a nice day. Bye.
"
],
]
]
dst
=
T5DST
(
speaker
=
'
user
'
,
context_window_size
=
100
,
model_name_or_path
=
'
output/dst/multiwoz21/user/context_100
'
)
dst
=
T5DST
(
'
multiwoz21
'
,
speaker
=
'
user
'
,
context_window_size
=
100
,
model_name_or_path
=
'
ConvLab/t5-small-dst-multiwoz21
'
)
dst
.
init_session
()
for
context
in
contexts
:
for
context
in
contexts
:
print
(
dst
.
update
(
context
))
dst
.
state
[
'
history
'
]
=
context
print
(
dst
.
update
())
print
()
print
()
This diff is collapsed.
Click to expand it.
convlab/base_models/t5/nlg/nlg.py
+
18
−
27
View file @
17229c70
import
logging
import
logging
import
os
import
torch
import
torch
from
transformers
import
AutoTokenizer
,
AutoModelForSeq2SeqLM
,
AutoConfig
from
transformers
import
AutoTokenizer
,
AutoModelForSeq2SeqLM
,
AutoConfig
from
convlab.nlg.nlg
import
NLG
from
convlab.nlg.nlg
import
NLG
from
convlab.base_models.t5.nlu.serialization
import
serialize_dialogue_acts
from
convlab.base_models.t5.nlu.serialization
import
serialize_dialogue_acts
from
convlab.util.custom_util
import
model_downloader
class
T5NLG
(
NLG
):
class
T5NLG
(
NLG
):
...
@@ -33,7 +31,18 @@ class T5NLG(NLG):
...
@@ -33,7 +31,18 @@ class T5NLG(NLG):
else
:
else
:
utts
=
[
''
]
utts
=
[
''
]
input_seq
=
'
\n
'
.
join
([
f
"
{
self
.
opponent
if
(
i
%
2
)
==
(
len
(
utts
)
%
2
)
else
self
.
speaker
}
:
{
utt
}
"
for
i
,
utt
in
enumerate
(
utts
)])
input_seq
=
'
\n
'
.
join
([
f
"
{
self
.
opponent
if
(
i
%
2
)
==
(
len
(
utts
)
%
2
)
else
self
.
speaker
}
:
{
utt
}
"
for
i
,
utt
in
enumerate
(
utts
)])
if
isinstance
(
dialogue_acts
,
dict
):
# da in unified format
dialogue_acts_seq
=
serialize_dialogue_acts
(
dialogue_acts
)
dialogue_acts_seq
=
serialize_dialogue_acts
(
dialogue_acts
)
elif
isinstance
(
dialogue_acts
[
0
],
dict
):
# da without da type
dialogue_acts_seq
=
serialize_dialogue_acts
({
'
categorical
'
:
dialogue_acts
})
elif
isinstance
(
dialogue_acts
[
0
],
list
):
# da is a list of list (convlab-2 format)
dialogue_acts_seq
=
serialize_dialogue_acts
(
{
'
categorical
'
:
[{
'
intent
'
:
da
[
0
],
'
domain
'
:
da
[
1
],
'
slot
'
:
da
[
2
],
'
value
'
:
da
[
3
]}
for
da
in
dialogue_acts
]})
else
:
raise
ValueError
(
f
"
invalid dialog acts format
{
dialogue_acts
}
"
)
input_seq
=
dialogue_acts_seq
+
'
\n
'
+
input_seq
input_seq
=
dialogue_acts_seq
+
'
\n
'
+
input_seq
# print(input_seq)
# print(input_seq)
input_seq
=
self
.
tokenizer
(
input_seq
,
return_tensors
=
"
pt
"
).
to
(
self
.
device
)
input_seq
=
self
.
tokenizer
(
input_seq
,
return_tensors
=
"
pt
"
).
to
(
self
.
device
)
...
@@ -47,7 +56,7 @@ class T5NLG(NLG):
...
@@ -47,7 +56,7 @@ class T5NLG(NLG):
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
das
=
[
das
=
[
{
{
# da in unified format
"
categorical
"
:
[],
"
categorical
"
:
[],
"
non-categorical
"
:
[],
"
non-categorical
"
:
[],
"
binary
"
:
[
"
binary
"
:
[
...
@@ -63,9 +72,7 @@ if __name__ == '__main__':
...
@@ -63,9 +72,7 @@ if __name__ == '__main__':
}
}
]
]
},
},
{
[
# da without da type
"
categorical
"
:
[],
"
non-categorical
"
:
[
{
{
"
intent
"
:
"
inform
"
,
"
intent
"
:
"
inform
"
,
"
domain
"
:
"
taxi
"
,
"
domain
"
:
"
taxi
"
,
...
@@ -83,25 +90,9 @@ if __name__ == '__main__':
...
@@ -83,25 +90,9 @@ if __name__ == '__main__':
"
end
"
:
78
"
end
"
:
78
}
}
],
],
"
binary
"
:
[
[
# da is a list of list (convlab-2 format)
{
[
"
reqmore
"
,
"
general
"
,
""
,
""
]
"
intent
"
:
"
book
"
,
],
"
domain
"
:
"
taxi
"
,
"
slot
"
:
""
}
]
},
{
"
categorical
"
:
[],
"
non-categorical
"
:
[],
"
binary
"
:
[
{
"
intent
"
:
"
reqmore
"
,
"
domain
"
:
"
general
"
,
"
slot
"
:
""
}
]
},
{
{
"
categorical
"
:
[],
"
categorical
"
:
[],
"
non-categorical
"
:
[],
"
non-categorical
"
:
[],
...
@@ -132,7 +123,7 @@ if __name__ == '__main__':
...
@@ -132,7 +123,7 @@ if __name__ == '__main__':
"
You are welcome. Is there anything else I can help you with today?
"
"
You are welcome. Is there anything else I can help you with today?
"
"
No, I am all set. Have a nice day. Bye.
"
],
"
No, I am all set. Have a nice day. Bye.
"
],
]
]
nlg
=
T5NLG
(
speaker
=
'
system
'
,
context_window_size
=
0
,
model_name_or_path
=
'
output/
nlg
/
multiwoz21
/system/context_3
'
)
nlg
=
T5NLG
(
speaker
=
'
system
'
,
context_window_size
=
0
,
model_name_or_path
=
'
ConvLab/t5-small-
nlg
-
multiwoz21
'
)
for
da
,
context
in
zip
(
das
,
contexts
):
for
da
,
context
in
zip
(
das
,
contexts
):
print
(
da
)
print
(
da
)
print
(
nlg
.
generate
(
da
,
context
))
print
(
nlg
.
generate
(
da
,
context
))
...
...
This diff is collapsed.
Click to expand it.
convlab/nlg/scgpt/scgpt.py
+
11
−
0
View file @
17229c70
...
@@ -19,6 +19,17 @@ class SCGPT(NLG):
...
@@ -19,6 +19,17 @@ class SCGPT(NLG):
self
.
model
.
load_state_dict
(
torch
.
load
(
model_path
))
self
.
model
.
load_state_dict
(
torch
.
load
(
model_path
))
def
generate
(
self
,
action
):
def
generate
(
self
,
action
):
if
isinstance
(
action
,
dict
):
# da in unified format
pass
elif
isinstance
(
action
[
0
],
dict
):
# da without da type
action
=
{
'
categorical
'
:
action
}
elif
isinstance
(
action
[
0
],
list
):
# da is a list of list (convlab-2 format)
action
=
{
'
categorical
'
:
[{
'
intent
'
:
da
[
0
],
'
domain
'
:
da
[
1
],
'
slot
'
:
da
[
2
],
'
value
'
:
da
[
3
]}
for
da
in
action
]}
else
:
raise
ValueError
(
f
"
invalid dialog acts format
{
action
}
"
)
action_str
=
act2str
(
action
)
action_str
=
act2str
(
action
)
output
=
self
.
_inference_batch
([
action_str
])[
0
]
output
=
self
.
_inference_batch
([
action_str
])[
0
]
return
output
return
output
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment