dayuian commited on
Commit
1b7a426
·
verified ·
1 Parent(s): 4eddfdb

Create quiz.py

Browse files
Files changed (1) hide show
  1. quiz.py +27 -0
quiz.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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} 分"