File size: 8,108 Bytes
7ff1fe0
4120946
7ff1fe0
6fa6b83
835f767
 
 
a35428b
7ff1fe0
 
 
 
 
6b07ee4
 
b31816e
 
48c823d
8acf519
48c823d
7ff1fe0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f929e
6b07ee4
007ec3d
 
25fedb7
8acf519
25fedb7
 
8acf519
25fedb7
d7c0eb6
 
 
 
 
 
 
 
25fedb7
 
 
8acf519
25fedb7
fbc5903
 
 
 
25fedb7
 
 
007ec3d
fbc5903
007ec3d
 
1054a05
b7f929e
 
8acf519
fbc5903
b7f929e
8acf519
fbc5903
b7f929e
8acf519
b7f929e
 
 
28a310a
 
48c823d
7b3a151
bf59cf6
6fa6b83
 
 
a35428b
6fa6b83
 
 
 
 
bf59cf6
 
6fa6b83
 
 
 
 
 
a35428b
6fa6b83
 
 
 
 
 
 
 
835f767
 
bf59cf6
6fa6b83
835f767
6fa6b83
 
 
 
a35428b
6fa6b83
 
d55a841
6fa6b83
a35428b
6fa6b83
 
 
bf59cf6
6fa6b83
 
 
 
 
 
 
bf59cf6
6fa6b83
 
 
bf59cf6
6fa6b83
 
 
 
 
bf59cf6
 
6fa6b83
 
bf59cf6
6fa6b83
 
835f767
d55a841
 
19299e4
 
a35428b
d55a841
 
 
 
 
 
 
 
19299e4
d55a841
a35428b
 
d55a841
 
 
 
 
835f767
19299e4
835f767
19299e4
 
 
a35428b
835f767
 
 
 
 
 
 
 
