File size: 21,366 Bytes
b2b9de7
 
 
 
 
 
 
2d02136
 
b2b9de7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d02136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2b9de7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d02136
 
b2b9de7
2d02136
 
b2b9de7
 
2d02136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2b9de7
2d02136
b2b9de7
2d02136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2b9de7
2d02136
 
 
 
 
b2b9de7
2d02136
 
 
 
 
 
 
 
 
 
 
 
 
b2b9de7
 
 
2d02136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2b9de7
 
 
2d02136
 
 
b2b9de7
 
 
2d02136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import pandas as pd
import requests
from typing import Tuple, Optional
from dataclasses import dataclass
import logging
from dotenv import load_dotenv
import os
import time
import re

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# .env ํŒŒ์ผ ๋กœ๋“œ
load_dotenv()

# Hugging Face API ์ •๋ณด
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
API_KEY = os.getenv("HUGGINGFACE_API_KEY")

base_path = os.path.dirname(os.path.abspath(__file__))
misconception_csv_path = os.path.join(base_path, 'misconception_mapping.csv')

if not API_KEY:
    raise ValueError("API_KEY๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. .env ํŒŒ์ผ์„ ํ™•์ธํ•˜์„ธ์š”.")

#์œ ์‚ฌ ๋ฌธ์ œ ์ƒ์„ฑ๊ธฐ ํด๋ž˜์Šค

@dataclass
class GeneratedQuestion:
    question: str
    choices: dict
    correct_answer: str
    explanation: str

# class SimilarQuestionGenerator:
#     def __init__(self, misconception_csv_path: str = 'misconception_mapping.csv'):
#         """
#         Initialize the generator by loading the misconception mapping and the language model.
#         """
#         self._load_data(misconception_csv_path)

#     def _load_data(self, misconception_csv_path: str):
#         logger.info("Loading misconception mapping...")
#         self.misconception_df = pd.read_csv(misconception_csv_path)

#     def get_misconception_text(self, misconception_id: float) -> Optional[str]:
#         # MisconceptionId๋ฅผ ๋ฐ›์•„ ํ•ด๋‹น ID์— ๋งค์นญ๋˜๋Š” ์˜ค๊ฐœ๋… ์„ค๋ช… ํ…์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค
#         """Retrieve the misconception text based on the misconception ID."""
#         if pd.isna(misconception_id):  # NaN ์ฒดํฌ
#             logger.warning("Received NaN for misconception_id.")
#             return "No misconception provided."
        
#         try:
#             row = self.misconception_df[self.misconception_df['MisconceptionId'] == int(misconception_id)]
#             if not row.empty:
#                 return row.iloc[0]['MisconceptionName']
#         except ValueError as e:
#             logger.error(f"Error processing misconception_id: {e}")
        
#         logger.warning(f"No misconception found for ID: {misconception_id}")
#         return "Misconception not found."

#     def generate_prompt(self, construct_name: str, subject_name: str, question_text: str, correct_answer_text: str, wrong_answer_text: str, misconception_text: str) -> str:
#         """Create a prompt for the language model."""
#         #๋ฌธ์ œ ์ƒ์„ฑ์„ ์œ„ํ•œ ํ”„๋กฌํ”„ํŠธ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑ
#         logger.info("Generating prompt...")
#         misconception_clause = (f"that targets the following misconception: \"{misconception_text}\"." if misconception_text != "There is no misconception" else "")
#         prompt = f"""
#             <|begin_of_text|>
#             <|start_header_id|>system<|end_header_id|>
#             You are an educational assistant designed to generate multiple-choice questions {misconception_clause}
#             <|eot_id|>
#             <|start_header_id|>user<|end_header_id|>
#             You need to create a similar multiple-choice question based on the following details:

#             Construct Name: {construct_name}
#             Subject Name: {subject_name}
#             Question Text: {question_text}
#             Correct Answer: {correct_answer_text}
#             Wrong Answer: {wrong_answer_text}

