forked from HKUDS/DeepCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepcode.py
More file actions
executable file
Β·275 lines (224 loc) Β· 8.42 KB
/
Copy pathdeepcode.py
File metadata and controls
executable file
Β·275 lines (224 loc) Β· 8.42 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python3
"""
DeepCode - AI Research Engine Launcher
Next-Generation AI Research Automation Platform
Transform research papers into working code automatically
Provides a lightweight cross-platform CLI for launching the Streamlit UI and
running paper test workflows.
"""
from __future__ import annotations
import argparse
import importlib.util
import os
import shutil
import subprocess
import sys
from pathlib import Path
from typing import Iterable, Tuple
# Fix UTF-8 encoding for Windows console at the very start
if sys.platform == 'win32':
# Attempt to set UTF-8 encoding
try:
if hasattr(sys.stdout, 'reconfigure'):
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
if hasattr(sys.stderr, 'reconfigure'):
sys.stderr.reconfigure(encoding='utf-8', errors='replace')
except Exception:
pass
# Always wrap print on Windows to catch any remaining UnicodeEncodeError
_original_print = print
def print(*args, **kwargs):
try:
_original_print(*args, **kwargs)
except UnicodeEncodeError:
# Fallback: encode with errors='replace'
safe_args = []
for arg in args:
if isinstance(arg, str):
# Replace unencodable characters
encoding = sys.stdout.encoding or 'ascii'
safe_args.append(arg.encode(encoding, errors='replace').decode(encoding))
else:
safe_args.append(arg)
try:
_original_print(*safe_args, **kwargs)
except Exception:
# If it still fails, purely ascii fallback
ascii_args = [
str(a).encode('ascii', errors='replace').decode('ascii')
for a in args
]
_original_print(*ascii_args, **kwargs)
import builtins
builtins.print = print
def print_banner() -> None:
"""Display a simple ASCII banner."""
banner = (
"\n"
"##############################################################\n"
"# DeepCode - AI Research Engine #\n"
"# Transform research papers into working code automatically #\n"
"##############################################################\n"
)
print(banner)
def check_dependencies() -> bool:
"""Check required Python packages and optional system tools."""
print("Checking Python dependencies...")
required_modules: Iterable[Tuple[str, str]] = (
("streamlit", "streamlit>=1.28.0"),
("yaml", "pyyaml"),
("asyncio", "asyncio"),
)
optional_modules: Iterable[Tuple[str, str]] = (("reportlab", "reportlab"),)
missing_required = []
missing_optional = []
for module_name, install_name in required_modules:
if importlib.util.find_spec(module_name) is None:
missing_required.append(install_name)
print(f" - missing {install_name}")
else:
print(f" - found {module_name}")
for module_name, install_name in optional_modules:
if importlib.util.find_spec(module_name) is None:
missing_optional.append(install_name)
print(f" - optional dependency missing: {install_name}")
else:
print(f" - optional dependency available: {module_name}")
libreoffice_cmd = shutil.which("libreoffice") or shutil.which("soffice")
if libreoffice_cmd:
print(f"Found LibreOffice executable: {libreoffice_cmd}")
else:
print(
"LibreOffice not detected. Office document conversions will be skipped."
)
if missing_required:
print("\nInstall missing dependencies with:")
print(f" pip install {' '.join(missing_required)}")
return False
if missing_optional:
print(
"\nOptional dependencies missing. Some features may be limited until they "
"are installed."
)
return True
def cleanup_cache(root: Path) -> None:
"""Remove __pycache__ directories and .pyc files under *root*."""
print("\nCleaning Python cache artifacts...")
removed_dirs = 0
removed_files = 0
for dir_path in root.rglob("__pycache__"):
try:
shutil.rmtree(dir_path)
removed_dirs += 1
except Exception as exc: # pragma: no cover - best effort cleanup
print(f" could not delete {dir_path}: {exc}")
for file_path in root.rglob("*.pyc"):
try:
file_path.unlink()
removed_files += 1
except Exception as exc: # pragma: no cover - best effort cleanup
print(f" could not delete {file_path}: {exc}")
print(f"Removed {removed_dirs} __pycache__ directories and {removed_files} .pyc files.")
def launch_paper_test(paper_name: str, fast_mode: bool) -> None:
"""Launch the paper test workflow."""
print(f"\nLaunching paper test workflow for '{paper_name}'")
print(f"Fast mode: {'enabled' if fast_mode else 'disabled'}")
cmd = [sys.executable, "test_paper.py", paper_name]
if fast_mode:
cmd.append("--fast")
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as exc:
print(f"Paper test workflow failed: {exc}")
sys.exit(exc.returncode)
print("Paper test workflow finished successfully.")
print("Generated artifacts can be found under deepcode_lab/papers/")
print("Next steps:")
print(" 1. Install MCP dependencies: pip install -r requirements.txt")
print(
" 2. Run the full pipeline: "
f"python -m workflows.paper_test_engine --paper {paper_name}"
+ (" --fast" if fast_mode else "")
)
def launch_streamlit(app_path: Path) -> None:
"""Launch the Streamlit UI."""
print(f"Starting Streamlit app at {app_path}")
print("π Starting DeepCode web interface...")
print("π Launching on http://localhost:8503")
print("=" * 70)
print("π‘ Tip: Keep this terminal open while using the application")
print("π Press Ctrl+C to stop the server")
print("=" * 70)
cmd = [
sys.executable,
"-m",
"streamlit",
"run",
str(app_path),
"--server.port",
"8503",
"--server.address",
"localhost",
"--browser.gatherUsageStats",
"false",
"--theme.base",
"dark",
"--theme.primaryColor",
"#3b82f6",
"--theme.backgroundColor",
"#0f1419",
"--theme.secondaryBackgroundColor",
"#1e293b",
]
# Set UTF-8 encoding for subprocess
env = os.environ.copy()
env["PYTHONIOENCODING"] = "utf-8"
env["PYTHONLEGACYWINDOWSSTDIO"] = "utf-8"
try:
subprocess.run(cmd, check=True, env=env)
except KeyboardInterrupt:
print("\n\nπ DeepCode server stopped by user")
print("Thank you for using DeepCode! π§¬")
except subprocess.CalledProcessError as exc:
print(f"\nβ Failed to launch Streamlit: {exc}")
print("Please check if Streamlit is properly installed.")
sys.exit(exc.returncode)
def build_parser() -> argparse.ArgumentParser:
"""Create the CLI argument parser."""
parser = argparse.ArgumentParser(
description="DeepCode launcher",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
subparsers = parser.add_subparsers(dest="command")
test_parser = subparsers.add_parser(
"test", help="Run the paper test workflow"
)
test_parser.add_argument("paper", help="Paper identifier, e.g. 'rice'")
test_parser.add_argument(
"--fast",
"-f",
action="store_true",
help="Skip long-running steps when preparing the test",
)
return parser
def main(argv: Iterable[str] | None = None) -> None:
"""CLI entry point."""
parser = build_parser()
args = parser.parse_args(argv)
project_root = Path(__file__).resolve().parent
if args.command == "test":
print_banner()
launch_paper_test(args.paper, args.fast)
return
print_banner()
if not check_dependencies():
print("\nInstall the missing dependencies and try again.")
sys.exit(1)
streamlit_app = project_root / "ui" / "streamlit_app.py"
if not streamlit_app.exists():
print(f"Streamlit app not found at {streamlit_app}")
sys.exit(1)
launch_streamlit(streamlit_app)
cleanup_cache(project_root)
if __name__ == "__main__":
main()