Spaces:
Sleeping
Sleeping
Delete module2.py
Browse files- module2.py +0 -196
module2.py
DELETED
@@ -1,196 +0,0 @@
|
|
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
|
31 |
-
choices: dict
|
32 |
-
correct_answer: 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|