Spaces:
Sleeping
Sleeping
File size: 3,487 Bytes
7a7b891 633c3f1 b582880 633c3f1 b582880 509c349 633c3f1 1fcbb38 633c3f1 1fcbb38 633c3f1 b582880 5a41f5d b582880 7c7358d b582880 7a7b891 7c7358d 7a7b891 5a41f5d c9dfe5e 07e7924 7c7358d 7a7b891 b582880 509c349 c03acf1 7c7358d 509c349 633c3f1 |
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 |
import base64
import uvicorn
from contextlib import asynccontextmanager
import sqlite3
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from openai import OpenAI
import os
# OpenAI APIクライアントを初期化
client = OpenAI()
client.api_key = os.getenv('OPENAI_API_KEY')
@asynccontextmanager
async def lifespan(app: FastAPI):
print("startup event")
yield
print("shutdown event")
app = FastAPI(lifespan=lifespan)
DATABASE = 'workbook.db'
def get_db():
db = getattr(app.state, '_database', None)
if db is None:
db = app.state._database = sqlite3.connect(DATABASE)
return db
@app.get('/api/chapters')
def get_chapters():
db = get_db()
cursor = db.cursor()
cursor.execute("""
SELECT c.chapter, c.chapter_name, COUNT(q.question) as n_questions
FROM chapter c
LEFT JOIN question q ON c.chapter = q.chapter
GROUP BY c.chapter, c.chapter_name
""")
chapters = cursor.fetchall()
chapter_list = [
{
"chapter_number": row[0],
"chapter_name": row[1],
"n_questions": row[2]
} for row in chapters
]
return JSONResponse(content=chapter_list)
class ChapterRequest(BaseModel):
chapter_number: int
@app.post('/api/questions')
def get_questions_by_chapter(request: ChapterRequest):
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT question, options, answer, explain, image FROM question WHERE chapter = ? AND kind = '正誤問題'", (request.chapter_number,))
questions = cursor.fetchall()
if not questions:
raise HTTPException(status_code=404, detail="Questions not found for the given chapter number")
question_list = []
for row in questions:
if row[4] is None:
encoded_string = ''
else:
image_path = os.path.join('./image', row[4])
with open(image_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
question_list.append({
"question_text": row[0],
"options": row[1],
"answer": row[2],
"explanation": row[3],
"image": encoded_string,
})
return JSONResponse(content=question_list)
class HintRequest(BaseModel):
question_text: str
options: str
answer: str
@app.post('/api/hint')
def generate_hint(request: HintRequest):
prompt = f"設問: {request.question_text}\n選択肢: {request.options}\n正解: {request.answer}\nこの設問に対するヒントを生成してください。なおレスポンスはヒント文章のみとせよ。冒頭に「ヒント:」などの見出しをつけるな。"
print(prompt)
try:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": [{'type': 'text', 'text': prompt}],
},
],
)
response_content = response.choices[0].message.content
return JSONResponse(content={"hint": response_content})
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == '__main__':
uvicorn.run(app, host="127.0.0.1", port=8000, log_level="debug") |