forked from yeyn19/DebugBench-Prototype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_logic.py
194 lines (157 loc) · 7.85 KB
/
main_logic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# 导入PyQt6相关模块和你的Ui_MainWindow类
from PyQt6.QtWidgets import QMainWindow, QApplication, QTableWidgetItem
from PyQt6.QtCore import QTimer
from test import Ui_MainWindow # 确保导入了正确的Ui_MainWindow类
import os
import json
from colorama import Fore, Style
import time
from tqdm import tqdm
def get_problems():
"""获取benchmark中所有的题目
"""
all_data = {}
benchmark_path = "/Users/yeyn/data/git_repos/DebugBench-Prototype/benchmark_data"
for file_name in tqdm(os.listdir(benchmark_path), desc="loading data..."):
if file_name.endswith(".json"):
programming_language = file_name.split("_")[0]
buggy_type = file_name.split("_")[1].split(".")[0]
with open(os.path.join(benchmark_path,file_name)) as reader:
file_problems = json.load(reader)
for problem in file_problems:
problem_name = problem["slug"]
problem_name = problem_name.replace("-"," ")
if problem_name not in all_data.keys():
all_data[problem_name] = {"code": [],
}
for key in ["description", "release_time", "level", "constraints"]:
if key in problem.keys() and key not in all_data[problem_name].keys():
all_data[problem_name][key] = problem[key]
item = {
"oracle_code": problem["oracle_code"],
"buggy_code": problem["buggy_code"],
"examples": problem["examples"],
"buggy_type": buggy_type,
"language": programming_language,
}
if "explanations" in problem.keys():
item["explanations"] = problem["explanations"]
all_data[problem_name]["code"].append(item)
return all_data
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# 初始化界面
self.setupUi(self)
# 在这里添加业务逻辑,例如按钮的点击事件
self.stackedWidget.setCurrentIndex(0)
self.orcale_code.setMinimumWidth(self.orcale_code_scrollArea.viewport().width())
self.orcale_code.setMinimumHeight(self.orcale_code_scrollArea.viewport().height())
self.back_to_problem_set.clicked.connect(self.go_to_problem_set)
self.back_to_problem_detail.clicked.connect(self.go_to_problem_detail)
self.go_to_debug.clicked.connect(self.render_debug)
self.reset.clicked.connect(self.reset_debug)
self.submit.clicked.connect(self.submit_debug)
self.leaderBoard.clicked.connect(self.show_leaderboard)
self.problem_table.cellClicked.connect(self.click_a_problem)
self.all_problems = get_problems()
self.load_problem_set()
self.debugging_start_time = time.time()
self.timer = QTimer()
self.timer.timeout.connect(self.updateTimer)
self.timer.start(1000) # 设置定时器每1000毫秒(1秒)触发一次
def updateTimer(self):
now_time = time.time()
if self.debugging_start_time != None:
self.debug_time.setText(f"Debug...{int(now_time-self.debugging_start_time)}s")
def click_a_problem(self, row, column):
"""点进了题目详情页
"""
print(f"click {row} {column}")
assert row < len(self.row_reflection)
self.last_choosed_problem = self.row_reflection[row]
self.stackedWidget.setCurrentIndex(2)
self.render_problem(self.last_choosed_problem)
def render_problem(self, choosed_problem):
self.problem_head.setText(choosed_problem['name'])
self.hardness.setText(choosed_problem['problem']['level'])
self.language.setText(choosed_problem['code']['language'])
description = choosed_problem["problem"]["description"]
if "examples" in choosed_problem["code"].keys():
if isinstance(choosed_problem["code"]["examples"], str):
description += "\n" + choosed_problem["code"]["examples"]
elif isinstance(choosed_problem["code"]["examples"], list):
description += "\n" + "\n".join(choosed_problem["code"]["examples"])
if "constraints" in choosed_problem["problem"].keys():
description += "\n" + choosed_problem["problem"]["constraints"]
self.problem_description.setText(description)
self.orcale_code.setText(choosed_problem["code"]["oracle_code"])
self.buggy_code.setText(choosed_problem["code"]["buggy_code"])
if "explanations" in choosed_problem["code"].keys():
self.buggy_reason.setText("Buggy Code: "+choosed_problem["code"]["explanations"])
else:
self.buggy_reason.setText("Buggy Code")
def render_debug(self):
assert self.last_choosed_problem != None
self.debugging_start_time = time.time()
self.stackedWidget.setCurrentIndex(1)
# TODO: 在QT里显示代码高亮?
self.buggy_code_instance.setText(self.last_choosed_problem["code"]["buggy_code"])
self.now_code.setPlainText(self.last_choosed_problem["code"]["buggy_code"])
def reset_debug(self):
self.now_code.setPlainText(self.last_choosed_problem["code"]["buggy_code"])
def submit_debug(self):
print("提交代码")
self.stackedWidget.setCurrentIndex(3)
end_time = time.time()
self.submit_info.setText(f"Time Cost: {int(end_time-self.debugging_start_time)}s")
# TODO: check the submitted code
now_code = self.now_code.toPlainText()
print(now_code)
self.submit_result.setText("Waiting for the online judger...")
def load_problem_set(self):
self.problem_table.clearContents()
problem_count = 0
for k, (problem_name, item) in enumerate(self.all_problems.items()):
problem_count += len(item["code"])
self.problem_table.setRowCount(problem_count)
self.row_reflection = []
now_item_position = 0
for k, (problem_name, item) in enumerate(self.all_problems.items()):
hardness = item["level"] if "level" in item.keys() else "Unknown"
if "release_time" in item.keys():
release_date = item["release_time"]
time_struct = time.gmtime(release_date)
release_date = time.strftime('%Y-%m-%d', time_struct)
else:
release_date = "Unknown"
for cont in item["code"]:
self.problem_table.setItem(now_item_position, 0, QTableWidgetItem(problem_name))
self.problem_table.setItem(now_item_position, 1, QTableWidgetItem(hardness))
self.problem_table.setItem(now_item_position, 2, QTableWidgetItem(cont["buggy_type"]))
self.problem_table.setItem(now_item_position, 3, QTableWidgetItem(release_date))
self.problem_table.setItem(now_item_position, 4, QTableWidgetItem(cont["language"]))
self.row_reflection.append({
"name": problem_name,
"problem": item,
"code": cont,
})
now_item_position += 1
def go_to_problem_set(self):
# 这里是按钮点击事件的处理逻辑
print("回到题目列表页")
self.stackedWidget.setCurrentIndex(0)
def go_to_problem_detail(self):
print("回到题目详情页")
self.stackedWidget.setCurrentIndex(2)
self.render_problem(self.last_choosed_problem)
def show_leaderboard(self):
print("显示所有模型Performance")
self.stackedWidget.setCurrentIndex(4)
#TODO 在这里显示一个echart?
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec())