-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathrun_demo.py
More file actions
209 lines (162 loc) · 6.9 KB
/
run_demo.py
File metadata and controls
209 lines (162 loc) · 6.9 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import os
import subprocess
from datetime import datetime
import time
import re
def create_directory(path):
if not os.path.exists(path):
os.makedirs(path)
print(f"Created directory: {path}")
return True
return False
def extract_token_usage(log_content):
"""从日志内容中提取token使用信息"""
token_info = {
'outline_input': 0,
'outline_output': 0,
'subsection_input': 0,
'subsection_output': 0
}
patterns = {
'outline_input': r'OutlineWriter Input token usage: (\d+)',
'outline_output': r'OutlineWriter Output token usage: (\d+)',
'subsection_input': r'SubsectionWriter Input token usage: (\d+)',
'subsection_output': r'SubsectionWriter Output token usage: (\d+)'
}
for key, pattern in patterns.items():
match = re.search(pattern, log_content)
if match:
token_info[key] = int(match.group(1))
return token_info
def run_experiment(topic, exp_num, base_path):
save_path = os.path.join(base_path, topic, f"exp_{exp_num}")
if os.path.exists(save_path):
print(f"\nSkipping experiment {exp_num} for topic {topic} - directory already exists: {save_path}")
return True
create_directory(save_path)
cmd = [
"python", "main.py",
"--topic", topic,
"--gpu", "0",
"--saving_path", save_path,
"--model", "gpt-4o-mini",# "claude-3-5-sonnet-20241022"
"--section_num", "7",
"--subsection_len", "500",
"--rag_num", "100",
"--rag_max_out", "60",
"--outline_reference_num", "1500",
"--survey_outline_path", "./",
"--db_path", "./database",
"--embedding_model", "./gte-large-en-v1.5",
"--api_key", "",
"--api_url", "https://api.openai.com/v1/chat/completions"
]
exp_start_time = datetime.now()
try:
print(f"\nRunning experiment {exp_num} for topic: {topic}")
print(f"Saving to: {save_path}")
print(f"Start time: {exp_start_time.strftime('%Y-%m-%d %H:%M:%S')}")
# Capture the output of the process
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
bufsize=1
)
full_output = []
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
print(output.strip())
full_output.append(output)
return_code = process.poll()
exp_end_time = datetime.now()
duration = exp_end_time - exp_start_time
if return_code != 0:
raise subprocess.CalledProcessError(return_code, cmd)
token_info = extract_token_usage(''.join(full_output))
# Log experiment times
log_file = os.path.join(base_path, "experiment_times.log")
with open(log_file, "a") as f:
f.write(f"Topic: {topic}, Exp {exp_num}\n")
f.write(f"Start: {exp_start_time.strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write(f"End: {exp_end_time.strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write(f"Duration: {duration}\n")
f.write(f"OutlineWriter Input token usage: {token_info['outline_input']}\n")
f.write(f"OutlineWriter Output token usage: {token_info['outline_output']}\n")
f.write(f"SubsectionWriter Input token usage: {token_info['subsection_input']}\n")
f.write(f"SubsectionWriter Output token usage: {token_info['subsection_output']}\n")
f.write("-" * 50 + "\n")
print(f"Experiment {exp_num} for {topic} completed successfully")
print(f"End time: {exp_end_time.strftime('%Y-%m-%d %H:%M:%S')}")
print(f"Duration: {duration}")
return True
except subprocess.CalledProcessError as e:
exp_end_time = datetime.now()
duration = exp_end_time - exp_start_time
error_message = f"Error in experiment {exp_num} for {topic}: {e}"
print(error_message)
print(f"Failed experiment duration: {duration}")
log_file = os.path.join(base_path, "experiment_times.log")
with open(log_file, "a") as f:
f.write(f"Topic: {topic}, Exp {exp_num} [FAILED]\n")
f.write(f"Start: {exp_start_time.strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write(f"End: {exp_end_time.strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write(f"Duration: {duration}\n")
f.write(f"Error: {str(e)}\n")
f.write("-" * 50 + "\n")
return False
def main():
base_path = "./output/res"
create_directory(base_path)
# Loading topics
with open("topics_demo.txt", "r") as f:
topics = [line.strip() for line in f if line.strip()]
start_time = datetime.now()
print(f"Starting experiments at: {start_time}")
log_file = os.path.join(base_path, "experiment_times.log")
log_exists = os.path.exists(log_file)
with open(log_file, "a" if log_exists else "w") as f:
if not log_exists:
f.write(f"Experiment Start Time: {start_time.strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write("=" * 50 + "\n\n")
else:
f.write(f"\nResuming experiments at: {start_time.strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write("=" * 50 + "\n\n")
for topic in topics:
print(f"\n{'='*50}")
print(f"Starting experiments for topic: {topic}")
print(f"{'='*50}")
topic_start_time = datetime.now()
successful_exps = 0
for exp_num in range(1, 2):
success = run_experiment(topic, exp_num, base_path)
if success:
successful_exps += 1
time.sleep(5)
topic_end_time = datetime.now()
topic_duration = topic_end_time - topic_start_time
with open(log_file, "a") as f:
f.write(f"\nTopic Summary: {topic}\n")
f.write(f"Total Duration: {topic_duration}\n")
f.write(f"Successful Experiments: {successful_exps}/10\n")
f.write("=" * 50 + "\n")
end_time = datetime.now()
duration = end_time - start_time
with open(log_file, "a") as f:
f.write(f"\nFinal Summary\n")
f.write("=" * 50 + "\n")
f.write(f"Total Start Time: {start_time.strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write(f"Total End Time: {end_time.strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write(f"Total Duration: {duration}\n")
f.write(f"Total Topics: {len(topics)}\n")
print(f"\nAll experiments completed!")
print(f"Start time: {start_time}")
print(f"End time: {end_time}")
print(f"Total duration: {duration}")
print(f"Log file saved to: {log_file}")
if __name__ == "__main__":
main()