19299e4
835f767
 
 
19299e4
835f767
 
 
 
 
19299e4
a35428b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""FastAPI endpoint
To run locally use 'uvicorn app:app --host localhost --port 7860'
"""
import ast
import scripts.quiz.generators as generators
import scripts.quiz.hints as hints
import scripts.quiz.questions as questions
import scripts.quiz.utils as utils

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from mathtext.sentiment import sentiment
from mathtext.text2int import text2int
from pydantic import BaseModel

from mathtext_fastapi.logging import prepare_message_data_for_logging
from mathtext_fastapi.conversation_manager import manage_conversation_response
from mathtext_fastapi.nlu import evaluate_message_with_nlu

app = FastAPI()

app.mount("/static", StaticFiles(directory="static"), name="static")

templates = Jinja2Templates(directory="templates")


class Text(BaseModel):
    content: str = ""


@app.get("/")
def home(request: Request):
    return templates.TemplateResponse("home.html", {"request": request})


@app.post("/hello")
def hello(content: Text = None):
    content = {"message": f"Hello {content.content}!"}
    return JSONResponse(content=content)


@app.post("/sentiment-analysis")
def sentiment_analysis_ep(content: Text = None):
    ml_response = sentiment(content.content)
    content = {"message": ml_response}
    return JSONResponse(content=content)


@app.post("/text2int")
def text2int_ep(content: Text = None):
    ml_response = text2int(content.content)
    content = {"message": ml_response}
    return JSONResponse(content=content)


@app.post("/manager")
async def programmatic_message_manager(request: Request):
    """
    Calls conversation management function to determine the next state

    Input
    request.body: dict - message data for the most recent user response
    {
        "author_id": "+47897891",
        "contact_uuid": "j43hk26-2hjl-43jk-hnk2-k4ljl46j0ds09",
        "author_type": "OWNER",
        "message_body": "a test message",
        "message_direction": "inbound",
        "message_id": "ABJAK64jlk3-agjkl2QHFAFH",
        "message_inserted_at": "2022-07-05T04:00:34.03352Z",
        "message_updated_at": "2023-02-14T03:54:19.342950Z",
    }

    Output
    context: dict - the information for the current state
    {
        "user": "47897891",
        "state": "welcome-message-state",
        "bot_message": "Welcome to Rori!",
        "user_message": "",
        "type": "ask"
    }
    """
    data_dict = await request.json()
    context = manage_conversation_response(data_dict)
    return JSONResponse(context)


@app.post("/nlu")
async def evaluate_user_message_with_nlu_api(request: Request):
    """ Calls nlu evaluation and returns the nlu_response

    Input
    - request.body: json - message data for the most recent user response

    Output
    - int_data_dict or sent_data_dict: dict - the type of NLU run and result
      {'type':'integer', 'data': '8'}
      {'type':'sentiment', 'data': 'negative'}
    """
    data_dict = await request.json()
    message_data = data_dict.get('message_data', '')
    nlu_response = evaluate_message_with_nlu(message_data)
    return JSONResponse(content=nlu_response)
    

@app.post("/question")
async def ask_math_question(request: Request):
    """Generate a question and return it as response along with question data
    
    Input
    request.body: json - amount of correct and incorrect answers in the account
    {
        'number_correct': 0,
        'number_incorrect': 0,
        'level': 'easy'
    }

    Output
    context: dict - the information for the current state
    {
        'text': 'What is 1+2?',
        'question_numbers': [1,2,3], #3 numbers - current number, ordinal number, times
        'right_answer': 3,
        'number_correct': 0,
        'number_incorrect': 0,
        'hints_used': 0
    }
    """
    data_dict = await request.json()
    message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
    right_answers = message_data['number_correct']
    wrong_answers = message_data['number_incorrect']
    level = message_data['level']

    return JSONResponse(generators.start_interactive_math(right_answers, wrong_answers, level))


@app.post("/hint")
async def get_hint(request: Request):
    """Generate a hint and return it as response along with hint data
    
    Input
    request.body:
    {
        'question_numbers': [1,2,3], #3 numbers - current number, ordinal number, times
        'right_answer': 3,
        'number_correct': 0,
        'number_incorrect': 0,
        'level': 'easy',
        'hints_used': 0
    }

    Output
    context: dict - the information for the current state
    {
        'text': 'What is 1+2?',
        'question_numbers': [1,2,3], #2 or 3 numbers
        'right_answer': 3,
        'number_correct': 0,
        'number_incorrect': 0,
        'level': 'easy',
        'hints_used': 0
    }
    """
    data_dict = await request.json()
    message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
    question_numbers = message_data['question_numbers']
    right_answer = message_data['right_answer']
    number_correct = message_data['number_correct']
    number_incorrect = message_data['number_incorrect']
    level = message_data['level']
    hints_used = message_data['hints_used']

    return JSONResponse(hints.generate_hint(question_numbers, right_answer, number_correct, number_incorrect, level, hints_used))


@app.post("/generate_question")
async def generate_question(request: Request):
    """Generate a bare question and return it as response
    
    Input
    request.body: json - level
    {
        'level': 'easy'
    }

    Output
    context: dict - the information for the current state
    {
        "question": "Let's count up by 2s. What number is next if we start from 10?
        6 8 10 ..."
    }
    """
    data_dict = await request.json()
    message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
    level = message_data['level']

    return JSONResponse(questions.generate_question_data(level)['question'])


@app.post("/numbers_by_level")
async def get_numbers_by_level(request: Request):
    """Generate three numbers and return them as response
    
    Input
    request.body: json - level
    {
        'level': 'easy'
    }

    Output
    context: dict - three generated numbers for specified level
    {
        "current_number": 10,
        "ordinal_number": 2,
        "times": 1
    }
    """
    data_dict = await request.json()
    message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
    level = message_data['level']
    return JSONResponse(questions.generate_numbers_by_level(level))


@app.post("/number_sequence")
async def get_number_sequence(request: Request):
    """Generate a number sequence
    
    Input
    request.body: json - level
    {
        "current_number": 10,
        "ordinal_number": 2,
        "times": 1
    }

    Output
    one of following strings with (numbers differ):
    ... 1 2 3
    1 2 3 ...
    """
    data_dict = await request.json()
    message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
    cur_num = message_data['current_number']
    ord_num = message_data['ordinal_number']
    times = message_data['times']
    return JSONResponse(questions.generate_number_sequence(cur_num, ord_num, times))


@app.post("/level")
async def get_next_level(request: Request):
    """Depending on current level and desire to level up/down return next level
    
    Input
    request.body: json - level
    {
        "current_level": "easy",
        "level_up": True
    }

    Output
    Literal - "easy", "medium" or "hard"
    """
    data_dict = await request.json()
    message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
    cur_level = message_data['current_level']
    level_up = message_data['level_up']
    return JSONResponse(utils.get_next_level(cur_level, level_up))