sviddo commited on
Commit
0e9529c
1 Parent(s): a1f8134

latest update

Browse files
Files changed (3) hide show
  1. app.py +47 -3
  2. scripts/make_request.py +35 -1
  3. scripts/quiz/questions.py +67 -8
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
 
@@ -269,3 +269,47 @@ async def get_next_level(request: Request):
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))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
 
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))
scripts/make_request.py CHANGED
@@ -57,7 +57,41 @@ def run_simulated_request(endpoint, sample_answer, context=None):
57
  # run_simulated_request('nlu', 'Today is a wonderful day')
58
  # run_simulated_request('nlu', 'IDK 5?')
59
  # run_simulated_request('manager', '')
60
- run_simulated_request('manager', 'add')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  # run_simulated_request('manager', 'subtract')
62
  # run_simulated_request('manager', 'exit')
63
 
 
57
  # run_simulated_request('nlu', 'Today is a wonderful day')
58
  # run_simulated_request('nlu', 'IDK 5?')
59
  # run_simulated_request('manager', '')
60
+ # run_simulated_request('manager', 'add')
61
+ # run_simulated_request("question", {
62
+ # 'number_correct': 0,
63
+ # 'number_incorrect': 0,
64
+ # 'level': 'easy'
65
+ # })
66
+ # run_simulated_request("hint", {
67
+ # 'question_numbers': [1,2,3],
68
+ # 'right_answer': 3,
69
+ # 'number_correct': 0,
70
+ # 'number_incorrect': 0,
71
+ # 'level': 'easy',
72
+ # 'hints_used': 0
73
+ # })
74
+ # run_simulated_request("generate_question", {
75
+ # 'level': 'medium'
76
+ # })
77
+ # run_simulated_request("numbers_by_level", {
78
+ # 'level': 'medium'
79
+ # })
80
+ # run_simulated_request("number_sequence", {
81
+ # "current_number": 10,
82
+ # "ordinal_number": 2,
83
+ # "times": 1
84
+ # })
85
+ # run_simulated_request("level", {
86
+ # "current_level": "hard",
87
+ # "level_up": False
88
+ # })
89
+ run_simulated_request("question_new", {
90
+ "start": 10,
91
+ "step": 1,
92
+ "sequence": "8... 9... 10...",
93
+ "question_num": 1
94
+ })
95
  # run_simulated_request('manager', 'subtract')
96
  # run_simulated_request('manager', 'exit')
97
 
scripts/quiz/questions.py CHANGED
@@ -2,24 +2,24 @@ import random
2
  from typing import Literal
3
 
4
 
5
- def generate_question_data(level: Literal["easy", "medium", "hard"] = "easy"):
6
  """generate question, its numbers and proper answer"""
7
 
8
  nums = generate_numbers_by_level(level)
9
- cur_num = nums['current_number'] # current number
10
- ord_num = nums['ordinal_number'] # ordinal number
11
- seq_up_by_one = generate_number_sequence(cur_num, ord_num=1, times=1) # sequence with ord_num = 1, times = 1
12
 
13
  count_up_by_one_questions = [
14
  {
15
- "question": f"Let's practice counting. After {cur_num}, what number is next?\n{seq_up_by_one}",
16
  "current_number": cur_num,
17
  "ordinal_number": 1,
18
  "times": 1,
19
  "answer": cur_num + 1
20
  }
21
  ]
22
- seq_up_by_ord = generate_number_sequence(cur_num, ord_num, times=1) # sequence with times = 1
23
  count_up_by_ord_questions = [
24
  {
25
  "question": f"What number comes {ord_num} number after {cur_num}?\n{seq_up_by_ord}",
@@ -47,7 +47,7 @@ def generate_question_data(level: Literal["easy", "medium", "hard"] = "easy"):
47
  "answer": cur_num + ord_num * times
48
  }
49
  ]
50
- times_only_seq = generate_number_sequence(cur_num, 1, times) # sequence with ordinal number = 1
51
  times_only_questions = [
52
  {
53
  "question": f"Let's count up by {times}s. What number is next if we start from {cur_num}?\n{times_only_seq}",
@@ -57,11 +57,70 @@ def generate_question_data(level: Literal["easy", "medium", "hard"] = "easy"):
57
  "answer": cur_num + times
58
  }
59
  ]
 
 
 
60
  questions = [*count_up_by_one_questions, *count_up_by_ord_questions, *times_only_questions, *times_ord_questions]
61
  random_choice = random.choice(questions)
62
  return random_choice
63
 
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def generate_numbers_by_level(level: Literal["easy", "medium", "hard"] = "easy"):
66
  """generate current number, ordinal number and times parameter
67
 
@@ -72,7 +131,7 @@ def generate_numbers_by_level(level: Literal["easy", "medium", "hard"] = "easy")
72
  :param times: the number of times we count up by ordinal number"""
73
 
74
  if level == "easy":
75
- cur_num = random.randint(1, 8)
76
  ord_num = random.randint(1, 2)
77
  times = 1
78
  elif level == "medium":
 
2
  from typing import Literal
3
 
4
 
