Commit 054eb744 authored by Patrick Schlindwein's avatar Patrick Schlindwein
Browse files

Merge branch '#23_strategy_pattern' into 'master'

#23 Adds API endpoints and corresponding tests for text summarization to the nlp project.

See merge request !14
parents 5443f286 5e4708ee
......@@ -8,15 +8,32 @@ app = FastAPI(
description="Based on spacy offer several nlp stuff such as summary, intent-id-generation as it is needed by the IntentFinder"
)
strategies = [] # strategies = [strat1(), strat2(), ...]
@app.get("/")
async def root():
return {"message": "nlp server is available"}
@app.post("/summarize-text", summary="Generate a summary of the given text.")
async def get_summary(text: str):
return summary.get_summary(text)
@app.get("/strategies")
async def api_strategies():
res = []
for strategy in strategies:
res.append(strategy.id)
return res
@app.post("/summarize/{strategy_id}", summary="Generate a summary of the given text.")
async def summarize(strategy_id: str, req: Request):
for strategy in strategies:
if strategy.id == strategy_id:
quality = 0.5
req_json = await req.json()
summary = strategy.summarize(req_json["text"])
return {"strategy": strategy_id, "quality": quality, "summary": summary}
return {"error": "invalid id"}
@app.post("/intentid", summary="Generate an intent id from a given intent text")
......
from unittest import TestCase
from app.nlp_server import app
import app.nlp_server
from fastapi.testclient import TestClient
from app.summary.summary_strategy_interface import ISummaryStrategy
import json
class TestStrategy1(ISummaryStrategy):
id = "test1"
def summarize(self, text: str) -> str:
return text
class TestStrategy2(ISummaryStrategy):
id = "test2"
def summarize(self, text: str) -> str:
return "result text"
class TestNlpApi(TestCase):
def test_root(self):
client = TestClient(app)
client = TestClient(app.nlp_server.app)
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "nlp server is available"}
def test_strategies(self):
# init test client
app.nlp_server.strategies = [TestStrategy1(), TestStrategy2()]
client = TestClient(app.nlp_server.app)
# run test
response = client.get("/strategies")
#assert results
assert response.status_code == 200
assert response.json() == ["test1", "test2"]
def test_summarize(self):
# init test client
app.nlp_server.strategies = [TestStrategy1(), TestStrategy2()]
client = TestClient(app.nlp_server.app)
# run test
response1 = client.post(
"/summarize/test1", json.dumps({"text": "test text"}))
response2 = client.post(
"/summarize/test2", json.dumps({"text": "test text"}))
#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"}
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