dayuian commited on
Commit
0bc32ef
·
verified ·
1 Parent(s): eea8700

Update quiz.py

Browse files
Files changed (1) hide show
  1. quiz.py +52 -21
quiz.py CHANGED
@@ -1,27 +1,58 @@
1
- from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
2
 
3
- from ai_sentence import load_model
4
 
5
- # 生成選擇題
6
- def generate_mcq(word, model_name):
7
- tokenizer, model = load_model(model_name)
8
 
9
- prompt = f"Write a simple multiple-choice English question for beginners using the word '{word}'. Provide 4 options labeled A, B, C, D, and mark the correct answer."
10
- inputs = tokenizer(prompt, return_tensors="pt")
11
- outputs = model.generate(
12
- **inputs,
13
- max_new_tokens=50,
14
- temperature=0.7,
15
- top_p=0.9
16
- )
17
- question = tokenizer.decode(outputs[0], skip_special_tokens=True)
18
 
19
- return question
 
 
 
20
 
21
- # 對答案(未來補)
22
- def check_answer(user_answer, correct_answer):
23
- return user_answer == correct_answer
24
 
25
- # 計算分數(未來補)
26
- def calculate_score(total, correct):
27
- return f"{correct}/{total} 分"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ from vocab import get_words_from_source
3
+ from sentences import get_sentence
4
 
5
+ # 生成單字選擇題
6
 
7
+ def generate_fill_in_blank_exam(source, num):
8
+ words_data = get_words_from_source(source)
9
+ words = random.sample(words_data, num)
10
 
11
+ questions = []
12
+ for word_data in words:
13
+ word = word_data['word']
14
+ phonetic = word_data['phonetic']
 
 
 
 
 
15
 
16
+ # 取得例句
17
+ sentence_data = get_sentence(word)
18
+ if not sentence_data:
19
+ continue # 沒例句的跳過
20
 
21
+ sentence = sentence_data[2]
22
+ blank_sentence = sentence.replace(word, '______')
 
23
 
24
+ # 生成干擾選項 (亂數抽 3 個其他單字)
25
+ other_words = [w['word'] for w in words_data if w['word'] != word]
26
+ distractors = random.sample(other_words, 3)
27
+ options = [word] + distractors
28
+ random.shuffle(options)
29
+
30
+ questions.append({
31
+ "sentence": blank_sentence,
32
+ "options": options,
33
+ "answer": word,
34
+ "phonetic": phonetic
35
+ })
36
+
37
+ return questions
38
+
39
+
40
+ # 自動對答案並計分
41
+ def check_exam(user_answers, questions):
42
+ correct_count = 0
43
+ results = []
44
+
45
+ for i, user_answer in enumerate(user_answers):
46
+ correct_answer = questions[i]['answer']
47
+ is_correct = (user_answer == correct_answer)
48
+ results.append({
49
+ "question": questions[i]['sentence'],
50
+ "user_answer": user_answer,
51
+ "correct_answer": correct_answer,
52
+ "is_correct": is_correct
53
+ })
54
+ if is_correct:
55
+ correct_count += 1
56
+
57
+ score = f"{correct_count}/{len(questions)} 分"
58
+ return score, results