Spaces:
Sleeping
Sleeping
Update src/ThirdModule/module3.py
Browse files- src/ThirdModule/module3.py +70 -100
src/ThirdModule/module3.py
CHANGED
@@ -1,121 +1,91 @@
|
|
1 |
# module3.py
|
2 |
-
import
|
3 |
-
from
|
4 |
-
from typing import Tuple
|
5 |
import logging
|
6 |
-
from
|
7 |
-
import
|
8 |
|
9 |
-
|
10 |
logging.basicConfig(level=logging.INFO)
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
self.tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir=Llama3_8b_PATH, trust_remote_code=True)
|
20 |
-
self.model = AutoModelForCausalLM.from_pretrained(
|
21 |
-
model_name,
|
22 |
-
cache_dir=Llama3_8b_PATH,
|
23 |
-
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
24 |
-
trust_remote_code=True,
|
25 |
-
device_map="auto"
|
26 |
-
)
|
27 |
-
self.model.eval()
|
28 |
-
if torch.cuda.is_available():
|
29 |
-
self.model.to('cuda')
|
30 |
-
logger.info("Model loaded on GPU for self-consistency.")
|
31 |
-
else:
|
32 |
-
logger.info("Model loaded on CPU for self-consistency.")
|
33 |
|
34 |
def _create_prompt(self, question: str, choices: dict) -> str:
|
35 |
-
"""
|
36 |
-
|
37 |
-
"""
|
38 |
-
prompt = f"""
|
39 |
<|begin_of_text|>
|
40 |
<|start_header_id|>system<|end_header_id|>
|
41 |
-
You are an expert
|
42 |
-
|
43 |
-
|
44 |
-
1. Carefully read the question and all options.
|
45 |
-
2. Use logical reasoning to select the best answer.
|
46 |
-
3. Output your answer strictly in the following format: "Answer: [A/B/C/D]"
|
47 |
-
4. Do not provide any explanation or extra information.
|
48 |
-
|
49 |
<|eot_id|>
|
50 |
<|start_header_id|>user<|end_header_id|>
|
51 |
Question: {question}
|
52 |
|
53 |
-
Choices:
|
54 |
A) {choices['A']}
|
55 |
B) {choices['B']}
|
56 |
C) {choices['C']}
|
57 |
D) {choices['D']}
|
58 |
|
59 |
-
|
60 |
<|eot_id|>
|
61 |
<|start_header_id|>assistant<|end_header_id|>
|
62 |
-
"""
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
answer
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
return ""
|
76 |
-
|
77 |
-
def check_answer(self, question: str, choices: dict, num_inferences: int = 10) -> Tuple[str, str]:
|
78 |
-
"""
|
79 |
-
Perform self-consistency check:
|
80 |
-
- Run inference num_inferences times.
|
81 |
-
- Extract answer each time.
|
82 |
-
- Majority vote the final answer.
|
83 |
-
"""
|
84 |
-
|
85 |
-
prompt = self._create_prompt(question, choices) # ์์ ๋ ํ๋กฌํํธ ์์ฑ
|
86 |
-
answer_counts = {"A": 0, "B": 0, "C": 0, "D": 0}
|
87 |
-
|
88 |
-
inputs = self.tokenizer(prompt, return_tensors='pt')
|
89 |
-
if torch.cuda.is_available():
|
90 |
-
inputs = {k: v.to('cuda') for k, v in inputs.items()}
|
91 |
-
|
92 |
-
for _ in range(num_inferences):
|
93 |
-
with torch.no_grad():
|
94 |
-
outputs = self.model.generate(
|
95 |
-
**inputs,
|
96 |
-
max_new_tokens=50,
|
97 |
-
num_return_sequences=1,
|
98 |
-
temperature=0.7,
|
99 |
-
top_p=0.9,
|
100 |
-
do_sample=True,
|
101 |
-
eos_token_id=self.tokenizer.eos_token_id
|
102 |
-
)
|
103 |
-
generated_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
104 |
-
predicted_answer = self._extract_answer(generated_text)
|
105 |
-
|
106 |
-
logger.info(f"Generated text: {generated_text}") # ๋ชจ๋ธ์ด ์์ฑํ ํ
์คํธ ํ์ธ
|
107 |
-
logger.info(f"Predicted answer: {predicted_answer}") # ์ถ์ถ๋ ์ ๋ต ํ์ธ
|
108 |
-
|
109 |
-
if predicted_answer in answer_counts:
|
110 |
-
answer_counts[predicted_answer] += 1
|
111 |
-
else:
|
112 |
-
logger.warning(f"Invalid answer extracted: {predicted_answer}")
|
113 |
-
|
114 |
-
# Majority vote
|
115 |
-
final_answer = max(answer_counts, key=answer_counts.get)
|
116 |
-
explanation = f"Answer counts: {answer_counts}. Majority answer: {final_answer}"
|
117 |
-
|
118 |
-
logger.info(f"Answer counts: {answer_counts}")
|
119 |
-
logger.info(f"Final Answer: {final_answer}")
|
120 |
-
|
121 |
-
return final_answer, explanation
|
|
|
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 |
+
|
19 |
+
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 |
+
prompt = self._create_prompt(question, choices)
|
27 |
+
headers = {"Authorization": f"Bearer {API_KEY}"}
|
28 |
+
|
29 |
+
response = requests.post(
|
30 |
+
API_URL,
|
31 |
+
headers=headers,
|
32 |
+
json={"inputs": prompt}
|
33 |
+
)
|
34 |
+
response.raise_for_status()
|
35 |
+
|
36 |
+
response_data = response.json()
|
37 |
+
logger.debug(f"Raw API response: {response_data}")
|
38 |
+
|
39 |
+
# API ์๋ต ์ฒ๋ฆฌ
|
40 |
+
generated_text = ""
|
41 |
+
if isinstance(response_data, list):
|
42 |
+
if response_data and isinstance(response_data[0], dict):
|
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 |
+
verified_answer = self._extract_answer(generated_text)
|
52 |
+
logger.info(f"Verified answer: {verified_answer}")
|
53 |
+
return verified_answer
|
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 checking student answers.
|
65 |
+
Please analyze the following question and select the single best answer.
|
66 |
+
Output ONLY the letter of the correct answer (A, B, C, or D) without any explanation.
|
|
|
|
|
|
|
|
|
|
|
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 |
+
Select the correct answer letter (A, B, C, or D):
|
77 |
<|eot_id|>
|
78 |
<|start_header_id|>assistant<|end_header_id|>
|
79 |
+
""".strip()
|
80 |
+
|
81 |
+
def _extract_answer(self, response: str) -> Optional[str]:
|
82 |
+
"""์๋ต์์ A, B, C, D ์ค ํ๋๋ฅผ ์ถ์ถ"""
|
83 |
+
response = response.strip().upper()
|
84 |
+
valid_answers = {'A', 'B', 'C', 'D'}
|
85 |
+
|
86 |
+
# ์๋ต์์ ์ ํจํ ๋ต์ ์ฐพ๊ธฐ
|
87 |
+
for answer in valid_answers:
|
88 |
+
if answer in response:
|
89 |
+
return answer
|
90 |
+
|
91 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|