Spaces:
Runtime error
Runtime error
update app with more endpoints
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
"""FastAPI endpoint
|
2 |
To run locally use 'uvicorn app:app --host localhost --port 7860'
|
3 |
"""
|
|
|
4 |
import re
|
5 |
|
6 |
from fastapi import FastAPI, Request
|
@@ -14,6 +15,8 @@ from pydantic import BaseModel
|
|
14 |
from mathtext_fastapi.logging import prepare_message_data_for_logging
|
15 |
from mathtext_fastapi.conversation_manager import manage_conversation_response
|
16 |
from mathtext_fastapi.nlu import evaluate_message_with_nlu
|
|
|
|
|
17 |
|
18 |
app = FastAPI()
|
19 |
|
@@ -100,3 +103,70 @@ async def evaluate_user_message_with_nlu_api(request: Request):
|
|
100 |
message_data = data_dict.get('message_data', '')
|
101 |
nlu_response = evaluate_message_with_nlu(message_data)
|
102 |
return JSONResponse(content=nlu_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
"""FastAPI endpoint
|
2 |
To run locally use 'uvicorn app:app --host localhost --port 7860'
|
3 |
"""
|
4 |
+
import ast
|
5 |
import re
|
6 |
|
7 |
from fastapi import FastAPI, Request
|
|
|
15 |
from mathtext_fastapi.logging import prepare_message_data_for_logging
|
16 |
from mathtext_fastapi.conversation_manager import manage_conversation_response
|
17 |
from mathtext_fastapi.nlu import evaluate_message_with_nlu
|
18 |
+
from scripts.quiz.generators import start_interactive_math
|
19 |
+
from scripts.quiz.hints import generate_hint
|
20 |
|
21 |
app = FastAPI()
|
22 |
|
|
|
103 |
message_data = data_dict.get('message_data', '')
|
104 |
nlu_response = evaluate_message_with_nlu(message_data)
|
105 |
return JSONResponse(content=nlu_response)
|
106 |
+
|
107 |
+
|
108 |
+
@app.post("/question")
|
109 |
+
async def ask_math_question(request: Request):
|
110 |
+
"""Generates a question and returns it as response along with question data
|
111 |
+
|
112 |
+
Input
|
113 |
+
request.body: json - amount of correct and incorrect answers in the account
|
114 |
+
{
|
115 |
+
'number_correct': 0,
|
116 |
+
'number_incorrect': 0
|
117 |
+
}
|
118 |
+
|
119 |
+
Output
|
120 |
+
context: dict - the information for the current state
|
121 |
+
{
|
122 |
+
'text': 'What is 1+2?',
|
123 |
+
'question_numbers': [1,2,3,4], #3 or 4 numbers
|
124 |
+
'right_answer': 3,
|
125 |
+
'number_correct': 0,
|
126 |
+
'number_incorrect': 0,
|
127 |
+
'hints_used': 0
|
128 |
+
}
|
129 |
+
"""
|
130 |
+
data_dict = await request.json()
|
131 |
+
message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
|
132 |
+
number_correct = message_data['number_correct']
|
133 |
+
number_incorrect = message_data['number_incorrect']
|
134 |
+
|
135 |
+
return JSONResponse(start_interactive_math(number_correct, number_incorrect))
|
136 |
+
|
137 |
+
|
138 |
+
@app.post("/hint")
|
139 |
+
async def get_hint(request: Request):
|
140 |
+
"""Generates a hint and returns it as response along with hint data
|
141 |
+
|
142 |
+
Input
|
143 |
+
request.body: json - amount of correct and incorrect answers in the account
|
144 |
+
{
|
145 |
+
'question_numbers': [1,2,3,4], # 3 or 4 numbers
|
146 |
+
'right_answer': 3,
|
147 |
+
'user_answer': 10,
|
148 |
+
'number_correct': 0,
|
149 |
+
'number_incorrect': 0,
|
150 |
+
'hints_used': 0
|
151 |
+
}
|
152 |
+
|
153 |
+
Output
|
154 |
+
context: dict - the information for the current state
|
155 |
+
{
|
156 |
+
'text': 'What is 1+2?',
|
157 |
+
'question_numbers': [1,2,3], #3 or 4 numbers
|
158 |
+
'right_answer': 3,
|
159 |
+
'number_correct': 0,
|
160 |
+
'number_incorrect': 0,
|
161 |
+
'hints_used': 0
|
162 |
+
}
|
163 |
+
"""
|
164 |
+
data_dict = await request.json()
|
165 |
+
message_data = ast.literal_eval(data_dict.get('message_data', '').get('message_body', ''))
|
166 |
+
question_numbers = message_data['number_correct']
|
167 |
+
question_numbers = message_data['number_correct']
|
168 |
+
number_correct = message_data['number_correct']
|
169 |
+
number_incorrect = message_data['number_incorrect']
|
170 |
+
hints_used = message_data['hints_used']
|
171 |
+
|
172 |
+
return JSONResponse(generate_hint(question_numbers, number_correct, number_incorrect, hints_used))
|