Spaces:
Runtime error
Runtime error
File size: 12,080 Bytes
b8a000d ba214d5 b8a000d |
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 |
# imports
import re
import os
from dotenv import load_dotenv
from openai import OpenAI
import streamlit as st
import subprocess
import tempfile
import shutil
import time
from timeit import default_timer as timer
def install_rust():
subprocess.run("curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y", shell=True)
subprocess.run("source $HOME/.cargo/env", shell=True)
install_rust()
# Load environment variables
os.environ['PATH'] += f':{os.path.expanduser("~/.cargo/bin")}'
load_dotenv()
os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')
OPENAI_MODEL = "gpt-4o-mini"
class TranslateCode:
def __init__(self, openai_client, model):
self.openai = openai_client
self.model = model
def user_prompt_for(self, python, lang_select):
user_prompt = f"Rewrite this Python code in {lang_select} with the fastest possible implementation that produces identical output in the least time. "
user_prompt += f"Respond only with {lang_select} code; do not explain your work; only return {lang_select} code. "
user_prompt += "Pay attention to number types to ensure no int overflows. Remember to include all necessary dependencies and libraries.\n\n"
user_prompt += "If translating to Rust, make sure to include the necessary packages and crates."
user_prompt += python
return user_prompt
def messages_for(self, python, lang_select):
# System message for OpenAI API
system_message = "You are an assistant that reimplements Python code in high performance code for a Windows PC. "
system_message += "Respond only with code; do not provide any explanations. "
system_message += "The response needs to produce an identical output in the fastest possible time."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": self.user_prompt_for(python, lang_select)}
]
def translate_code(self, code_file, lang_select):
stream = self.openai.chat.completions.create(model=self.model, messages=self.messages_for(code_file, lang_select), stream=True)
code = ""
for chunk in stream:
fragment = chunk.choices[0].delta.content or ""
code += fragment
pattern = r"```(c|cpp|rust|javascript)\n"
code = re.sub(pattern, "", code).replace("```", "")
return code
class ExecuteCode:
def __init__(self, translator):
self.translator = translator
def extract_dependencies(self, code):
try:
dependency_pattern = r"""
(?:use\s+(?!std::)[a-zA-Z_][a-zA-Z0-9_]*::|extern\s+crate\s+(?!std)[a-zA-Z_][a-zA-Z0-9_]*);?
|
\#include\s*<([a-zA-Z_][a-zA-Z0-9_/.]*)>
|
(?:import\s+.*\s+from\s+['"]([a-zA-Z_][a-zA-Z0-9_/.]*)['"]
|require\s*\(\s*['"]([a-zA-Z_][a-zA-Z0-9_/.]*)['"]\s*\))
"""
matches = re.findall(dependency_pattern, code, re.VERBOSE)
dependencies = [match for match in matches if any(match)]
return dependencies if matches else []
except re.error as e:
raise ValueError(f"Regex error while extracting dependencies: {e}")
def execute_code(self, code_file, lang_select):
if lang_select == "Rust":
rust_code = self.translator.translate_code(code_file, lang_select)
try:
dependencies = self.extract_dependencies(rust_code)
temp_dir = tempfile.mkdtemp()
src_dir = os.path.join(temp_dir, "src")
os.makedirs(src_dir, exist_ok=True)
cargo_toml = f"""
[package]
name = "temp_project"
version = "0.1.0"
edition = "2021"
[dependencies]
"""
for dependency in dependencies:
crate = dependency[0]
cargo_toml += f"{crate} = \"*\"\n"
with open(os.path.join(temp_dir, "Cargo.toml"), "w") as f:
f.write(cargo_toml)
main_rs_path = os.path.join(src_dir, "main.rs")
with open(main_rs_path, "w", encoding="utf-8") as f:
f.write(rust_code)
cargo_build = subprocess.run(["cargo", "build", "--release"],
cwd=temp_dir,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
if cargo_build.returncode != 0:
return f"Cargo build failed:\n{cargo_build.stderr}", 0
executable_path = os.path.join(temp_dir, "target", "release", "temp_project")
start_time = timer()
run_result = subprocess.run([executable_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
end_time = timer()
execution_time = end_time - start_time
if run_result.returncode != 0:
print(f"Execution failed: {run_result.stderr}")
return run_result.stdout, execution_time
finally:
if temp_dir:
shutil.rmtree(temp_dir, ignore_errors=True)
elif lang_select in ["C", "C++"]:
code = self.translator.translate_code(code_file, lang_select)
with tempfile.TemporaryDirectory() as temp_dir:
file_extension = "c" if lang_select == "C" else "cpp"
file_path = os.path.join(temp_dir, f"translated_code.{file_extension}")
with open(file_path, "w") as f:
f.write(code)
executable_path = os.path.join(temp_dir, "translated_code")
compiler = "gcc" if lang_select == "C" else "g++"
compile_result = subprocess.run([compiler, file_path, "-o", executable_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
if compile_result.returncode != 0:
return f"Compilation failed:\n{compile_result.stderr}", 0
start_time = timer()
run_result = subprocess.run([executable_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
end_time = timer()
execution_time = end_time - start_time
return run_result.stdout, execution_time
elif lang_select == "Javascript":
js_code = self.translator.translate_code(code_file, lang_select)
with tempfile.NamedTemporaryFile(suffix='.js', delete=False) as js_file:
js_file.write(js_code.encode("utf-8"))
js_file.flush()
js_file_path = js_file.name
try:
start_time = timer()
run_result = subprocess.run(["node", js_file_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
end_time = timer()
execution_time = end_time - start_time
return run_result.stdout, execution_time
finally:
os.remove(js_file_path)
else:
return "Language not supported", 0
class StreamlitApp:
def __init__(self, translator, executor):
self.translator = translator
self.executor = executor
def main(self):
st.title("SyntaxShift: Code Translator")
st.write("It's like Google Translate, but for code.\n\nUpload a Python file to translate it to C, C++, Rust, or Javascript, and run the code.")
with st.sidebar:
st.write("Upload Python file here:")
uploaded_file = st.file_uploader("Choose a Python file", type="py")
lang_select = st.selectbox("Select the language to translate to:", ["C", "C++", "Rust", "Javascript"])
if uploaded_file is not None and lang_select:
source_code = uploaded_file.read().decode("utf-8")
col1, col2 = st.columns(2)
with col1:
st.subheader("Original Python Code")
st.code(source_code, language='python')
if st.button("Run Python Code"):
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as temp_py_file:
temp_py_file.write(source_code.encode('utf-8'))
temp_py_file_path = temp_py_file.name
start_time = time.time()
result = subprocess.run(["python", temp_py_file_path], capture_output=True, text=True)
end_time = time.time()
output = result.stdout if result.returncode == 0 else result.stderr
execution_time = end_time - start_time
except Exception as e:
output = str(e)
execution_time = 0
finally:
if os.path.exists(temp_py_file_path):
os.remove(temp_py_file_path)
st.subheader("Output")
st.code(output, language='text')
st.code(f"Execution time: {execution_time:4f} seconds", language='text')
with col2:
source_code_key = f"source_code_{lang_select}"
translated_code_key = f"translated_code_{lang_select}"
if source_code_key not in st.session_state or translated_code_key not in st.session_state:
translated_code = self.translator.translate_code(source_code, lang_select)
translated_code = translated_code.encode("utf-8").decode("utf-8")
translated_code = translated_code.replace("Â", "")
st.session_state[translated_code_key] = translated_code
else:
translated_code = st.session_state[translated_code_key]
lang_dict = {"C": "c", "C++": "cpp", "Rust": "rust", "Javascript": "javascript"}
st.subheader(f"Translated {lang_select} Code")
st.code(translated_code, language=lang_dict[lang_select])
#suffix_dict = {"C": ".c", "C++": ".cpp", "Rust": ".rs", "Javascript": ".js"}
if st.button(f"Run Translated {lang_select} Code"):
output_key = f"output_{lang_select}"
execution_time_key = f"execution_time_{lang_select}"
if output_key not in st.session_state or execution_time_key not in st.session_state:
output, execution_time = self.executor.execute_code(translated_code, lang_select)
st.session_state[output_key] = output
st.session_state[execution_time_key] = execution_time
else:
output = st.session_state[output_key]
execution_time = st.session_state[execution_time_key]
st.subheader("Output")
st.code(output.replace("Â", ""), language='text')
st.code(f"Execution time: {execution_time:4f} seconds", language='text')
if __name__ == "__main__":
openai_client = OpenAI()
translator = TranslateCode(openai_client, OPENAI_MODEL)
executor = ExecuteCode(translator)
app = StreamlitApp(translator, executor)
app.main() |