Commit 83776a9a authored by Christof Walther's avatar Christof Walther
Browse files

[refactor, #76] removed unsused paramters

parent e7cce475
Pipeline #78706 passed with stages
in 3 minutes and 1 second
import json
import os
from fastapi import FastAPI, Request
from pydantic import BaseModel
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from pydantic import BaseModel # pylint: disable=no-name-in-module
from chatette.facade import Facade
from app.quality.balance_report import BalanceReport
from app.questiongenerator import QuestionGenerator
from app.summary.simple_spacy_summarizer import SimpleSpacySummarizer
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_sentence_embedding import SentenceEmbeddingSummarizer
from app.summary.summary_word_embedding import WordEmbeddingSummarizer
from app.utilities.success_response import SuccessResponse
from app.utilities.client_error_response import ClientErrorResponse
from app.utilities.server_error_response import ServerErrorResponse
from app.utilities import generator
from app.utilities.models import nlp, bert_model, bert_tokenizer
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder
from app.quality.similarity_report import SimilarityReport
from app.db.mongodb_connector import MongoDBConnector
from app.utilities.success_response import SuccessResponse
class Item(BaseModel):
......@@ -23,6 +29,10 @@ class Item(BaseModel):
answer_style: str
class Item2(BaseModel):
input_path: str
app = FastAPI(
title="IntentFinder: NLP-API",
version="1.0",
......@@ -44,19 +54,26 @@ strategies = [
BertSummary(_bert_model=bert_model, _bert_tokenizer=bert_tokenizer),
WordEmbeddingSummarizer(_nlp=nlp)]
mongo_db_connector = MongoDBConnector('root', 'localhost:27017',
'rootpassword', 'chats')
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc: RequestValidationError):
async def validation_exception_handler(request,
exc: RequestValidationError
): # pylint: disable=unused-argument
"""
Handle all raised request exceptions and return them to the client as JSON
"""
return JSONResponse(content=jsonable_encoder({'statusCode': 400,
'message': exc._error_cache,
return JSONResponse(content=jsonable_encoder({'status_code': 400,
'message': str(exc),
'body': None}))
@app.exception_handler(Exception)
async def exception_handler(request: Request, exception: Exception):
async def exception_handler(request: Request,
exception: Exception
): # pylint: disable=unused-argument
"""
Handle all raised exceptions and return them to the client as JSON
"""
......@@ -65,18 +82,34 @@ async def exception_handler(request: Request, exception: Exception):
@app.post("/questionGenerator")
async def api_questionGenerator(item: Item):
qg = QuestionGenerator()
return qg.generate(
async def api_question_generator(item: Item):
question_generator = QuestionGenerator(_nlp=nlp)
return question_generator.generate(
item.text,
num_questions=item.num_questions,
answer_style=item.answer_style
)
@app.post("/chatette")
async def test(item2: Item2):
facade = Facade(
master_file_path=item2.input_path,
output_dir_path="./app/output",
force_overwriting=True
)
facade.run()
with open(os.path.join(
"./app/output/train/output.json"),
"r"
) as json_output:
return_json = json_output.read()
return json.loads(return_json)
@app.get("/")
async def root():
return {"message": "nlp server is available"}
return SuccessResponse({"message": "nlp server is available"})
@app.get("/strategies")
......@@ -148,8 +181,78 @@ async def post_summarize(strategy_id: str, req: Request):
@app.get("/{bot}/similarIntents", summary="Get similar Intents.")
async def get_similar_intents(bot, t=0.8):
pass
async def get_similar_intents(t: float = 0.8):
"""
Run a similarity analysis on the bot's intents
Example result:
{
"intents": [ ... ]
"similar_by_answer": [
[
2,
3
]
],
"similar_by_id": [
[
2,
3
]
],
"similar_by_question": {
"3, 2": [
[
1,
1
]
]
}
}
similar_by_answer: The Result is an array of coordinates. In this example
intent 2 and 3 are similar by answer
similar_by_id: The Result is an array of coordinates. In this example
intent 2 and 3 are similar by id
similar_by_question: The Result is an object. The keys of the object
indicate which intents are compared. The question and alternative questions
of these intents are merged into lists. The first entry of this list is the
question and the following entries are the alternative questions.
These list are being compared then. In this example the question
intent[3].question[1] is similar to the question intent[2].question[1]
:param bot: The id of the bot
:param t: The threshold of for the cosine similarity
:returns: The result of the analysis
"""
# get intents from db
intents = mongo_db_connector.get_intents()
similar_by_answer = intents.similar_by_answer(t)
similar_by_id = intents.similar_by_intent_id(t)
similar_by_question = intents.similar_by_question(t)
return SuccessResponse(SimilarityReport(intents,
similar_by_answer,
similar_by_id,
similar_by_question))
@app.get("/{bot}/intentBalance", summary="Get alternative question balance of"
" the intents.")
async def get_intent_balance():
"""
Run a balance analysis on the bot's intents alternative questions
:returns: The result of the analysis
"""
# get intents from db
intents = mongo_db_connector.get_intents()
avg, under, over = intents.alternative_question_balance_analysis()
return SuccessResponse(BalanceReport(intents, avg, under, over))
@app.post("/intentid", summary="Generate an intent id from a given intent"
......
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