Spaces:
Sleeping
Sleeping
Upload workbook_server.py
Browse files- workbook_server.py +10 -6
workbook_server.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import uvicorn
|
2 |
from contextlib import asynccontextmanager
|
3 |
import sqlite3
|
@@ -54,19 +55,22 @@ class ChapterRequest(BaseModel):
|
|
54 |
def get_questions_by_chapter(request: ChapterRequest):
|
55 |
db = get_db()
|
56 |
cursor = db.cursor()
|
57 |
-
cursor.execute("SELECT question, options, answer, explain
|
58 |
questions = cursor.fetchall()
|
59 |
if not questions:
|
60 |
raise HTTPException(status_code=404, detail="Questions not found for the given chapter number")
|
61 |
-
question_list = [
|
62 |
-
|
|
|
|
|
|
|
|
|
63 |
"question_text": row[0],
|
64 |
"options": row[1],
|
65 |
"answer": row[2],
|
66 |
"explanation": row[3],
|
67 |
-
"image":
|
68 |
-
}
|
69 |
-
]
|
70 |
return JSONResponse(content=question_list)
|
71 |
|
72 |
class HintRequest(BaseModel):
|
|
|
1 |
+
import base64
|
2 |
import uvicorn
|
3 |
from contextlib import asynccontextmanager
|
4 |
import sqlite3
|
|
|
55 |
def get_questions_by_chapter(request: ChapterRequest):
|
56 |
db = get_db()
|
57 |
cursor = db.cursor()
|
58 |
+
cursor.execute("SELECT question, options, answer, explain FROM question WHERE chapter = ? AND kind = '正誤問題'", (request.chapter_number,))
|
59 |
questions = cursor.fetchall()
|
60 |
if not questions:
|
61 |
raise HTTPException(status_code=404, detail="Questions not found for the given chapter number")
|
62 |
+
question_list = []
|
63 |
+
for row in questions:
|
64 |
+
image_path = os.path.join('./image', row[4])
|
65 |
+
with open(image_path, "rb") as image_file:
|
66 |
+
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
|
67 |
+
question_list.append({
|
68 |
"question_text": row[0],
|
69 |
"options": row[1],
|
70 |
"answer": row[2],
|
71 |
"explanation": row[3],
|
72 |
+
# "image": encoded_string,
|
73 |
+
})
|
|
|
74 |
return JSONResponse(content=question_list)
|
75 |
|
76 |
class HintRequest(BaseModel):
|