Commit d92a2731 authored by Christof Walther's avatar Christof Walther Committed by Patrick Schlindwein
Browse files

#60 added the post variant of the summarize api (nlp) endpoint

parent 2eb67996
from fastapi import FastAPI from fastapi import FastAPI, Request
from app.summary.summary_word_embedding import WordEmbeddingSummarizer
from app.utilities import generator
from app.summary.simple_spacy_summarizer import SimpleSpacySummarizer from app.summary.simple_spacy_summarizer import SimpleSpacySummarizer
from app.summary.summary_sentence_embedding import SentenceEmbeddingSummarizer
from app.summary.summarization_with_strategy_TFIDF import SummaryTFIDF from app.summary.summarization_with_strategy_TFIDF import SummaryTFIDF
from app.summary.summary_bert import BertSummary from app.summary.summary_bert import BertSummary
from app.summary.summary_sentence_embedding import SentenceEmbeddingSummarizer
from app.summary.summary_word_embedding import WordEmbeddingSummarizer
from app.utilities import generator
app = FastAPI( app = FastAPI(
title="IntentFinder: NLP-API", title="IntentFinder: NLP-API",
...@@ -47,9 +47,7 @@ async def api_strategies(): ...@@ -47,9 +47,7 @@ async def api_strategies():
return res return res
@app.get("/summarize/{strategy_id}", summary="Generate a summary of the given" async def call_strategy(strategy_id: str, text: str, max_length: int):
" text.")
async def summarize(strategy_id: str, max_length: int, text: str):
""" """
This function will summarize a given text with a given summarization This function will summarize a given text with a given summarization
strategy strategy
...@@ -69,6 +67,36 @@ async def summarize(strategy_id: str, max_length: int, text: str): ...@@ -69,6 +67,36 @@ async def summarize(strategy_id: str, max_length: int, text: str):
return {"error": "invalid id"} return {"error": "invalid id"}
@app.get("/summarize/{strategy_id}", summary="Generate a summary of the given"
" text.")
async def get_summarize(strategy_id: str, max_length: int, text: str):
"""
get variant of the summarize api endpoint
:param max_length: max number of characters in the summarization
:param strategy_id: The id of the strategy
:param text: The text to be summarized
:return: The summary, strategy and quality of the summary in JSON format
"""
return await call_strategy(strategy_id, text, max_length)
@app.post("/summarize/{strategy_id}", summary="Generate a summary of the given"
" text.")
async def post_summarize(strategy_id: str, req: Request):
"""
post variant of the summarize api endpoint
:param strategy_id: The id of the strategy
:param req: The post body of the Request. Should have a text and maxLength
field.
:return: The summary, strategy and quality of the summary in JSON format
"""
req_json = await req.json()
return await call_strategy(strategy_id, req_json["text"],
req_json["maxLength"])
@app.post("/intentid", summary="Generate an intent id from a given intent" @app.post("/intentid", summary="Generate an intent id from a given intent"
" text") " text")
async def generate_intent_id(intent: str, max_tokens: int): async def generate_intent_id(intent: str, max_tokens: int):
......
from unittest import TestCase from unittest import TestCase
import app.nlp_server
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
import app.nlp_server
from app.summary.summary_strategy_interface import ISummaryStrategy from app.summary.summary_strategy_interface import ISummaryStrategy
import json
class TestStrategy1(ISummaryStrategy): class TestStrategy1(ISummaryStrategy):
""" """
...@@ -55,9 +59,10 @@ class TestNlpApi(TestCase): ...@@ -55,9 +59,10 @@ class TestNlpApi(TestCase):
assert response.status_code == 200 assert response.status_code == 200
assert response.json() == ["test1", "test2"] assert response.json() == ["test1", "test2"]
def test_summarize(self): def test_get_summarize(self):
""" """
In this case the "/summarize/{strategy}" path of the rest api is tested In this case the get variant of the "/summarize/{strategy}"
path of the rest api is tested
""" """
# init test client # init test client
...@@ -77,3 +82,27 @@ class TestNlpApi(TestCase): ...@@ -77,3 +82,27 @@ class TestNlpApi(TestCase):
assert response2.status_code == 200 assert response2.status_code == 200
assert response2.json() == {"strategy": "test2", assert response2.json() == {"strategy": "test2",
"quality": 0.5, "summary": "result text"} "quality": 0.5, "summary": "result text"}
def test_post_summarize(self):
"""
In this case the post variant of the "/summarize/{strategy}"
path of the rest api is tested
"""
app.nlp_server.strategies = [TestStrategy1(), TestStrategy2()]
client = TestClient(app.nlp_server.app)
test_data = json.dumps({"text": "test text", "maxLength": 130})
# run test
response1 = client.post(
"/summarize/test1", test_data)
response2 = client.post(
"/summarize/test2", test_data)
# assert result
assert response1.status_code == 200
assert response1.json() == {"strategy": "test1",
"quality": 0.5, "summary": "test text"}
assert response2.status_code == 200
assert response2.json() == {"strategy": "test2",
"quality": 0.5, "summary": "result text"}
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment