Exemplo n.º 1
0
    def respond(s):
        global show_exit_button
        """Respond to a POST request."""
        print 'path: ' + s.path
        if s.path == '/exit':
            if not show_exit_button:
                print 'Ignoring exit request. show_exit_button=False.'
            else:
                print 'Got exit command'
                s.send_response(200)
                s.send_header("Content-type", "text/plain;charset=UTF-8")
                s.end_headers()
                s.wfile.write('System is exiting.')
                assassin = Thread(target=s.server.shutdown)
                assassin.daemon = True
                assassin.start()
        elif s.path == '/get_summary':
            print 'Got summarize command'
            # Extract and print the contents of the POST
            length = int(s.headers['Content-Length'])
            err = None
            try:
                post_data = urlparse.parse_qs(
                    s.rfile.read(length).decode('utf-8'))
                input_text = post_data['input_text'][0]
                summary_length = int(post_data['summary_length'][0])
                print 'length: ' + str(summary_length)
                tfidf = (post_data['tfidf'][0] == "tfidf")
                sentiment = (post_data['sentiment'][0] == "sentiment")
                w2v = (post_data['w2v'][0] == "w2v")
                for key, value in post_data.iteritems():
                    print "%s=%s" % (key, value)
                    #if key == 'input_text': input_text = value
                #print u'input_text: '+convert_all(input_text)
                input_text = convert_all(input_text)
                #print input_text.encode('latin1').decode('utf-8')
            except:
                err = 'Failure while processing input. Please make sure that you input valid text.'
            try:
                summary = multsum.summarize_strings(
                    [[input_text]],
                    length=summary_length,
                    use_tfidf_similarity=tfidf,
                    use_sentiment_similarity=sentiment,
                    use_w2v_similarity=w2v,
                    split_sentences=True,
                    w2v_backend=True)
            except:
                err = 'Failed to run the summarization subsystem. Internal error. Make sure that your input is valid.'

            s.send_response(200)
            s.send_header("Content-type", "text/plain;charset=UTF-8")
            s.end_headers()
            if not err:
                s.wfile.write(summary.encode('utf-8'))
            else:
                s.wfile.write(err)
        elif s.path == '/':
            print 'Got root request.'
            s.send_response(200)
            s.send_header("Content-type", "text/html;charset=UTF-8")
            s.end_headers()
            s.wfile.write(BASE_DOCUMENT_PREFIX)
            s.wfile.write(BASE_DOCUMENT_FORM_1)
            if show_exit_button:
                s.wfile.write(BASE_DOCUMENT_FORM_EXIT_BUTTON)
            s.wfile.write(BASE_DOCUMENT_FORM_2)
            s.wfile.write(BASE_DOCUMENT_SUFFIX)
        elif s.path == '/backend_status':
            s.send_response(200)
            s.send_header("Content-type", "text/plain;charset=UTF-8")
            s.end_headers()
            if use_w2v_similarity and backend_client.backend_check():
                s.wfile.write('OK')
            else:
                s.wfile.write('NOK')
        else:
            print 'Got other request (404).'
            s.send_response(404)
            s.send_header("Content-type", "text/html;charset=UTF-8")
            s.end_headers()
            s.wfile.write(
                '''<html><head><title>404: Page not found.</title></head><body><h1>404: Page not found</h1></body></html>
      ''')
