piskvorky / gensim

Topic Modelling for Humans
https://radimrehurek.com/gensim
GNU Lesser General Public License v2.1
15.66k stars 4.38k forks source link

Error using Gensim with Flask API #2492

Closed as-ghub closed 5 years ago

as-ghub commented 5 years ago

I am trying to build a python API for the first time. Trying to build an API end point to summarize using flask rest and gensim libraries. Any suggestions?

Code:

import flask
from flask import json
from flask import request
from flask_restful import Resource, Api, reqparse
from gensim.summarization.summarizer import summarize

app = flask.Flask(__name__)
app.config["DEBUG"] = True

@app.route('/', methods=['GET'])
def home():
    return "<p>This is a test GET End point</p>"

@app.route('/', methods = ['POST'])
def api_message():
        return summarize(request.data)  
app.run()

Expected output:

 * Serving Flask app "TextSummarizerAPI" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 295-630-739
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 

Actual output:


Traceback (most recent call last):
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Blog Code\TextSummarizer\TextSummarizer\TextSummarizer\PythonAPI\TextSummarizerAPI.py", line 18, in api_message
    return summarize(request.data)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gensim\summarization\summarizer.py", line 419, in summarize
    sentences = _clean_text_by_sentences(text)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gensim\summarization\textcleaner.py", line 248, in clean_text_by_sentences
    original_sentences = split_sentences(text)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gensim\summarization\textcleaner.py", line 77, in split_sentences
    processed = replace_abbreviations(text)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gensim\summarization\textcleaner.py", line 102, in replace_abbreviations
    return replace_with_separator(text, SEPARATOR, [AB_SENIOR, AB_ACRONYM])
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gensim\summarization\textcleaner.py", line 148, in replace_with_separator
    result = regex.sub(replacement, result)
TypeError: cannot use a string pattern on a bytes-like object
127.0.0.1 - - [11/May/2019 11:57:05] "POST / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Blog Code\TextSummarizer\TextSummarizer\TextSummarizer\PythonAPI\TextSummarizerAPI.py", line 18, in api_message
    return summarize(request.data)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gensim\summarization\summarizer.py", line 419, in summarize
    sentences = _clean_text_by_sentences(text)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gensim\summarization\textcleaner.py", line 248, in clean_text_by_sentences
    original_sentences = split_sentences(text)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gensim\summarization\textcleaner.py", line 77, in split_sentences
    processed = replace_abbreviations(text)
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gensim\summarization\textcleaner.py", line 102, in replace_abbreviations
    return replace_with_separator(text, SEPARATOR, [AB_SENIOR, AB_ACRONYM])
  File "C:\Users\aniru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gensim\summarization\textcleaner.py", line 148, in replace_with_separator
    result = regex.sub(replacement, result)
TypeError: cannot use a string pattern on a bytes-like object
 * Detected change in 'C:\\Blog Code\\TextSummarizer\\TextSummarizer\\TextSummarizer\\PythonAPI\\TextSummarizerAPI.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 295-630-739
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
mpenkov commented 5 years ago

Please log the value of request.data.

What is its type? I suspect you're passing bytes instead of unicode.

as-ghub commented 5 years ago

Thanks for the hint @mpenkov . I changed the code this way and it worked.

@app.route('/', methods = ['POST'])
def api_message():
    text=request.data.strip().decode('utf-8')
    return summarize(text)
app.run()