#             Please follow this output format:
#             ---
#             Question: <Your Question Text>
#             A) <Choice A>
#             B) <Choice B>
#             C) <Choice C>
#             D) <Choice D>
#             Correct Answer: <Correct Choice (e.g., A)>
#             Explanation: <Brief explanation for the correct answer>
#             ---
#             Ensure that the question is conceptually similar but not identical to the original. Ensure clarity and educational value.
#             <|eot_id|>
#             <|start_header_id|>assistant<|end_header_id|>
#         """.strip()
#         logger.debug(f"Generated prompt: {prompt}")
#         return prompt

#     def call_model_api(self, prompt: str) -> str:
#         """Hugging Face API ํ˜ธ์ถœ"""
#         logger.info("Calling Hugging Face API...")
#         headers = {"Authorization": f"Bearer {API_KEY}"}
        
#         try:
#             response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
#             response.raise_for_status()
            
#             response_data = response.json()
#             logger.debug(f"Raw API response: {response_data}")
            
#             # API ์‘๋‹ต์ด ๋ฆฌ์ŠคํŠธ์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
#             if isinstance(response_data, list):
#                 if response_data and isinstance(response_data[0], dict):
#                     generated_text = response_data[0].get('generated_text', '')
#                 else:
#                     generated_text = response_data[0] if response_data else ''
#             # API ์‘๋‹ต์ด ๋”•์…”๋„ˆ๋ฆฌ์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
#             elif isinstance(response_data, dict):
#                 generated_text = response_data.get('generated_text', '')
#             else:
#                 generated_text = str(response_data)
                
#             logger.info(f"Generated text: {generated_text}")
#             return generated_text
            
#         except requests.exceptions.RequestException as e:
#             logger.error(f"API request failed: {e}")
#             raise
#         except Exception as e:
#             logger.error(f"Unexpected error in call_model_api: {e}")
#             raise
#     def parse_model_output(self, output: str) -> GeneratedQuestion:
#         if not isinstance(output, str):
#             logger.error(f"Invalid output format: {type(output)}. Expected string.")
#             raise ValueError("Model output is not a string.")

#         logger.info(f"Parsing output: {output}")
#         output_lines = output.strip().splitlines()
#         logger.debug(f"Split output into lines: {output_lines}")

#         question, choices, correct_answer, explanation = "", {}, "", ""

#         for line in output_lines:
#             if line.lower().startswith("question:"):
#                 question = line.split(":", 1)[1].strip()
#             elif line.startswith("A)"):
#                 choices["A"] = line[2:].strip()
#             elif line.startswith("B)"):
#                 choices["B"] = line[2:].strip()
#             elif line.startswith("C)"):
#                 choices["C"] = line[2:].strip()
#             elif line.startswith("D)"):
#                 choices["D"] = line[2:].strip()
#             elif line.lower().startswith("correct answer:"):
#                 correct_answer = line.split(":", 1)[1].strip()
#             elif line.lower().startswith("explanation:"):
#                 explanation = line.split(":", 1)[1].strip()

#         if not question or len(choices) < 4 or not correct_answer or not explanation:
#             logger.warning("Incomplete generated question.")
#         return GeneratedQuestion(question, choices, correct_answer, explanation)

#     def generate_similar_question_with_text(self, construct_name: str, subject_name: str, question_text: str, correct_answer_text: str, wrong_answer_text: str, misconception_id: float) -> Tuple[Optional[GeneratedQuestion], Optional[str]]:
#         logger.info("generate_similar_question_with_text initiated")

#         # ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์ถ”๊ฐ€
#         try:
#             misconception_text = self.get_misconception_text(misconception_id)
#             logger.info(f"Misconception text retrieved: {misconception_text}")
#         except Exception as e:
#             logger.error(f"Error retrieving misconception text: {e}")
#             return None, None

#         if not misconception_text:
#             logger.info("Skipping question generation due to lack of misconception.")
#             return None, None

#         prompt = self.generate_prompt(construct_name, subject_name, question_text, correct_answer_text, wrong_answer_text, misconception_text)
#         logger.info(f"Generated prompt: {prompt}")

#         generated_text = None  # ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”
#         try:
#             logger.info("Calling call_model_api...")
#             generated_text = self.call_model_api(prompt)
#             logger.info(f"Generated text from API: {generated_text}")

