sviddo commited on
Commit
d7a52dd
1 Parent(s): 096aad0

populate app.py module with endpoints

Browse files
Files changed (2) hide show
  1. app.py +84 -133
  2. poetry.lock +0 -0
app.py CHANGED
@@ -15,9 +15,9 @@ from mathtext.sentiment import sentiment
15
  from mathtext.text2int import text2int
16
  from pydantic import BaseModel
17
 
18
- # from mathtext_fastapi.logging import prepare_message_data_for_logging
19
- # from mathtext_fastapi.conversation_manager import manage_conversation_response
20
- # from mathtext_fastapi.nlu import evaluate_message_with_nlu
21
 
22
  app = FastAPI()
23
 
@@ -106,210 +106,161 @@ async def evaluate_user_message_with_nlu_api(request: Request):
106
  return JSONResponse(content=nlu_response)
107
 
108
 
109
- @app.post("/question")
110
  async def ask_math_question(request: Request):
111
- """Generate a question and return it as response along with question data
112
 
113
  Input
114
- request.body: json - amount of correct and incorrect answers in the account
115
  {
116
- 'number_correct': 0,
117
- 'number_incorrect': 0,
118
- 'level': 'easy'
119
  }
120
 
121
  Output
122
- context: dict - the information for the current state
123
  {
124
  'text': 'What is 1+2?',
125
- 'question_numbers': [1,2,3], #3 numbers - current number, ordinal number, times
126
- 'right_answer': 3,
127
- 'number_correct': 0,
128
- 'number_incorrect': 0,
129
- 'hints_used': 0
130
  }
131
  """
132
  data_dict = await request.json()
133
  message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
134
- right_answers = message_data['number_correct']
135
- wrong_answers = message_data['number_incorrect']
136
- level = message_data['level']
137
 
138
- return JSONResponse(generators.start_interactive_math(right_answers, wrong_answers, level))
139
 
140
 
141
  @app.post("/hint")
142
  async def get_hint(request: Request):
143
- """Generate a hint and return it as response along with hint data
144
 
145
  Input
146
- request.body:
147
  {
148
- 'question_numbers': [1,2,3], #3 numbers - current number, ordinal number, times
149
- 'right_answer': 3,
150
- 'number_correct': 0,
151
- 'number_incorrect': 0,
152
- 'level': 'easy',
153
- 'hints_used': 0
154
  }
155
 
156
  Output
157
- context: dict - the information for the current state
158
  {
159
- 'text': 'What is 1+2?',
160
- 'question_numbers': [1,2,3], #2 or 3 numbers
161
- 'right_answer': 3,
162
- 'number_correct': 0,
163
- 'number_incorrect': 0,
164
- 'level': 'easy',
165
- 'hints_used': 0
166
  }
167
  """
168
  data_dict = await request.json()
169
  message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
170
- question_numbers = message_data['question_numbers']
171
- right_answer = message_data['right_answer']
172
- number_correct = message_data['number_correct']
173
- number_incorrect = message_data['number_incorrect']
174
- level = message_data['level']
175
- hints_used = message_data['hints_used']
176
 
177
- return JSONResponse(hints.generate_hint(question_numbers, right_answer, number_correct, number_incorrect, level, hints_used))
178
 
179
 
180
- @app.post("/generate_question")
181
- async def generate_question(request: Request):
182
- """Generate a bare question and return it as response
183
 
184
  Input
185
- request.body: json - level
186
  {
187
- 'level': 'easy'
 
 
188
  }
189
 
190
  Output
191
- context: dict - the information for the current state
192
  {
193
- "question": "Let's count up by 2s. What number is next if we start from 10?
194
- 6 8 10 ..."
 
 
195
  }
196
  """
197
  data_dict = await request.json()
198
  message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
199
- level = message_data['level']
 
 
 
 
 
 
 
200
 
201
- return JSONResponse(questions.generate_question_data(level)['question'])
202
 
203
 
204
- @app.post("/numbers_by_level")
205
- async def get_numbers_by_level(request: Request):
206
- """Generate three numbers and return them as response
207
 
208
  Input
209
- request.body: json - level
210
  {
211
- 'level': 'easy'
 
212
  }
213
 
214
- Output
215
- context: dict - three generated numbers for specified level
216
- {
217
- "current_number": 10,
218
- "ordinal_number": 2,
219
- "times": 1
220
- }
221
  """
