-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d924658
commit 809bfd4
Showing
9 changed files
with
124 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,33 @@ | ||
import json | ||
|
||
# 載入 aicup_noocr.json 和 aicup_ref.json | ||
with open('data/aicup_noocr.json', 'r', encoding='utf-8') as file: | ||
with open('data/aicup_noocr.json', encoding='utf-8') as file: | ||
noocr_data = json.load(file) | ||
|
||
with open('data/aicup_ref.json', 'r', encoding='utf-8') as file: | ||
with open('data/aicup_ref.json', encoding='utf-8') as file: | ||
ref_data = json.load(file) | ||
|
||
# 建立 ref_data 的 dictionary,並檢查 content 是否為字串,再去除空格 | ||
ref_dict = { | ||
(item["category"], item["pid"]): ''.join(item["content"].split()) if isinstance(item["content"], str) else item["content"] | ||
(item['category'], item['pid']): ''.join(item['content'].split()) | ||
if isinstance(item['content'], str) | ||
else item['content'] | ||
for item in ref_data | ||
} | ||
|
||
# 更新 noocr_data 中空的 content | ||
for item in noocr_data: | ||
category = item["category"] | ||
pid = item["pid"] | ||
content = item["content"] | ||
category = item['category'] | ||
pid = item['pid'] | ||
content = item['content'] | ||
|
||
# 如果 content 是 string 並且為空,則從 ref_data 裡填入去掉空格的 content | ||
if isinstance(content, str) and content == "": | ||
if isinstance(content, str) and content == '': | ||
if (category, pid) in ref_dict: | ||
item["content"] = ref_dict[(category, pid)] | ||
item['content'] = ref_dict[(category, pid)] | ||
|
||
# 將結果寫入 aicup_noocr_sec.json | ||
with open('data/aicup_noocr_sec.json', 'w', encoding='utf-8') as file: | ||
json.dump(noocr_data, file, ensure_ascii=False, indent=4) | ||
|
||
print("已完成比對並生成 aicup_noocr_sec.json,並移除轉入的 content 中的空格(如果 content 是字串)") | ||
print('已完成比對並生成 aicup_noocr_sec.json,並移除轉入的 content 中的空格(如果 content 是字串)') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,49 @@ | ||
import requests | ||
import json | ||
import time # Import time module for timing | ||
|
||
import requests | ||
|
||
# Load questions from the JSON file | ||
with open('data/questions_example.json', 'r', encoding='utf-8') as file: | ||
with open('data/questions_example.json', encoding='utf-8') as file: | ||
questions = json.load(file)['questions'] | ||
|
||
output_data = {"answers": []} # Initialize output format with "answers" array | ||
output_data = {'answers': []} # Initialize output format with "answers" array | ||
|
||
url = "http://127.0.0.1:5000/api/chat" | ||
url = 'http://127.0.0.1:5000/api/chat' | ||
|
||
total_start_time = time.time() # Start timing for the entire process | ||
|
||
for question in questions: | ||
question_start_time = time.time() # Start timing for each question | ||
|
||
# Send POST request | ||
response = requests.post(url, json=question) | ||
|
||
if response.status_code == 200: | ||
response_json = response.json() | ||
|
||
# Extract qid and retrieve from the API response | ||
qid = question.get("qid") # Assuming each question has a unique "qid" field | ||
retrieve = response_json.get("retrieve") | ||
qid = question.get('qid') # Assuming each question has a unique "qid" field | ||
retrieve = response_json.get('retrieve') | ||
|
||
# Append formatted result to the answers array | ||
output_data["answers"].append({ | ||
"qid": qid, | ||
"retrieve": retrieve | ||
}) | ||
print("成功取得 JSON:", response_json) | ||
output_data['answers'].append({'qid': qid, 'retrieve': retrieve}) | ||
print('成功取得 JSON:', response_json) | ||
else: | ||
print("請求失敗,狀態碼:", response.status_code) | ||
print('請求失敗,狀態碼:', response.status_code) | ||
|
||
# Calculate and print time for each question | ||
question_end_time = time.time() | ||
question_duration = question_end_time - question_start_time | ||
print(f"QID: {qid} - 花費時間: {question_duration:.2f} 秒") | ||
print(f'QID: {qid} - 花費時間: {question_duration:.2f} 秒') | ||
|
||
# Calculate and print total time | ||
total_end_time = time.time() | ||
total_duration = total_end_time - total_start_time | ||
print(f"全部題目處理完成,總共花費時間: {total_duration:.2f} 秒") | ||
print(f'全部題目處理完成,總共花費時間: {total_duration:.2f} 秒') | ||
|
||
# Save the output data to a new JSON file | ||
with open('data/pred_retrieve.json', 'w', encoding='utf-8') as output_file: | ||
json.dump(output_data, output_file, ensure_ascii=False, indent=4) | ||
|
||
print("合併輸出已保存到 pred_retrieve.json 文件中。") | ||
print('合併輸出已保存到 pred_retrieve.json 文件中。') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.