Spaces:
Build error
Build error
Update module2.py
Browse files- module2.py +176 -17
module2.py
CHANGED
|
@@ -1,10 +1,30 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
-
import
|
| 3 |
-
from typing import
|
| 4 |
from dataclasses import dataclass
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
| 6 |
logger = logging.getLogger(__name__)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
@dataclass
|
| 9 |
class GeneratedQuestion:
|
| 10 |
question: str
|
|
@@ -13,25 +33,164 @@ class GeneratedQuestion:
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
return "No misconception provided."
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
+
import requests
|
| 3 |
+
from typing import Tuple, Optional
|
| 4 |
from dataclasses import dataclass
|
| 5 |
+
import logging
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
+
# Set up logging
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
+
# .env ํ์ผ ๋ก๋
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
# Hugging Face API ์ ๋ณด
|
| 17 |
+
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
|
| 18 |
+
API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
| 19 |
+
|
| 20 |
+
base_path = os.path.dirname(os.path.abspath(__file__))
|
| 21 |
+
misconception_csv_path = os.path.join(base_path, 'misconception_mapping.csv')
|
| 22 |
+
|
| 23 |
+
if not API_KEY:
|
| 24 |
+
raise ValueError("API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. .env ํ์ผ์ ํ์ธํ์ธ์.")
|
| 25 |
+
|
| 26 |
+
#์ ์ฌ ๋ฌธ์ ์์ฑ๊ธฐ ํด๋์ค
|
| 27 |
+
|
| 28 |
@dataclass
|
| 29 |
class GeneratedQuestion:
|
| 30 |
question: str
|
|
|
|
| 33 |
explanation: str
|
| 34 |
|
| 35 |
class SimilarQuestionGenerator:
|
| 36 |
+
def __init__(self, misconception_csv_path: str = 'misconception_mapping.csv'):
|
| 37 |
+
"""
|
| 38 |
+
Initialize the generator by loading the misconception mapping and the language model.
|
| 39 |
+
"""
|
| 40 |
self._load_data(misconception_csv_path)
|
| 41 |
|
| 42 |
def _load_data(self, misconception_csv_path: str):
|
| 43 |
+
logger.info("Loading misconception mapping...")
|
| 44 |
self.misconception_df = pd.read_csv(misconception_csv_path)
|
| 45 |
|
| 46 |
def get_misconception_text(self, misconception_id: float) -> Optional[str]:
|
| 47 |
+
# MisconceptionId๋ฅผ ๋ฐ์ ํด๋น ID์ ๋งค์นญ๋๋ ์ค๊ฐ๋
์ค๋ช
ํ
์คํธ๋ฅผ ๋ฐํํฉ๋๋ค
|
| 48 |
+
"""Retrieve the misconception text based on the misconception ID."""
|
| 49 |
+
if pd.isna(misconception_id): # NaN ์ฒดํฌ
|
| 50 |
+
logger.warning("Received NaN for misconception_id.")
|
| 51 |
return "No misconception provided."
|
| 52 |
+
|
| 53 |
+
try:
|
| 54 |
+
row = self.misconception_df[self.misconception_df['MisconceptionId'] == int(misconception_id)]
|
| 55 |
+
if not row.empty:
|
| 56 |
+
return row.iloc[0]['MisconceptionName']
|
| 57 |
+
except ValueError as e:
|
| 58 |
+
logger.error(f"Error processing misconception_id: {e}")
|
| 59 |
+
|
| 60 |
+
logger.warning(f"No misconception found for ID: {misconception_id}")
|
| 61 |
+
return "Misconception not found."
|
| 62 |
+
|
| 63 |
+
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:
|
| 64 |
+
"""Create a prompt for the language model."""
|
| 65 |
+
#๋ฌธ์ ์์ฑ์ ์ํ ํ๋กฌํํธ ํ
์คํธ๋ฅผ ์์ฑ
|
| 66 |
+
logger.info("Generating prompt...")
|
| 67 |
+
misconception_clause = (f"that targets the following misconception: \"{misconception_text}\"." if misconception_text != "There is no misconception" else "")
|
| 68 |
+
prompt = f"""
|
| 69 |
+
<|begin_of_text|>
|
| 70 |
+
<|start_header_id|>system<|end_header_id|>
|
| 71 |
+
You are an educational assistant designed to generate multiple-choice questions {misconception_clause}
|
| 72 |
+
<|eot_id|>
|
| 73 |
+
<|start_header_id|>user<|end_header_id|>
|
| 74 |
+
You need to create a similar multiple-choice question based on the following details:
|
| 75 |
+
|
| 76 |
+
Construct Name: {construct_name}
|
| 77 |
+
Subject Name: {subject_name}
|
| 78 |
+
Question Text: {question_text}
|
| 79 |
+
Correct Answer: {correct_answer_text}
|
| 80 |
+
Wrong Answer: {wrong_answer_text}
|
| 81 |
+
|
| 82 |
+
Please follow this output format:
|
| 83 |
+
---
|
| 84 |
+
Question: <Your Question Text>
|
| 85 |
+
A) <Choice A>
|
| 86 |
+
B) <Choice B>
|
| 87 |
+
C) <Choice C>
|
| 88 |
+
D) <Choice D>
|
| 89 |
+
Correct Answer: <Correct Choice (e.g., A)>
|
| 90 |
+
Explanation: <Brief explanation for the correct answer>
|
| 91 |
+
---
|
| 92 |
+
Ensure that the question is conceptually similar but not identical to the original. Ensure clarity and educational value.
|
| 93 |
+
<|eot_id|>
|
| 94 |
+
<|start_header_id|>assistant<|end_header_id|>
|
| 95 |
+
""".strip()
|
| 96 |
+
logger.debug(f"Generated prompt: {prompt}")
|
| 97 |
+
return prompt
|
| 98 |
+
|
| 99 |
+
def call_model_api(self, prompt: str) -> str:
|
| 100 |
+
"""Hugging Face API ํธ์ถ"""
|
| 101 |
+
logger.info("Calling Hugging Face API...")
|
| 102 |
+
headers = {"Authorization": f"Bearer {API_KEY}"}
|
| 103 |
+
|
| 104 |
+
try:
|
| 105 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
|
| 106 |
+
response.raise_for_status()
|
| 107 |
+
|
| 108 |
+
response_data = response.json()
|
| 109 |
+
logger.debug(f"Raw API response: {response_data}")
|
| 110 |
+
|
| 111 |
+
# API ์๋ต์ด ๋ฆฌ์คํธ์ธ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
|
| 112 |
+
if isinstance(response_data, list):
|
| 113 |
+
if response_data and isinstance(response_data[0], dict):
|
| 114 |
+
generated_text = response_data[0].get('generated_text', '')
|
| 115 |
+
else:
|
| 116 |
+
generated_text = response_data[0] if response_data else ''
|
| 117 |
+
# API ์๋ต์ด ๋์
๋๋ฆฌ์ธ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
|
| 118 |
+
elif isinstance(response_data, dict):
|
| 119 |
+
generated_text = response_data.get('generated_text', '')
|
| 120 |
+
else:
|
| 121 |
+
generated_text = str(response_data)
|
| 122 |
+
|
| 123 |
+
logger.info(f"Generated text: {generated_text}")
|
| 124 |
+
return generated_text
|
| 125 |
+
|
| 126 |
+
except requests.exceptions.RequestException as e:
|
| 127 |
+
logger.error(f"API request failed: {e}")
|
| 128 |
+
raise
|
| 129 |
+
except Exception as e:
|
| 130 |
+
logger.error(f"Unexpected error in call_model_api: {e}")
|
| 131 |
+
raise
|
| 132 |
+
def parse_model_output(self, output: str) -> GeneratedQuestion:
|
| 133 |
+
if not isinstance(output, str):
|
| 134 |
+
logger.error(f"Invalid output format: {type(output)}. Expected string.")
|
| 135 |
+
raise ValueError("Model output is not a string.")
|
| 136 |
+
|
| 137 |
+
logger.info(f"Parsing output: {output}")
|
| 138 |
+
output_lines = output.strip().splitlines()
|
| 139 |
+
logger.debug(f"Split output into lines: {output_lines}")
|
| 140 |
+
|
| 141 |
+
question, choices, correct_answer, explanation = "", {}, "", ""
|
| 142 |
+
|
| 143 |
+
for line in output_lines:
|
| 144 |
+
if line.lower().startswith("question:"):
|
| 145 |
+
question = line.split(":", 1)[1].strip()
|
| 146 |
+
elif line.startswith("A)"):
|
| 147 |
+
choices["A"] = line[2:].strip()
|
| 148 |
+
elif line.startswith("B)"):
|
| 149 |
+
choices["B"] = line[2:].strip()
|
| 150 |
+
elif line.startswith("C)"):
|
| 151 |
+
choices["C"] = line[2:].strip()
|
| 152 |
+
elif line.startswith("D)"):
|
| 153 |
+
choices["D"] = line[2:].strip()
|
| 154 |
+
elif line.lower().startswith("correct answer:"):
|
| 155 |
+
correct_answer = line.split(":", 1)[1].strip()
|
| 156 |
+
elif line.lower().startswith("explanation:"):
|
| 157 |
+
explanation = line.split(":", 1)[1].strip()
|
| 158 |
+
|
| 159 |
+
if not question or len(choices) < 4 or not correct_answer or not explanation:
|
| 160 |
+
logger.warning("Incomplete generated question.")
|
| 161 |
+
return GeneratedQuestion(question, choices, correct_answer, explanation)
|
| 162 |
+
|
| 163 |
+
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]]:
|
| 164 |
+
logger.info("generate_similar_question_with_text initiated")
|
| 165 |
+
|
| 166 |
+
# ์์ธ ์ฒ๋ฆฌ ์ถ๊ฐ
|
| 167 |
+
try:
|
| 168 |
+
misconception_text = self.get_misconception_text(misconception_id)
|
| 169 |
+
logger.info(f"Misconception text retrieved: {misconception_text}")
|
| 170 |
+
except Exception as e:
|
| 171 |
+
logger.error(f"Error retrieving misconception text: {e}")
|
| 172 |
+
return None, None
|
| 173 |
+
|
| 174 |
+
if not misconception_text:
|
| 175 |
+
logger.info("Skipping question generation due to lack of misconception.")
|
| 176 |
+
return None, None
|
| 177 |
+
|
| 178 |
+
prompt = self.generate_prompt(construct_name, subject_name, question_text, correct_answer_text, wrong_answer_text, misconception_text)
|
| 179 |
+
logger.info(f"Generated prompt: {prompt}")
|
| 180 |
+
|
| 181 |
+
generated_text = None # ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ด๊ธฐํ
|
| 182 |
+
try:
|
| 183 |
+
logger.info("Calling call_model_api...")
|
| 184 |
+
generated_text = self.call_model_api(prompt)
|
| 185 |
+
logger.info(f"Generated text from API: {generated_text}")
|
| 186 |
+
|
| 187 |
+
# ํ์ฑ
|
| 188 |
+
generated_question = self.parse_model_output(generated_text)
|
| 189 |
+
logger.info(f"Generated question object: {generated_question}")
|
| 190 |
+
return generated_question, generated_text
|
| 191 |
+
|
| 192 |
+
except Exception as e:
|
| 193 |
+
logger.error(f"Failed to generate question: {e}")
|
| 194 |
+
logger.debug(f"API output for debugging: {generated_text}")
|
| 195 |
+
return None, generated_text
|
| 196 |
+
|