222
  data_dict = await request.json()
223
  message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
224
- level = message_data['level']
225
- return JSONResponse(questions.generate_numbers_by_level(level))
226
 
 
227
 
228
- @app.post("/number_sequence")
229
- async def get_number_sequence(request: Request):
230
- """Generate a number sequence
231
-
232
- Input
233
- request.body: json - level
234
- {
235
- "current_number": 10,
236
- "ordinal_number": 2,
237
- "times": 1
238
- }
239
 
240
- Output
241
- one of following strings with (numbers differ):
242
- ... 1 2 3
243
- 1 2 3 ...
244
- """
245
- data_dict = await request.json()
246
- message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
247
- cur_num = message_data['current_number']
248
- ord_num = message_data['ordinal_number']
249
- times = message_data['times']
250
- return JSONResponse(questions.generate_number_sequence(cur_num, ord_num, times))
251
-
252
-
253
- @app.post("/level")
254
- async def get_next_level(request: Request):
255
- """Depending on current level and desire to level up/down return next level
256
 
257
  Input
258
- request.body: json - level
259
  {
260
- "current_level": "easy",
261
- "level_up": True
262
  }
263
 
264
- Output
265
- Literal - "easy", "medium" or "hard"
266
  """
267
  data_dict = await request.json()
268
  message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
269
- cur_level = message_data['current_level']
270
- level_up = message_data['level_up']
271
- return JSONResponse(utils.get_next_level(cur_level, level_up))
 
 
 
 
272
 
 
273
 
274
- @app.post("/score")
275
- async def get_next_level(request: Request):
276
- """Depending on current level and desire to level up/down return next level
277
-
278
- Input
279
- request.body: json - score argument with value from 0 to 1
280
- {
281
- "score": 0.1
282
- }
283
-
284
- Output
285
- number in range 1-495
286
- """
287
- data_dict = await request.json()
288
- message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
289
- score = message_data['score']
290
- return JSONResponse(questions.generate_start_by_score(score))
291
 
292
-
293
- @app.post("/question_new")
294
- async def get_next_level(request: Request):
295
- """Depending on current level and desire to level up/down return next level
296
 
297
  Input
298
- request.body: json - score argument with value from 0 to 1
299
  {
300
- "score": 0.1
 
 
301
  }
302
 
303
  Output
304
- number in range 1-495
305
  """
306
  data_dict = await request.json()
307
  message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
308
- start = message_data.get('start', "")
309
- start = int(start) if start else start
310
- step = message_data.get('step', "")
311
- step = int(step) if step else step
312
- sequence = message_data.get('sequence', "")
313
- question_num = message_data.get('question_num', "")
314
- question_num = int(question_num) if question_num else question_num
315
- return JSONResponse(questions.generate_question(start, step=step, seq=sequence, question_num=question_num))
 
 
 
15
  from mathtext.text2int import text2int
16
  from pydantic import BaseModel
17
 
18
+ from mathtext_fastapi.logging import prepare_message_data_for_logging
19
+ from mathtext_fastapi.conversation_manager import manage_conversation_response
20
+ from mathtext_fastapi.nlu import evaluate_message_with_nlu
21
 
22
  app = FastAPI()
23
 
 
106
  return JSONResponse(content=nlu_response)
107
 
108
 
109
+ @app.post("/start")
110
  async def ask_math_question(request: Request):
111
+ """Generate a question data
112
 
113
  Input
 
114
  {
115
+ 'difficulty': 0.1,
116
+ 'do_increase': True | False
 
117
  }
118
 
119
  Output
 
120
  {
121
  'text': 'What is 1+2?',
122
+ 'difficulty': 0.2,
123
+ 'question_numbers': [3, 1, 4]
 
 
 
124
  }
125
  """
126
  data_dict = await request.json()
127
  message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
128
+ difficulty = message_data['difficulty']
129
+ do_increase = message_data['do_increase']
 
130
 
131
+ return JSONResponse(generators.start_interactive_math(difficulty, do_increase))
132
 
