code: def smart_unicode_decode(encoded_string):
      
      if not encoded_string:
          return u''
  
      # optimization -- first try ascii
      try:
          return encoded_string.decode('ascii')
      except UnicodeDecodeError:
          pass
  
      # detect encoding
      detected_encoding = chardet.detect(encoded_string)
      # bug 54 -- depending on chardet version, if encoding is not guessed,
      # either detected_encoding will be None or detected_encoding['encoding'] will be None
      detected_encoding = detected_encoding['encoding'] if detected_encoding and detected_encoding.get('encoding') else 'utf8'
      decoded_string = six.text_type(
          encoded_string,
          encoding=detected_encoding,
          errors='replace'
      )
  
      # unicode string may still have useless BOM character at the beginning
      if decoded_string and decoded_string[0] in bom_headers:
          decoded_string = decoded_string[1:]
  
      return decoded_string
docs: Given an encoded string of unknown format, detect the format with
          chardet and return the unicode version.
          Example input from bug #11:
           ('\xfe\xff\x00I\x00n\x00s\x00p\x00e\x00c\x00t\x00i\x00o\x00n\x00'
            '\x00R\x00e\x00p\x00o\x00r\x00t\x00 \x00v\x002\x00.\x002')