Exemplo n.º 2
0
  def respond(s):
    global show_exit_button
    """Respond to a POST request."""
    print 'path: '+s.path
    if s.path == '/exit':
      if not show_exit_button:
        print 'Ignoring exit request. show_exit_button=False.'
      else:
        print 'Got exit command'
        s.send_response(200)
        s.send_header("Content-type", "text/plain;charset=UTF-8")
        s.end_headers()
        s.wfile.write('System is exiting.')
        assassin = Thread(target=s.server.shutdown)
        assassin.daemon = True
        assassin.start()
    elif s.path == '/get_summary':
      print 'Got summarize command'
      # Extract and print the contents of the POST
      length = int(s.headers['Content-Length'])
      err = None
      try:
        post_data = urlparse.parse_qs(s.rfile.read(length).decode('utf-8'))
        input_text     = post_data['input_text'][0]
        summary_length = int(post_data['summary_length'][0])
        print 'length: '+str(summary_length)
        tfidf          = (post_data['tfidf'][0] == "tfidf")
        sentiment      = (post_data['sentiment'][0] == "sentiment")
        w2v            = (post_data['w2v'][0] == "w2v")
        for key, value in post_data.iteritems():
          print "%s=%s" % (key, value)
          #if key == 'input_text': input_text = value
        #print u'input_text: '+convert_all(input_text)
        input_text = convert_all(input_text)
        #print input_text.encode('latin1').decode('utf-8')
      except:
        err = 'Failure while processing input. Please make sure that you input valid text.'
      try:
        summary = multsum.summarize_strings([[input_text]], length=summary_length, use_tfidf_similarity=tfidf, use_sentiment_similarity=sentiment, use_w2v_similarity=w2v, split_sentences=True, w2v_backend=True)
      except:
        err = 'Failed to run the summarization subsystem. Internal error. Make sure that your input is valid.'

      s.send_response(200)
      s.send_header("Content-type", "text/plain;charset=UTF-8")
      s.end_headers()
      if not err:
        s.wfile.write(summary.encode('utf-8'))
      else:
        s.wfile.write(err)
    elif s.path == '/':
      print 'Got root request.'
      s.send_response(200)
      s.send_header("Content-type", "text/html;charset=UTF-8")
      s.end_headers()
      s.wfile.write(BASE_DOCUMENT_PREFIX)
      s.wfile.write(BASE_DOCUMENT_FORM_1)
      if show_exit_button:
        s.wfile.write(BASE_DOCUMENT_FORM_EXIT_BUTTON)
      s.wfile.write(BASE_DOCUMENT_FORM_2)
      s.wfile.write(BASE_DOCUMENT_SUFFIX)
    elif s.path == '/backend_status':
      s.send_response(200)
      s.send_header("Content-type", "text/plain;charset=UTF-8")
      s.end_headers()
      if use_w2v_similarity and backend_client.backend_check():
        s.wfile.write('OK')
      else:
        s.wfile.write('NOK')
    else:
      print 'Got other request (404).'
      s.send_response(404)
      s.send_header("Content-type", "text/html;charset=UTF-8")
      s.end_headers()
      s.wfile.write('''<html><head><title>404: Page not found.</title></head><body><h1>404: Page not found</h1></body></html>
      ''')
Exemplo n.º 3
0
        #elif sys.argv[i] == '--no-exit-button':
        #  show_exit_button = False

    #p_model = Process(target=init_model)
    #p_model.start()
    for port_number in range(PORT_NUMBER_FIRST, PORT_NUMBER_LAST):
        try:
            p = Process(target=run_server, kwargs={'port_number': port_number})
            p.start()
            break
        except socket.error, e:
            print('Failed to start http server at port %d.' % (port_number))

    backend_started = False
    if use_w2v_similarity:
        if not backend_client.backend_check(
        ) and not backend_client.backend_is_initializing():
            p_backend = Process(target=backend_worker.run_backend,
                                args={'w2v_vector_file': w2v_vector_file})
            p_backend.start()
            backend_started = True
    if launch_browser:
        try:
            run_browser()
            try:
                p.terminate()
                if backend_started:
                    print('Sending exit command to w2v worker.')
                    backend_client.backend_exit()
                    p_backend.terminate()
            except:
                print 'Failed to kill server. Please do so manually.'
Exemplo n.º 4
0
    #elif sys.argv[i] == '--no-exit-button':
    #  show_exit_button = False

  #p_model = Process(target=init_model)
  #p_model.start()
  for port_number in range(PORT_NUMBER_FIRST,PORT_NUMBER_LAST):
    try:
      p = Process(target=run_server, kwargs={'port_number': port_number})
      p.start()
      break
    except socket.error, e:
      print('Failed to start http server at port %d.'%(port_number))

  backend_started = False
  if use_w2v_similarity:
    if not backend_client.backend_check() and not backend_client.backend_is_initializing():
      p_backend = Process(target=backend_worker.run_backend, args={'w2v_vector_file': w2v_vector_file})
      p_backend.start()
      backend_started = True
  if launch_browser:
    try:
      run_browser()
      try:
        p.terminate()
        if backend_started:
          print('Sending exit command to w2v worker.')
          backend_client.backend_exit()
          p_backend.terminate()
      except:
        print 'Failed to kill server. Please do so manually.'
    except: