Spaces:
Runtime error
Runtime error
File size: 12,388 Bytes
cac74e8 99aeccf cac74e8 f1e2bf4 99aeccf cac74e8 f1e2bf4 cac74e8 ace688c 6ed9eed cac74e8 ace688c cac74e8 ace688c 6ed9eed cac74e8 | 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | #!/usr/bin/env python
# coding: utf-8
# # Code Generator
#
# The requirement: use a Frontier model to generate high performance C++ code from Python code
#
# <table style="margin: 0; text-align: left;">
# <tr>
# <td style="width: 150px; height: 150px; vertical-align: middle;">
# <img src="../resources.jpg" width="150" height="150" style="display: block;" />
# </td>
# <td>
# <h2 style="color:#f71;">Reminder: fetch latest code</h2>
# <span style="color:#f71;">I'm continually improving these labs, adding more examples and exercises.
# At the start of each week, it's worth checking you have the latest code.<br/>
# First do a <a href="https://chatgpt.com/share/6734e705-3270-8012-a074-421661af6ba9">git pull and merge your changes as needed</a>. Any problems? Try asking ChatGPT to clarify how to merge - or contact me!<br/><br/>
# After you've pulled the code, from the llm_engineering directory, in an Anaconda prompt (PC) or Terminal (Mac), run:<br/>
# <code>conda env update --f environment.yml --prune</code><br/>
# Or if you used virtualenv rather than Anaconda, then run this from your activated environment in a Powershell (PC) or Terminal (Mac):<br/>
# <code>pip install -r requirements.txt</code>
# <br/>Then restart the kernel (Kernel menu >> Restart Kernel and Clear Outputs Of All Cells) to pick up the changes.
# </span>
# </td>
# </tr>
# </table>
# <table style="margin: 0; text-align: left;">
# <tr>
# <td style="width: 150px; height: 150px; vertical-align: middle;">
# <img src="../important.jpg" width="150" height="150" style="display: block;" />
# </td>
# <td>
# <h1 style="color:#900;">Important Note</h1>
# <span style="color:#900;">
# In this lab, I use GPT-4o and Claude-3.5-Sonnet, which are the slightly higher priced models. The costs are still low, but if you'd prefer to keep costs ultra low, please make the suggested switches to the models (3 cells down from here).
# </span>
# </td>
# </tr>
# </table>
# In[1]:
# imports
import os
import io
import sys
from dotenv import load_dotenv
from IPython.display import Markdown, display, update_display
import gradio as gr
import subprocess
import google.generativeai as genai
# In[2]:
# environment
load_dotenv()
google_api_key = os.getenv('GOOGLE_API_KEY')
# In[3]:
GeminiModel=genai.configure(api_key=google_api_key)
# In[4]:
system_message = "You are an assistant that reimplements Python code in high performance C++ for an windows 11 OS. "
system_message += "Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments. "
system_message += "The C++ response needs to produce an identical output in the fastest possible time."
# In[5]:
def user_prompt_for(python):
user_prompt = "Rewrite this Python code in C++ with the fastest possible implementation that produces identical output in the least time. "
user_prompt += "Respond only with C++ code; do not explain your work other than a few comments. "
user_prompt += "Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\n\n"
user_prompt += python
return user_prompt
# In[6]:
def messages_for(python):
return [
{"role": "system", "content": system_message},
{"role": "user", "content": user_prompt_for(python)}
]
# In[7]:
def write_output(cpp):
code = cpp.replace("```cpp","").replace("```","")
with open("optimized.cpp", "w") as f:
f.write(code)
# In[8]:
def optimize_gemini(python):
stream=genai.GenerativeModel("gemini-1.5-flash")
prompt=f"{system_message}\n\n{messages_for(python)}"
response=stream.generate_content(prompt,stream=True)
result=""
for chunks in response:
if chunks.text:
result+=chunks.text
print(chunks.text,end='',flush=True)
write_output(result)
# In[11]:
pi = """
import time
def calculate(iterations, param1, param2):
result = 1.0
for i in range(1, iterations+1):
j = i * param1 - param2
result -= (1/j)
j = i * param1 + param2
result += (1/j)
return result
start_time = time.time()
result = calculate(100_000_000, 4, 1) * 4
end_time = time.time()
print(f"Result: {result:.12f}")
print(f"Execution Time: {(end_time - start_time):.6f} seconds")
"""
# In[12]:
exec(pi)
# In[13]:
optimize_gemini(pi)
# In[14]:
exec(pi)
# # Compiling C++ and executing
#
# This next cell contains the command to compile a C++ file on my M1 Mac.
# It compiles the file `optimized.cpp` into an executable called `optimized`
# Then it runs the program called `optimized`
#
# You can google (or ask ChatGPT!) for how to do this on your platform, then replace the lines below.
# If you're not comfortable with this step, you can skip it for sure - I'll show you exactly how it performs on my Mac.
# In[15]:
try:
subprocess.run(['g++', '-O3', '-std=c++17', '-o', 'optimized.exe', 'optimized.cpp'], check=True)
subprocess.run(['./optimized.exe'], check=True)
except subprocess.CalledProcessError as e:
print(f"Error during compilation or execution: {e}")
# In[16]:
optimize_gemini(pi)
# In[20]:
python_hard = """
def lcg(seed, a=1664525, c=1013904223, m=2**32):
value = seed
while True:
value = (a * value + c) % m
yield value
def max_subarray_sum(n, seed, min_val, max_val):
lcg_gen = lcg(seed)
random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]
max_sum = float('-inf')
for i in range(n):
current_sum = 0
for j in range(i, n):
current_sum += random_numbers[j]
if current_sum > max_sum:
max_sum = current_sum
return max_sum
def total_max_subarray_sum(n, initial_seed, min_val, max_val):
total_sum = 0
lcg_gen = lcg(initial_seed)
for _ in range(20):
seed = next(lcg_gen)
total_sum += max_subarray_sum(n, seed, min_val, max_val)
return total_sum
# Parameters
n = 10000 # Number of random numbers
initial_seed = 42 # Initial seed for the LCG
min_val = -10 # Minimum value of random numbers
max_val = 10 # Maximum value of random numbers
# Timing the function
import time
start_time = time.time()
result = total_max_subarray_sum(n, initial_seed, min_val, max_val)
end_time = time.time()
print("Total Maximum Subarray Sum (20 runs):", result)
print("Execution Time: {:.6f} seconds".format(end_time - start_time))
"""
# In[21]:
exec(python_hard)
# In[22]:
optimize_gemini(python_hard)
# In[23]:
try:
subprocess.run(['g++', '-O3', '-std=c++17', '-o', 'optimized.exe', 'optimized.cpp'], check=True)
subprocess.run(['./optimized.exe'], check=True)
except subprocess.CalledProcessError as e:
print(f"Error during compilation or execution: {e}")
# In[28]:
def write_output(cpp):
code = cpp.replace("```cpp","").replace("```","")
with open("optimized.cpp", "w") as f:
f.write(code)
# In[29]:
def stream_gemini(python):
stream=genai.GenerativeModel("gemini-1.5-flash")
prompt=f"{system_message}\n\n{messages_for(python)}"
response=stream.generate_content(prompt,stream=True)
result=""
for chunks in response:
if chunks.text:
result+=chunks.text
print(chunks.text,end='',flush=True)
write_output(result)
return result
# In[32]:
def optimize(python, model):
if model=="GEMINI":
result = stream_gemini(python)
return result
else:
raise ValueError("Unknown model")
# for stream_so_far in result:
# yield stream_so_far
# In[34]:
def execute_python(code):
try:
output = io.StringIO()
sys.stdout = output
exec(code)
finally:
sys.stdout = sys.__stdout__
return output.getvalue()
# In[35]:
# def execute_cpp(code):
# write_output(code)
# try:
# # Windows compilation and execution commands
# compile_cmd = ["g++", "-O3", "-std=c++17", "-o", "optimized.exe", "optimized.cpp"]
# compile_result = subprocess.run(compile_cmd, check=True, text=True, capture_output=True)
# run_cmd = ["optimized.exe"]
# run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)
# return run_result.stdout
# except subprocess.CalledProcessError as e:
# return f"An error occurred:\n{e.stderr}"
def execute_cpp(code):
try:
print("Compiling C++ code...")
compile_result = subprocess.run(
['g++', '-O3', '-std=c++17', '-o', 'optimized', 'optimized.cpp'],
check=True, text=True, capture_output=True
)
print("Compilation output:", compile_result.stdout)
print("Running optimized executable...")
subprocess.run(['chmod', '+x', 'optimized'], check=True)
result = subprocess.run(['./optimized'], check=True, text=True, capture_output=True)
return result.stdout
except subprocess.CalledProcessError as e:
return f"Error during compilation or execution: {e.stderr}"
# In[51]:
css = """
.container { margin: 15px; padding: 15px; }
.title { text-align: center; margin-bottom: 20px; }
.code-container {
background: #f5f5f5;
border-radius: 10px;
padding: 15px;
height: 500px !important; /* Fixed height */
overflow-y: auto !important; /* Enable vertical scrolling */
}
.button-row { gap: 10px; }
.convert-button { background: #4CAF50 !important; }
.run-button { background: #2196F3 !important; }
.output-container {
border-radius: 8px;
padding: 10px;
margin-top: 10px;
}
.python { background-color: #306998 !important; color: white !important; }
.cpp { background-color: #00599C !important; color: white !important; }
# /* Make sure the code editors take full height */
# .code-container > div {
# height: 100% !important;
# }
# .code-container textarea {
# height: 100% !important;
# }
"""
# In[52]:
with gr.Blocks(css=css) as ui:
with gr.Column(elem_classes=["container"]):
gr.Markdown("# 🔄 Python to C++ Converter", elem_classes=["title"])
# Code input section
with gr.Row(equal_height=True):
with gr.Column():
gr.Markdown("### Source Code")
python = gr.Code(
label="Python Code",
value=python_hard,
language="python",
elem_classes=["code-container"]
)
with gr.Column():
gr.Markdown("### Generated Code")
cpp = gr.Code(
label="C++ Code",
language="cpp",
elem_classes=["code-container"]
)
# Controls section
with gr.Row(elem_classes=["button-row"]):
model = gr.Dropdown(
["GEMINI"],
label="Select Model",
value="GEMINI",
container=False
)
convert = gr.Button("🔄 Convert", elem_classes=["convert-button"])
gr.Markdown("### Execution Results")
with gr.Row(equal_height=True):
with gr.Column():
python_run = gr.Button("▶️ Run Python", elem_classes=["run-button"])
python_out = gr.TextArea(
label="Python Output",
elem_classes=["output-container", "python"]
)
with gr.Column():
cpp_run = gr.Button("▶️ Run C++", elem_classes=["run-button"])
cpp_out = gr.TextArea(
label="C++ Output",
elem_classes=["output-container", "cpp"]
)
# Event handlers
convert.click(fn=optimize, inputs=[python, model], outputs=cpp)
python_run.click(fn=execute_python, inputs=[python], outputs=[python_out])
cpp_run.click(fn=execute_cpp, inputs=[cpp], outputs=[cpp_out])
ui.launch(inbrowser=True)
# In[ ]:
|