-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathmain.py
66 lines (53 loc) · 2.13 KB
/
main.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
import os
import sys
import importlib
import inspect
from pathlib import Path
from scenarios.common.scenario import ChatMLAppScenario
def find_subclasses(base_class, folder_path):
subclasses = []
sys.path.insert(0, folder_path)
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".py"):
module_path = Path(root) / file
relative_module_path = module_path.relative_to(folder_path).with_suffix("")
module_name = str(relative_module_path).replace("/", ".").replace("\\", ".")
if any(name in module_name for name in ["__init__", "common", "code-completion", "gpt3langchain", "puzzle"]):
continue
try:
module = importlib.import_module(module_name)
except ImportError as e:
print(f"Error importing module {module_name}: {e}")
continue
for _, cls in inspect.getmembers(module, inspect.isclass):
if issubclass(cls, base_class) and cls != base_class:
subclasses.append(cls)
sys.path.pop(0)
return subclasses
def menu():
scenarios = find_subclasses(ChatMLAppScenario, os.path.dirname(__file__))
while True:
print("Which scenario would you like to run?")
print("0. Run all scenarios")
for i, scenario in enumerate(scenarios):
print(f"{i + 1}. {scenario.name}")
print("q. Quit")
choice = input("> ")
if choice == "q":
break
elif choice == "0":
results = []
for scenario in scenarios:
results.append(scenario(interactive=False).run())
for scenario, result in zip(scenarios, results):
print(f"{scenario.name} {'PASS' if result else 'FAIL'}")
else:
try:
scenario = scenarios[int(choice) - 1]
except IndexError:
print("Invalid choice")
else:
scenario(interactive=True, verbose=True).run()
if __name__ == "__main__":
menu()