<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def attach_file(self, filename, resource_id=None):
 """Upload an attachment for the model run. Paramerers filename : string Path to uploaded file resource_id : string Identifier of the attachment. If None, the filename will be used as resource identifier. Returns ------- ModelRunHandle Refreshed run handle. """

        # Use file base name as resource identifier if none given
        if resource_id is None:
            resource_id = os.path.basename(filename)
        # Need to append resource identifier to base attachment Url
        upload_url = self.links[REF_MODEL_RUN_ATTACHMENTS] + '/' + resource_id
        # Upload model output
        response = requests.post(
            upload_url,
            files={'file': open(filename, 'rb')}
        )
        if response.status_code != 200:
            try:
                raise ValueError(json.loads(response.text)['message'])
            except KeyError as ex:
                raise ValueError('invalid state change: ' + str(response.text))
        # Returned refreshed verion of the handle
        return self.refresh()