icebreaking / ice_breaking_challenge /background_task.py
dongwook-chan's picture
Update
7ff0925
raw
history blame
1.31 kB
from flask import session
import time
import random
from ice_breaking_challenge import model, quiz_generated, quiz_results
def background_task(sid, qna):
"""Session์— ์ €์žฅ๋œ ์‘๋‹ต์„ ๋ฐ”ํƒ•์œผ๋กœ Gemma ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ ํ€ด์ฆˆ ์ƒ์„ฑ"""
quiz_generated[sid] = False
generated_quizzes = generate_quiz(qna)
quiz_results[sid] = generated_quizzes
# ํ€ด์ฆˆ ์ƒ์„ฑ ์™„๋ฃŒ
quiz_generated[sid] = True
def generate_quiz(data):
global model
generated_quizzes = []
selected_data = random.sample(data, min(10, len(data)))
template_input="""
<instruction>
Using the text: {question} {answer}, create a new multiple-choice question with 4 answer options.
"""
for row in selected_data:
question, answer, name = row
input_text = template_input.format(question=question, answer=answer)
response = model.generate(input_text, max_length=512)
lines = response.split('\n')
index = lines.index('<response>')
question_generated = lines[index+1]
multiple_choice_generated = lines[index + 2]
answer_generated = lines[index + 3]
generated_quizzes.append([question_generated, multiple_choice_generated, answer_generated])
return generated_quizzes