Spaces:
Runtime error
Runtime error
Update src/ThirdModule/module3.py
Browse files- src/ThirdModule/module3.py +125 -45
src/ThirdModule/module3.py
CHANGED
|
@@ -1,18 +1,21 @@
|
|
| 1 |
-
# module3.py
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
-
from typing import Optional
|
| 4 |
import logging
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
# Set up logging
|
| 9 |
logging.basicConfig(level=logging.INFO)
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
-
# .env
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
-
# Hugging Face API
|
| 16 |
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
|
| 17 |
API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
| 18 |
|
|
@@ -20,72 +23,149 @@ if not API_KEY:
|
|
| 20 |
raise ValueError("API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. .env ํ์ผ์ ํ์ธํ์ธ์.")
|
| 21 |
|
| 22 |
class AnswerVerifier:
|
| 23 |
-
def verify_answer(self, question: str, choices: dict) -> Optional[str]:
|
| 24 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
generated_text = response_data[0].get('generated_text', '')
|
| 44 |
-
else:
|
| 45 |
-
generated_text = response_data[0] if response_data else ''
|
| 46 |
-
elif isinstance(response_data, dict):
|
| 47 |
-
generated_text = response_data.get('generated_text', '')
|
| 48 |
-
else:
|
| 49 |
-
generated_text = str(response_data)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
logger.error(f"Error in verify_answer: {e}")
|
| 57 |
-
return None
|
| 58 |
|
| 59 |
def _create_prompt(self, question: str, choices: dict) -> str:
|
| 60 |
-
"""
|
| 61 |
return f"""
|
| 62 |
<|begin_of_text|>
|
| 63 |
<|start_header_id|>system<|end_header_id|>
|
| 64 |
-
You are an expert mathematics teacher
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
<|eot_id|>
|
| 68 |
<|start_header_id|>user<|end_header_id|>
|
| 69 |
Question: {question}
|
| 70 |
-
|
|
|
|
| 71 |
A) {choices['A']}
|
| 72 |
B) {choices['B']}
|
| 73 |
C) {choices['C']}
|
| 74 |
D) {choices['D']}
|
| 75 |
-
|
| 76 |
-
|
| 77 |
<|eot_id|>
|
| 78 |
<|start_header_id|>assistant<|end_header_id|>
|
| 79 |
""".strip()
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
def _extract_answer(self, response: str) -> Optional[str]:
|
| 82 |
-
"""
|
| 83 |
response = response.strip().upper()
|
| 84 |
-
valid_answers = {'A', 'B', 'C', 'D'}
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
return
|
|
|
|
| 1 |
+
# # module3.py
|
| 2 |
+
|
| 3 |
+
import re
|
| 4 |
import requests
|
| 5 |
+
from typing import Optional, Tuple
|
| 6 |
import logging
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
import os
|
| 9 |
+
from collections import Counter
|
| 10 |
|
| 11 |
# Set up logging
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
+
# Load .env file
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
+
# Hugging Face API information
|
| 19 |
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
|
| 20 |
API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
| 21 |
|
|
|
|
| 23 |
raise ValueError("API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. .env ํ์ผ์ ํ์ธํ์ธ์.")
|
| 24 |
|
| 25 |
class AnswerVerifier:
|
| 26 |
+
def verify_answer(self, question: str, choices: dict, num_checks: int = 5) -> Tuple[Optional[str], str]:
|
| 27 |
+
"""
|
| 28 |
+
Self-consistency approach๋ฅผ ํ์ฉํ ๋ต๋ณ ๊ฒ์ฆ
|
| 29 |
+
num_checks: ๋์ผ ์ง๋ฌธ์ ๋ํด ๋ฐ๋ณต ๊ฒ์ฆํ ํ์
|
| 30 |
+
๋ฐํ๊ฐ: (๊ฒ์ฆ๋ ๋ต์, ์ค๋ช
) ํํ
|
| 31 |
+
"""
|
| 32 |
try:
|
| 33 |
+
answers = []
|
| 34 |
+
for i, _ in enumerate(range(num_checks)):
|
| 35 |
+
prompt = self._create_prompt(question, choices)
|
| 36 |
+
headers = {"Authorization": f"Bearer {API_KEY}"}
|
| 37 |
+
|
| 38 |
+
response = requests.post(
|
| 39 |
+
API_URL,
|
| 40 |
+
headers=headers,
|
| 41 |
+
json={"inputs": prompt}
|
| 42 |
+
)
|
| 43 |
+
response.raise_for_status()
|
| 44 |
+
|
| 45 |
+
response_data = response.json()
|
| 46 |
+
logger.debug(f"Raw API response: {response_data}")
|
| 47 |
+
|
| 48 |
+
# API ์๋ต ์ฒ๋ฆฌ
|
| 49 |
+
generated_text = self._process_response(response_data)
|
| 50 |
+
|
| 51 |
+
logger.debug(f"Trial {i+1}:")
|
| 52 |
+
logger.debug(f"Generated text: {generated_text}")
|
| 53 |
|
| 54 |
+
answer = self._extract_answer(generated_text)
|
| 55 |
+
|
| 56 |
+
logger.debug(f"Extracted answer: {answer}")
|
| 57 |
+
|
| 58 |
+
if answer:
|
| 59 |
+
answers.append(answer)
|
| 60 |
|
| 61 |
+
if not answers:
|
| 62 |
+
return None, "No valid answers extracted"
|
| 63 |
|
| 64 |
+
# # ๋ค์๊ฒฐ ํฌํ๋ก ์ต์ข
๋ต์ ๊ฒฐ์
|
| 65 |
+
# final_answer, explanation = self._get_majority_vote(answers)
|
| 66 |
+
# logger.info(f"Final verified answer: {final_answer} ({explanation})")
|
| 67 |
+
# return final_answer, explanation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# Return only the final answer instead of a tuple
|
| 70 |
+
final_answer, explanation = self._get_majority_vote(answers)
|
| 71 |
+
logger.info(f"Final verified answer: {final_answer} ({explanation})")
|
| 72 |
+
return final_answer # ๊ธฐ์กด: return final_answer, explanation
|
| 73 |
+
|
| 74 |
|
| 75 |
except Exception as e:
|
| 76 |
logger.error(f"Error in verify_answer: {e}")
|
| 77 |
+
return None, f"Error occurred: {str(e)}"
|
| 78 |
|
| 79 |
def _create_prompt(self, question: str, choices: dict) -> str:
|
| 80 |
+
"""๊ฐ์ ๋ ํ๋กฌํํธ - ๋ ๋ช
ํํ ์๋ต ํ์ ์๊ตฌ"""
|
| 81 |
return f"""
|
| 82 |
<|begin_of_text|>
|
| 83 |
<|start_header_id|>system<|end_header_id|>
|
| 84 |
+
You are an expert mathematics teacher evaluating multiple-choice answers.
|
| 85 |
+
Analyze the question and options carefully to select the correct answer.
|
| 86 |
+
|
| 87 |
+
IMPORTANT: You must respond ONLY with "Answer: X" where X is A, B, C, or D.
|
| 88 |
+
Do not include any explanation or additional text.
|
| 89 |
<|eot_id|>
|
| 90 |
<|start_header_id|>user<|end_header_id|>
|
| 91 |
Question: {question}
|
| 92 |
+
|
| 93 |
+
Options:
|
| 94 |
A) {choices['A']}
|
| 95 |
B) {choices['B']}
|
| 96 |
C) {choices['C']}
|
| 97 |
D) {choices['D']}
|
| 98 |
+
|
| 99 |
+
Provide your answer in the format: "Answer: X" (where X is A, B, C, or D)
|
| 100 |
<|eot_id|>
|
| 101 |
<|start_header_id|>assistant<|end_header_id|>
|
| 102 |
""".strip()
|
| 103 |
|
| 104 |
+
def _process_response(self, response_data) -> str:
|
| 105 |
+
"""API ์๋ต ๋ฐ์ดํฐ ์ฒ๋ฆฌ - ๊ฐ์ ๋ ๋ฒ์ """
|
| 106 |
+
generated_text = ""
|
| 107 |
+
|
| 108 |
+
if isinstance(response_data, list):
|
| 109 |
+
if response_data and isinstance(response_data[0], dict):
|
| 110 |
+
generated_text = response_data[0].get('generated_text', '')
|
| 111 |
+
else:
|
| 112 |
+
generated_text = response_data[0] if response_data else ''
|
| 113 |
+
elif isinstance(response_data, dict):
|
| 114 |
+
generated_text = response_data.get('generated_text', '')
|
| 115 |
+
else:
|
| 116 |
+
generated_text = str(response_data)
|
| 117 |
+
|
| 118 |
+
# assistant ์๋ต ๋ถ๋ถ๋ง ์ถ์ถ
|
| 119 |
+
parts = generated_text.split('<|start_header_id|>assistant<|end_header_id|>')
|
| 120 |
+
if len(parts) > 1:
|
| 121 |
+
return parts[-1].strip()
|
| 122 |
+
return generated_text.strip()
|
| 123 |
+
|
| 124 |
+
|
| 125 |
def _extract_answer(self, response: str) -> Optional[str]:
|
| 126 |
+
"""๊ฐ์ ๋ ๋ต์ ์ถ์ถ ๋ก์ง"""
|
| 127 |
response = response.strip().upper()
|
|
|
|
| 128 |
|
| 129 |
+
# 1. "ANSWER: X" ํ์ ์ฐพ๊ธฐ
|
| 130 |
+
answer_pattern = r'(?:ANSWER:|CORRECT ANSWER:)\s*([ABCD])'
|
| 131 |
+
answer_match = re.search(answer_pattern, response)
|
| 132 |
+
if answer_match:
|
| 133 |
+
return answer_match.group(1)
|
| 134 |
+
|
| 135 |
+
# 2. ๋จ๋
์ผ๋ก ์๋ A, B, C, D ์ฐพ๊ธฐ
|
| 136 |
+
standalone_pattern = r'\b([ABCD])\b'
|
| 137 |
+
matches = re.findall(standalone_pattern, response)
|
| 138 |
+
|
| 139 |
+
# ๋ง์ง๋ง์ ์๋ ๋ต์ ๋ฐํ (์ผ๋ฐ์ ์ผ๋ก ์ต์ข
๋ต์์ด ๋ง์ง๋ง์ ์์น)
|
| 140 |
+
if matches:
|
| 141 |
+
return matches[-1]
|
| 142 |
+
|
| 143 |
+
return None
|
| 144 |
+
|
| 145 |
+
def _get_majority_vote(self, answers: list) -> Tuple[str, str]:
|
| 146 |
+
"""๊ฐ์ ๋ ๋ค์๊ฒฐ ํฌํ ์์คํ
"""
|
| 147 |
+
if not answers:
|
| 148 |
+
return None, "No valid answers extracted"
|
| 149 |
+
|
| 150 |
+
counter = Counter(answers)
|
| 151 |
+
|
| 152 |
+
# ๋์ ์ธ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
|
| 153 |
+
max_count = max(counter.values())
|
| 154 |
+
top_answers = [ans for ans, count in counter.items() if count == max_count]
|
| 155 |
+
|
| 156 |
+
if len(top_answers) > 1:
|
| 157 |
+
return None, f"Tie between answers: {top_answers}"
|
| 158 |
+
|
| 159 |
+
final_answer = counter.most_common(1)[0][0]
|
| 160 |
+
total_votes = len(answers)
|
| 161 |
+
confidence = (counter[final_answer] / total_votes) * 100
|
| 162 |
+
|
| 163 |
+
# ์ ๋ขฐ๋ ์๊ณ๊ฐ ์ค์
|
| 164 |
+
if confidence < 60:
|
| 165 |
+
return None, f"Low confidence ({confidence:.1f}%) for answer {final_answer}"
|
| 166 |
+
|
| 167 |
+
explanation = (f"Answer '{final_answer}' selected with {confidence:.1f}% confidence "
|
| 168 |
+
f"({counter[final_answer]}/{total_votes} votes). "
|
| 169 |
+
f"Distribution: {dict(counter)}")
|
| 170 |
|
| 171 |
+
return final_answer, explanation
|