Spaces:
Running
Running
Update caller.py
Browse files
caller.py
CHANGED
|
@@ -1,100 +1,101 @@
|
|
| 1 |
-
# Copyright 2024 Bytedance Ltd. and/or its affiliates
|
| 2 |
-
#
|
| 3 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
-
# you may not use this file except in compliance with the License.
|
| 5 |
-
# You may obtain a copy of the License at
|
| 6 |
-
#
|
| 7 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
-
#
|
| 9 |
-
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
-
# See the License for the specific language governing permissions and
|
| 13 |
-
# limitations under the License.
|
| 14 |
-
|
| 15 |
-
import regex as re
|
| 16 |
-
from typing import Dict, List
|
| 17 |
-
import json
|
| 18 |
-
from mathruler.grader import extract_boxed_content, grade_answer
|
| 19 |
-
import os
|
| 20 |
-
import time
|
| 21 |
-
import random
|
| 22 |
-
import requests
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
for i in range(4)
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
| 1 |
+
# Copyright 2024 Bytedance Ltd. and/or its affiliates
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
import regex as re
|
| 16 |
+
from typing import Dict, List
|
| 17 |
+
import json
|
| 18 |
+
from mathruler.grader import extract_boxed_content, grade_answer
|
| 19 |
+
import os
|
| 20 |
+
import time
|
| 21 |
+
import random
|
| 22 |
+
import requests
|
| 23 |
+
import app_math as app_math
|
| 24 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 25 |
+
|
| 26 |
+
STORAGE_PATH = os.getenv("STORAGE_PATH")
|
| 27 |
+
|
| 28 |
+
def generate_temp_filename(prefix="temp", suffix=".json"):
|
| 29 |
+
timestamp = int(time.time() * 1000)
|
| 30 |
+
rand_part = random.randint(0, 99999)
|
| 31 |
+
return f"{STORAGE_PATH}/temp_results/{prefix}_{timestamp}_{rand_part}{suffix}"
|
| 32 |
+
def split_list(lst, n=4):
|
| 33 |
+
k, m = divmod(len(lst), n)
|
| 34 |
+
return [lst[i*k + min(i, m):(i+1)*k + min(i+1, m)] for i in range(n)]
|
| 35 |
+
|
| 36 |
+
os.environ["NO_PROXY"] = "0.0.0.0,127.0.0.1"
|
| 37 |
+
|
| 38 |
+
def fetch(index,i):
|
| 39 |
+
response = requests.get(f"http://0.0.0.0:{5000+index}/hello?name={i}")
|
| 40 |
+
print(response)
|
| 41 |
+
return True
|
| 42 |
+
|
| 43 |
+
def generate_results(data):
|
| 44 |
+
datas = split_list(data,4)
|
| 45 |
+
random_names = [generate_temp_filename(prefix=f"temp_{i}", suffix=".json") for i in range(4)]
|
| 46 |
+
for i in range(4):
|
| 47 |
+
with open(random_names[i],'w') as f:
|
| 48 |
+
json.dump(datas[i],f,indent=4)
|
| 49 |
+
|
| 50 |
+
final_results = []
|
| 51 |
+
with ThreadPoolExecutor(max_workers=4) as executor:
|
| 52 |
+
futures = [executor.submit(fetch, i,random_names[i]) for i in range(4)]
|
| 53 |
+
|
| 54 |
+
for future in as_completed(futures):
|
| 55 |
+
print(future.result())
|
| 56 |
+
|
| 57 |
+
for future in as_completed(futures):
|
| 58 |
+
with open(random_names[i].replace('.json','_results.json'),'r') as f:
|
| 59 |
+
final_results.extend(json.load(f))
|
| 60 |
+
|
| 61 |
+
return final_results
|
| 62 |
+
|
| 63 |
+
def format_reward(predict: str) -> float:
|
| 64 |
+
pattern = re.compile(r"<think>.*</think>.*\\boxed\{.*\}.*", re.DOTALL)
|
| 65 |
+
format_match = re.fullmatch(pattern, predict)
|
| 66 |
+
return 1.0 if format_match else 0.0
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def accuracy_reward(predict: str, ground_truth: str) -> float:
|
| 70 |
+
answer = extract_boxed_content(predict)
|
| 71 |
+
return 1.0 if grade_answer(answer, ground_truth) else 0.0
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def compute_score(predicts: List[str], ground_truths: List[str], format_weight: float = 0.1, file_path: str = "") -> List[Dict[str, float]]:
|
| 75 |
+
results = []
|
| 76 |
+
with open('test.json','w') as f:
|
| 77 |
+
json.dump(predicts,f,indent=4)
|
| 78 |
+
for i in range(len(predicts)):
|
| 79 |
+
questions = re.findall(r"<question>(.*?)</question>", predicts[i], re.DOTALL)
|
| 80 |
+
answers = extract_boxed_content(predicts[i])
|
| 81 |
+
if questions and answers:
|
| 82 |
+
try:
|
| 83 |
+
question = questions[-1].strip()
|
| 84 |
+
answer = answers[-1].strip()
|
| 85 |
+
results.append({"question": question, "answer": answer})
|
| 86 |
+
except:
|
| 87 |
+
results.append({"question": "", "answer": ""})
|
| 88 |
+
else:
|
| 89 |
+
results.append({"question": "", "answer": ""})
|
| 90 |
+
|
| 91 |
+
final_results = generate_results(results)
|
| 92 |
+
scores = [{"overall": min(item["score"],1-item["score"]) if item['question'] else -1,"format": 1 if item['question'] else 0,"accuracy": 1 if item['answer'] else 0} for item in final_results]
|
| 93 |
+
return scores
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|