133
 
134
  @app.post("/hint")
135
  async def get_hint(request: Request):
136
+ """Generate a hint data
137
 
138
  Input
 
139
  {
140
+ 'start': 5,
141
+ 'step': 1,
142
+ 'difficulty': 0.1
 
 
 
143
  }
144
 
145
  Output
 
146
  {
147
+ 'text': 'What number is greater than 4 and less than 6?',
148
+ 'difficulty': 0.1,
149
+ 'question_numbers': [5, 1, 6]
 
 
 
 
150
  }
151
  """
152
  data_dict = await request.json()
153
  message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
154
+ start = message_data['start']
155
+ step = message_data['step']
156
+ difficulty = message_data['difficulty']
 
 
 
157
 
158
+ return JSONResponse(hints.generate_hint(start, step, difficulty))
159
 
160
 
161
+ @app.post("/question")
162
+ async def ask_math_question(request: Request):
163
+ """Generate a question data
164
 
165
  Input
 
166
  {
167
+ 'start': 5,
168
+ 'step': 1,
169
+ 'question_num': 1 # optional
170
  }
171
 
172
  Output
 
173
  {
174
+ 'question': 'What is 1+2?',
175
+ 'start': 5,
176
+ 'step': 1,
177
+ 'answer': 6
178
  }
179
  """
180
  data_dict = await request.json()
181
  message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
182
+ start = message_data['start']
183
+ step = message_data['step']
184
+ arg_tuple = (start, step)
185
+ try:
186
+ question_num = message_data['question_num']
187
+ arg_tuple += (question_num,)
188
+ except KeyError:
189
+ pass
190
 
191
+ return JSONResponse(questions.generate_question_data(*arg_tuple))
192
 
193
 
194
+ @app.post("/difficulty")
195
+ async def get_hint(request: Request):
196
+ """Generate a number matching difficulty
197
 
198
  Input
 
199
  {
200
+ 'difficulty': 0.01,
201
+ 'do_increase': True
202
  }
203
 
204
+ Output - value from 0.01 to 0.99 inclusively:
205
+ 0.09
 
 
 
 
 
206
  """
207
  data_dict = await request.json()
208
  message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
209
+ difficulty = message_data['difficulty']
210
+ do_increase = message_data['do_increase']
211
 
212
+ return JSONResponse(utils.get_next_difficulty(difficulty, do_increase))
213
 
 
 
 
 
 
 
 
 
 
 
 
214
 
215
+ @app.post("/start_step")
216
+ async def get_hint(request: Request):
217
+ """Generate a start and step values
 
 
 
 
 
 
 
 
 
 
 
 
 
218
 
219
  Input
 
220
  {
221
+ 'difficulty': 0.01,
222
+ 'path_to_csv_file': 'scripts/quiz/data.csv' # optional
223
  }
224
 
225
+ Output - tuple (start, step):
226
+ (5, 1)
227
  """
228
  data_dict = await request.json()
229
  message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
230
+ difficulty = message_data['difficulty']
231
+ arg_tuple = (difficulty,)
232
+ try:
233
+ path_to_csv_file = message_data['path_to_csv_file']
234
+ arg_tuple += (path_to_csv_file,)
235
+ except KeyError:
236
+ pass
237
 
238
+ return JSONResponse(utils.get_next_difficulty(*arg_tuple))
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
 
241
+ @app.post("/sequence")
242
+ async def generate_question(request: Request):
243
+ """Generate a sequence from start, step and optional separator parameter
 
244
 
245
  Input
 
246
  {
247
+ 'start': 5,
248
+ 'step': 1,
249
+ 'sep': ', ' # optional
250
  }
251
 
252
  Output
253
+ 5, 6, 7
254
  """
255
  data_dict = await request.json()
256
  message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
257
+ start = message_data['start']
258
+ step = message_data['step']
259
+ arg_tuple = (start, step)
260
+ try:
261
+ sep = message_data['sep']
262
+ arg_tuple += (sep,)
263
+ except KeyError:
264
+ pass
265
+
266
+ return JSONResponse(utils.convert_sequence_to_string(*arg_tuple))
poetry.lock ADDED
The diff for this file is too large to render. See raw diff