5
+ def generate_question_data(level: Literal["easy", "medium", "hard"] = "easy", increment=1):
6
  """generate question, its numbers and proper answer"""
7
 
8
  nums = generate_numbers_by_level(level)
9
+ cur_num = nums['current_number'] # current number
10
+ ord_num = nums['ordinal_number'] # ordinal number
11
+ num_seq = generate_number_sequence(cur_num, ord_num, times=increment) # sequence with ord_num = 1, times = 1
12
 
13
  count_up_by_one_questions = [
14
  {
15
+ "question": f"Let's practice counting. After {cur_num}, what number is next?\n{num_seq}",
16
  "current_number": cur_num,
17
  "ordinal_number": 1,
18
  "times": 1,
19
  "answer": cur_num + 1
20
  }
21
  ]
22
+ seq_up_by_ord = generate_number_sequence(cur_num, ord_num, times=1) # sequence with times = 1
23
  count_up_by_ord_questions = [
24
  {
25
  "question": f"What number comes {ord_num} number after {cur_num}?\n{seq_up_by_ord}",
 
47
  "answer": cur_num + ord_num * times
48
  }
49
  ]
50
+ times_only_seq = generate_number_sequence(cur_num, 1, times) # sequence with ordinal number = 1
51
  times_only_questions = [
52
  {
53
  "question": f"Let's count up by {times}s. What number is next if we start from {cur_num}?\n{times_only_seq}",
 
57
  "answer": cur_num + times
58
  }
59
  ]
60
+ print(f"Let's practice counting {'... '.join([str(num) for num in range(8, 11)])}... After 10, what is the next number you will count?")
61
+ # Let's practice counting 8... 9... 10... After 10, what is the next number you will count?
62
+ 8, 9, 10
63
  questions = [*count_up_by_one_questions, *count_up_by_ord_questions, *times_only_questions, *times_ord_questions]
64
  random_choice = random.choice(questions)
65
  return random_choice
66
 
67
 
68
+ def generate_question(start, step=None, seq=None, question_num=1):
69
+ """returns question by provided number with filled parameters
70
+
71
+ parameters
72
+ ----------
73
+ :num: question number
74
+ :start: current number
75
+ :step: interval between current and next numbers
76
+ :seq: sequence of numbers"""
77
+ convert_sequence_to_string(start, step, )
78
+ questions = {
79
+ 1: f"Let's practice counting {seq} After {start}, what is the next number you will count?",
80
+ 2: f"What number comes {step} number after {start}?",
81
+ 3: f"We're counting by {step}s. What number is 1 after {start}?",
82
+ 4: f"What is {step} number up from {start}?",
83
+ 5: f"If we could up {step} from {start}, what number is next?",
84
+ }
85
+ if step > 1:
86
+ questions.update({
87
+ 6: f"Now we're counting up by {step} 😄 From {start - 2 * step}, I count {step} numbers to get to {start - step}. Then from {start - step} I count {step} numbers to get to {start}. What is {step} numbers after {start}?",
88
+ 7: f"What is {step} numbers after {start}? Let's count one number ... {start + 1}... now what is the next number?"
89
+ })
90
+ return questions[question_num]
91
+
92
+
93
+ def generate_start_by_score(score):
94
+ """generate number by scrore
95
+
96
+ to calculate the starting point """
97
+ if score <= 0.3:
98
+ range_size = 27 - 1 # total range size
99
+ range_offset = score # offset from the lower end of the range
100
+ proportion = range_offset / 0.3 # proportion of the total range
101
+ number_range = int(proportion * range_size) # size of the range based on the argument
102
+ return random.randint(1, 1 + number_range)
103
+ elif score >= 0.6:
104
+ range_size = 495 - 97 # total range size
105
+ range_offset = score - 0.6 # offset from the lower end of the range
106
+ proportion = range_offset / (1 - 0.6) # proportion of the total range
107
+ number_range = int(proportion * range_size) # size of the range based on the argument
108
+ return random.randint(98, 98 + number_range)
109
+ else:
110
+ range_size = 97 - 28 # total range size
111
+ range_offset = score - 0.3 # offset from the lower end of the range
112
+ proportion = range_offset / (0.6 - 0.3) # proportion of the total range
113
+ number_range = int(proportion * range_size) # size of the range based on the argument
114
+ return random.randint(28, 28 + number_range)
115
+
116
+
117
+ def convert_sequence_to_string(start, step):
118
+ func_vars = {}
119
+ exec("seq = ', '.join([str(num) for num in range({start}, {stop}, {step})][::-1])".format(start=start, stop=start-3*step, step=step * (-1)), func_vars)
120
+ return func_vars['seq']
121
+
122
+
123
+
124
  def generate_numbers_by_level(level: Literal["easy", "medium", "hard"] = "easy"):
125
  """generate current number, ordinal number and times parameter
126
 
 
131
  :param times: the number of times we count up by ordinal number"""
132
 
133
  if level == "easy":
134
+ cur_num = random.randint(5, 15)
135
  ord_num = random.randint(1, 2)
136
  times = 1
137
  elif level == "medium":