Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

BlenderBot2 interactive_web: Object of type AverageMetric is not JSON serializable #3821

Merged
merged 3 commits into from
Jul 20, 2021

Conversation

justquick
Copy link
Contributor

Patch description
Simply cast the model_response['metrics'] values as plain ints instead of AverageMetric objects so model_response can be JSON serialized.

Testing steps
Running the following command

$ parlai interactive_web --task blended_skill_talk:all -mf zoo:blenderbot2/blenderbot2_400M/model --search-server http://localhost:5000

Open localhost:8080 when the interactive_web task comes online. Put in a message and post and backend gets this traceback:

Traceback (most recent call last):
  File "/Users/jquick/.pyenv/versions/3.8.7/lib/python3.8/socketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/Users/jquick/.pyenv/versions/3.8.7/lib/python3.8/socketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
  File "/Users/jquick/.pyenv/versions/3.8.7/lib/python3.8/socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/Users/jquick/.pyenv/versions/3.8.7/lib/python3.8/socketserver.py", line 720, in __init__
    self.handle()
  File "/Users/jquick/.pyenv/versions/3.8.7/lib/python3.8/http/server.py", line 427, in handle
    self.handle_one_request()
  File "/Users/jquick/.pyenv/versions/3.8.7/lib/python3.8/http/server.py", line 415, in handle_one_request
    method()
  File "/Users/jquick/Projects/ParlAI/parlai/scripts/interactive_web.py", line 202, in do_POST
    json_str = json.dumps(model_response)
  File "/Users/jquick/.pyenv/versions/3.8.7/lib/python3.8/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/Users/jquick/.pyenv/versions/3.8.7/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Users/jquick/.pyenv/versions/3.8.7/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/Users/jquick/.pyenv/versions/3.8.7/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Av

Other information
This is what the chunk of model_response looks like before the change

'metrics': {'clen': AverageMetric(5),
            'ctrunc': AverageMetric(0),
            'ctrunclen': AverageMetric(0)}

And after

'metrics': {'clen': 5,
            'ctrunc': 0,
            'ctrunclen': 0}

Now interactive_web page is working and can chat w/ bot

@facebook-github-bot
Copy link

Hi @justquick!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

Copy link
Contributor

@stephenroller stephenroller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution. Instead, could you please change it instead to:

json.dumps(model_response.json_safe_payload())?

@justquick
Copy link
Contributor Author

Ah, didnt know that was a thing, im new to this but finding it very useful. Fixed & tested

Copy link
Contributor

@klshuster klshuster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the fix!

@klshuster klshuster merged commit 0c7cd04 into facebookresearch:master Jul 20, 2021
@stephenroller
Copy link
Contributor

Thanks for the fix!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants