Spaces:
Build error
Build error
Update module2.py
Browse files- module2.py +37 -0
module2.py
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import logging
|
| 3 |
+
from typing import Optional, Tuple
|
| 4 |
+
from dataclasses import dataclass
|
| 5 |
+
|
| 6 |
+
logger = logging.getLogger(__name__)
|
| 7 |
+
|
| 8 |
+
@dataclass
|
| 9 |
+
class GeneratedQuestion:
|
| 10 |
+
question: str
|
| 11 |
+
choices: dict
|
| 12 |
+
correct_answer: str
|
| 13 |
+
explanation: str
|
| 14 |
+
|
| 15 |
+
class SimilarQuestionGenerator:
|
| 16 |
+
def __init__(self, misconception_csv_path: str):
|
| 17 |
+
self._load_data(misconception_csv_path)
|
| 18 |
+
|
| 19 |
+
def _load_data(self, misconception_csv_path: str):
|
| 20 |
+
self.misconception_df = pd.read_csv(misconception_csv_path)
|
| 21 |
+
|
| 22 |
+
def get_misconception_text(self, misconception_id: float) -> Optional[str]:
|
| 23 |
+
if pd.isna(misconception_id):
|
| 24 |
+
return "No misconception provided."
|
| 25 |
+
row = self.misconception_df[self.misconception_df['MisconceptionId'] == int(misconception_id)]
|
| 26 |
+
return row.iloc[0]['MisconceptionName'] if not row.empty else "Misconception not found."
|
| 27 |
+
|
| 28 |
+
def generate_similar_question_with_text(self, construct_name, subject_name, question_text, correct_answer_text, wrong_answer_text, misconception_id) -> Tuple[Optional[GeneratedQuestion], Optional[str]]:
|
| 29 |
+
prompt = f"Generate a similar question for: {question_text}"
|
| 30 |
+
# Mock API call for demonstration
|
| 31 |
+
return GeneratedQuestion(question="Sample Question", choices={"A": "Option A", "B": "Option B"}, correct_answer="A", explanation="Sample Explanation"), None
|
| 32 |
+
|
| 33 |
+
def generate_similar_question(wrong_q, misconception_id, generator):
|
| 34 |
+
if not isinstance(wrong_q, dict):
|
| 35 |
+
return None
|
| 36 |
+
misconception_text = generator.get_misconception_text(misconception_id)
|
| 37 |
+
return {"question": f"Generated Question targeting {misconception_text}"}
|