#             # ํŒŒ์‹ฑ
#             generated_question = self.parse_model_output(generated_text)
#             logger.info(f"Generated question object: {generated_question}")
#             return generated_question, generated_text

#         except Exception as e:
#             logger.error(f"Failed to generate question: {e}")
#             logger.debug(f"API output for debugging: {generated_text}")
#             return None, generated_text


class SimilarQuestionGenerator:
    def __init__(self, misconception_csv_path: str = 'misconception_mapping.csv'):
        """
        Initialize the generator by loading the misconception mapping and the language model.
        """
        self._load_data(misconception_csv_path)

    def _load_data(self, misconception_csv_path: str):
        logger.info("Loading misconception mapping...")
        self.misconception_df = pd.read_csv(misconception_csv_path)

    def get_misconception_text(self, misconception_id: float) -> Optional[str]:
        # MisconceptionId๋ฅผ ๋ฐ›์•„ ํ•ด๋‹น ID์— ๋งค์นญ๋˜๋Š” ์˜ค๊ฐœ๋… ์„ค๋ช… ํ…์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค
        """Retrieve the misconception text based on the misconception ID."""
        if pd.isna(misconception_id):  # NaN ์ฒดํฌ
            logger.warning("Received NaN for misconception_id.")
            return "No misconception provided."
        
        try:
            row = self.misconception_df[self.misconception_df['MisconceptionId'] == int(misconception_id)]
            if not row.empty:
                return row.iloc[0]['MisconceptionName']
        except ValueError as e:
            logger.error(f"Error processing misconception_id: {e}")
        
        logger.warning(f"No misconception found for ID: {misconception_id}")
        return "Misconception not found."

    def generate_prompt(self, construct_name: str, subject_name: str, question_text: str, correct_answer_text: str, wrong_answer_text: str, misconception_text: str) -> str:
        """Create a prompt for the language model."""
        #๋ฌธ์ œ ์ƒ์„ฑ์„ ์œ„ํ•œ ํ”„๋กฌํ”„ํŠธ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑ
        logger.info("Generating prompt...")
        misconception_clause = (f"that targets the following misconception: \"{misconception_text}\"." if misconception_text != "There is no misconception" else "")
        prompt = f"""
            <|begin_of_text|>
            <|start_header_id|>system<|end_header_id|>
            You are an educational assistant designed to generate multiple-choice questions {misconception_clause}
            <|eot_id|>
            <|start_header_id|>user<|end_header_id|>
            You need to create a similar multiple-choice question based on the following details:

            Construct Name: {construct_name}
            Subject Name: {subject_name}
            Question Text: {question_text}
            Correct Answer: {correct_answer_text}
            Wrong Answer: {wrong_answer_text}

            Please follow this output format:
            ---
            Question: <Your Question Text>
            A) <Choice A>
            B) <Choice B>
            C) <Choice C>
            D) <Choice D>
            Correct Answer: <Correct Choice (e.g., A)>
            Explanation: <Brief explanation for the correct answer>
            ---
            Ensure that the question is conceptually similar but not identical to the original. Ensure clarity and educational value.
            <|eot_id|>
            <|start_header_id|>assistant<|end_header_id|>
        """.strip()
        logger.debug(f"Generated prompt: {prompt}")
        return prompt

    def call_model_api(self, prompt: str) -> str:
        """Hugging Face API ํ˜ธ์ถœ"""
        logger.info("Calling Hugging Face API...")
        headers = {"Authorization": f"Bearer {API_KEY}"}
        
        try:
            response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
            response.raise_for_status()
            
            response_data = response.json()
            logger.debug(f"Raw API response: {response_data}")
            
            # API ์‘๋‹ต์ด ๋ฆฌ์ŠคํŠธ์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
            if isinstance(response_data, list):
                if response_data and isinstance(response_data[0], dict):
                    generated_text = response_data[0].get('generated_text', '')
                else:
                    generated_text = response_data[0] if response_data else ''
            # API ์‘๋‹ต์ด ๋”•์…”๋„ˆ๋ฆฌ์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
            elif isinstance(response_data, dict):
                generated_text = response_data.get('generated_text', '')
            else:
                generated_text = str(response_data)
                
            logger.info(f"Generated text: {generated_text}")
            return generated_text
            
        except requests.exceptions.RequestException as e:
            logger.error(f"API request failed: {e}")
            raise
        except Exception as e:
            logger.error(f"Unexpected error in call_model_api: {e}")
            raise
    
            # --- module2.py ์ค‘ ์ผ๋ถ€ ---
    def parse_model_output(self, output: str) -> GeneratedQuestion:
        """Parse the model output with improved extraction of the question components."""
    
        if not isinstance(output, str):
            logger.error(f"Invalid output format: {type(output)}. Expected string.")
            raise ValueError("Model output is not a string")
    
        logger.info("Parsing model output...")
    
        # 1) ์ „์ฒด ํ…์ŠคํŠธ๋ฅผ ์ค„ ๋‹จ์œ„๋กœ ๋‚˜๋ˆ”
        lines = output.splitlines()
        
        # 2) ๋งˆ์ง€๋ง‰์œผ๋กœ ๋“ฑ์žฅํ•˜๋Š” Question~Explanation ๋ธ”๋ก์„ ์ฐพ๊ธฐ ์œ„ํ•œ ์ž„์‹œ ๋ณ€์ˆ˜
        question = ""
        choices = {}
        correct_answer = ""
        explanation = ""
        
        # ์ด ๋ธ”๋ก์„ ์—ฌ๋Ÿฌ ๋ฒˆ ๋งŒ๋‚  ์ˆ˜ ์žˆ์œผ๋‹ˆ, ์ผ๋‹จ ๋ฐœ๊ฒฌํ•  ๋•Œ๋งˆ๋‹ค ์ €์žฅํ•ด๋‘๊ณ  ๋ฎ์–ด์”Œ์šฐ๋Š” ๋ฐฉ์‹.
        # ์ตœ์ข…์ ์œผ๋กœ "๋งˆ์ง€๋ง‰์— ๋ฐœ๊ฒฌ๋œ" Question ๋ธ”๋ก์ด ์•„๋ž˜ ๋ณ€์ˆ˜๋ฅผ ๋ฎ์–ด์“ฐ๊ฒŒ ๋จ
        temp_question = ""
        temp_choices = {}
        temp_correct = ""
        temp_explanation = ""
    
        for line in lines:
            line = line.strip()
            if not line:
                continue

            # Question:
            if line.lower().startswith("question:"):
                # ์ง€๊ธˆ๊นŒ์ง€ ์ €์žฅํ•ด๋‘” ์ด์ „ ๋ธ”๋ก๋“ค์„ ์ตœ์ข… ์ €์žฅ ์˜์—ญ์— ๋ฎ์–ด์”Œ์šด๋‹ค
                if temp_question:
                    question = temp_question
                    choices = temp_choices
                    correct_answer = temp_correct
                    explanation = temp_explanation
                
                # ์ƒˆ ๋ธ”๋ก์„ ์‹œ์ž‘
                temp_question = line.split(":", 1)[1].strip()
                temp_choices = {}
                temp_correct = ""
                temp_explanation = ""
    
            # A) / B) / C) / D)
            elif re.match(r"^[ABCD]\)", line):
                # "A) ์„ ํƒ์ง€ ๋‚ด์šฉ"
                letter = line[0]  # A, B, C, D
                choice_text = line[2:].strip()
                temp_choices[letter] = choice_text
    
            # Correct Answer:
            elif line.lower().startswith("correct answer:"):
                # "Correct Answer: A)" ํ˜•ํƒœ์—์„œ A๋งŒ ์ถ”์ถœ
                ans_part = line.split(":", 1)[1].strip()
                temp_correct = ans_part[0].upper() if ans_part else ""
    
            # Explanation:
            elif line.lower().startswith("explanation:"):
                temp_explanation = line.split(":", 1)[1].strip()
    
        # ๋ฃจํ”„๊ฐ€ ๋๋‚œ ๋’ค, ํ•œ ๋ฒˆ ๋” ์ตœ์‹  ๋ธ”๋ก์„ ์ตœ์ข… ๋ณ€์ˆ˜์— ๋ฐ˜์˜
        if temp_question:
            question = temp_question
            choices = temp_choices
            correct_answer = temp_correct
            explanation = temp_explanation
    
        # ์ด์ œ question, choices, correct_answer, explanation์ด ์ตœ์ข… ํŒŒ์‹ฑ ๊ฒฐ๊ณผ
        logger.debug(f"Parsed components - Question: {question}, Choices: {choices}, "
                     f"Correct Answer: {correct_answer}, Explanation: {explanation}")
        
        return GeneratedQuestion(question, choices, correct_answer, explanation)




        
    def validate_generated_question(self, question: GeneratedQuestion) -> bool:
        """Validate if all components of the generated question are present and valid."""
        logger.info("Validating generated question...")
        
        try:
            # Check if question text exists and is not too short
            if not question.question or len(question.question.strip()) < 10:
                logger.warning("Question text is missing or too short")
                return False
            
            # Check if all four choices exist and are not empty
            required_choices = set(['A', 'B', 'C', 'D'])
            if set(question.choices.keys()) != required_choices:
                logger.warning(f"Missing choices. Found: {set(question.choices.keys())}")
                return False
                
            if not all(choice.strip() for choice in question.choices.values()):
                logger.warning("Empty choice text found")
                return False
            
            # Check if correct answer is valid (should be just A, B, C, or D)
            if not question.correct_answer or question.correct_answer not in required_choices:
                logger.warning(f"Invalid correct answer: {question.correct_answer}")
                return False
            
            # Check if explanation exists and is not too short
            if not question.explanation or len(question.explanation.strip()) < 20:
                logger.warning("Explanation is missing or too short")
                return False
            
            logger.info("Question validation passed")
            return True
            
        except Exception as e:
            logger.error(f"Error during validation: {e}")
            return False

    def generate_similar_question_with_text(self, construct_name: str, subject_name: str, 
                                          question_text: str, correct_answer_text: str, 
                                          wrong_answer_text: str, misconception_id: float, 
                                          max_retries: int = 3) -> Tuple[Optional[GeneratedQuestion], Optional[str]]:
        """Generate a similar question with validation and retry mechanism."""
        logger.info("generate_similar_question_with_text initiated")
    
        # Get misconception text
        try:
            misconception_text = self.get_misconception_text(misconception_id)
            logger.info(f"Misconception text retrieved: {misconception_text}")
            if not misconception_text:
                logger.info("Skipping question generation due to lack of misconception.")
                return None, None
        except Exception as e:
            logger.error(f"Error retrieving misconception text: {e}")
            return None, None
    
        # Generate prompt once since it doesn't change between retries
        prompt = self.generate_prompt(construct_name, subject_name, question_text, 
                                    correct_answer_text, wrong_answer_text, misconception_text)
        
        # Attempt generation with retries
        for attempt in range(max_retries):
            try:
                logger.info(f"Attempt {attempt + 1} of {max_retries}")
                
                # Call API
                generated_text = self.call_model_api(prompt)
                logger.info(f"Generated text from API: {generated_text}")
                
                # Parse output
                generated_question = self.parse_model_output(generated_text)
                
                # Validate the generated question
                if self.validate_generated_question(generated_question):
                    logger.info("Successfully generated valid question")
                    return generated_question, generated_text
                else:
                    logger.warning(f"Generated question failed validation on attempt {attempt + 1}")
                    
                    # If this was the last attempt, return None
                    if attempt == max_retries - 1:
                        logger.error("Max retries reached without generating valid question")
                        return None, generated_text
                    
                    # Add delay between retries to avoid rate limiting
                    time.sleep(2)  # 2 second delay between retries
                    
            except Exception as e:
                logger.error(f"Error during question generation attempt {attempt + 1}: {e}")
                if attempt == max_retries - 1:
                    return None, None
                time.sleep(2)  # Add delay before retry
                
        return None, None