code: def main():
      global identifier
  
      parser = optparse.OptionParser()
      parser.add_option(
          "-s",
          "--serve",
          action="store_true",
          default=False,
          dest="serve",
          help="launch web service",
      )
      parser.add_option(
          "--host", default=HOST, dest="host", help="host/ip to bind to"
      )
      parser.add_option(
          "--port", default=PORT, dest="port", help="port to listen on"
      )
      parser.add_option(
          "-v",
          action="count",
          dest="verbosity",
          help="increase verbosity (repeat for greater effect)",
      )
      parser.add_option("-m", dest="model", help="load model from file")
      parser.add_option(
          "-l",
          "--langs",
          dest="langs",
          help="comma-separated set of target ISO639 language codes (e.g en,de)",
      )
      parser.add_option(
          "-r",
          "--remote",
          action="store_true",
          default=False,
          help="auto-detect IP address for remote access",
      )
      parser.add_option(
          "-b",
          "--batch",
          action="store_true",
          default=False,
          help="specify a list of files on the command line",
      )
      parser.add_option(
          "--demo",
          action="store_true",
          default=False,
          help="launch an in-browser demo application",
      )
      parser.add_option(
          "-d",
          "--dist",
          action="store_true",
          default=False,
          help="show full distribution over languages",
      )
      parser.add_option("-u", "--url", help="langid of URL")
      parser.add_option(
          "--line",
          action="store_true",
          default=False,
          help="process pipes line-by-line rather than as a document",
      )
      parser.add_option(
          "-n",
          "--normalize",
          action="store_true",
          default=False,
          help="normalize confidence scores to probability values",
      )
      options, args = parser.parse_args()
  
      if options.verbosity:
          logging.basicConfig(
              level=max((5 - options.verbosity) * 10, 0)
          )
      else:
          logging.basicConfig()
  
      if options.batch and options.serve:
          parser.error(
              "cannot specify both batch and serve at the same time"
          )
  
      # unpack a model
      if options.model:
          try:
              identifier = LanguageIdentifier.from_modelpath(
                  options.model, norm_probs=options.normalize
              )
              logger.info("Using external model: %s", options.model)
          except IOError as e:
              logger.warning(
                  "Failed to load %s: %s" % (options.model, e)
              )
  
      if identifier is None:
          identifier = LanguageIdentifier.from_modelstring(
              model, norm_probs=options.normalize
          )
          logger.info("Using internal model")
  
      if options.langs:
          langs = options.langs.split(",")
          identifier.set_languages(langs)
  
      def _process(text):
          """
          Set up a local function to do output, configured according to our settings.
          """
          if options.dist:
              payload = identifier.rank(text)
          else:
              payload = identifier.classify(text)
  
          return payload
  
      if options.url:
          import contextlib
          import urllib.request, urllib.error, urllib.parse
  
          try:
              from urllib.request import urlopen
          except ImportError:
              from urllib2 import urlopen
          with contextlib.closing(urlopen(options.url)) as url:
              text = url.read()
              output = _process(text)
              print(options.url, len(text), output)
  
      elif options.serve or options.demo:
          # from http://stackoverflow.com/questions/166506/finding-local-ip-addresses-in-python
          if options.remote and options.host is None:
              # resolve the external ip address
              import socket
  
              s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
              s.connect(("google.com", 80))
              hostname = s.getsockname()[0]
          elif options.host is None:
              # resolve the local hostname
              import socket
  
              hostname = socket.gethostbyname(socket.gethostname())
          else:
              hostname = options.host
  
          if options.demo:
              import webbrowser
  
              webbrowser.open(
                  "http://{0}:{1}/demo".format(hostname, options.port)
              )
          try:
              if FORCE_WSGIREF:
                  raise ImportError
              # Use fapws3 if available
              import fapws._evwsgi as evwsgi
              from fapws import base
  
              evwsgi.start(hostname, str(options.port))
              evwsgi.set_base_module(base)
              evwsgi.wsgi_cb(("", application))
              evwsgi.set_debug(0)
              evwsgi.run()
          except ImportError:
              print(
                  "Listening on %s:%d" % (hostname, int(options.port))
              )
              print("Press Ctrl+C to exit")
              httpd = make_server(
                  hostname, int(options.port), application
              )
              try:
                  httpd.serve_forever()
              except KeyboardInterrupt:
                  pass
      elif options.batch:
          # Start in batch mode - interpret input as paths rather than content
          # to classify.
          import sys, os, csv
          import multiprocessing as mp
  
          def generate_paths():
              for line in sys.stdin:
                  path = line.strip()
                  if path:
                      if os.path.isfile(path):
                          yield path
                      else:
                          # No such path
                          pass
  
          writer = csv.writer(sys.stdout)
          pool = mp.Pool()
          if options.dist:
              writer.writerow(["path"] + nb_classes)
              for path, ranking in pool.imap_unordered(
                  rank_path, generate_paths()
              ):
                  ranking = dict(ranking)
                  row = [path] + [ranking[c] for c in nb_classes]
                  writer.writerow(row)
          else:
              for path, (lang, conf) in pool.imap_unordered(
                  cl_path, generate_paths()
              ):
                  writer.writerow((path, lang, conf))
      else:
          import sys
  
          if sys.stdin.isatty():
              # Interactive mode
              while True:
                  try:
                      print(">>>", end=" ")
                      text = input()
                  except Exception as e:
                      print(e)
                      break
                  print(_process(text))
          else:
              # Redirected
              if options.line:
                  for line in sys.stdin:
                      print(_process(line))
              else:
                  print(_process(sys.stdin.read()))
  
imports: from __future__ import print_function
  import base64
  import bz2
  import json
  import optparse
  import sys
  import logging
  import numpy
  from wsgiref.simple_server import make_server
  from wsgiref.util import shift_path_info
  from collections import defaultdict
label_desc: Use image node_id 8 for calling a global function with example usage: main() and returns: payload