Browse Source

feat(week 3): added anime audio translator and fix code from previous code conversion

pull/321/head
ken 4 weeks ago
parent
commit
418944e752
  1. 1
      week3/community-contributions/anime_audio_translator.colab.ipynb
  2. 60
      week4/community-contributions/code_conversion.ipynb

1
week3/community-contributions/anime_audio_translator.colab.ipynb

File diff suppressed because one or more lines are too long

60
week4/community-contributions/code_conversion.ipynb

@ -298,7 +298,6 @@
"outputs": [],
"source": [
"def execute_cpp(code):\n",
" write_output(code, \"cpp\")\n",
" try:\n",
" compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-o\", \"optimized\", \"optimized.cpp\"]\n",
" compile_result = subprocess.run(compile_cmd, shell=True, text=True, capture_output=True)\n",
@ -306,25 +305,53 @@
" run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
" return run_result.stdout\n",
" except subprocess.CalledProcessError as e:\n",
" return f\"An error occurred:\\n{e.stderr}\"\n",
"\n",
" return f\"An error occurred:\\n{e.stderr}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "91ba8a3c-8686-4636-bf21-efc861f3a2b7",
"metadata": {},
"outputs": [],
"source": [
"def execute_js(code):\n",
" write_output(code, \"js\")\n",
" try:\n",
" run_result = subprocess.run([\"node\", \"optimized.js\"], shell=True, text=True, capture_output=True)\n",
" run_result = subprocess.run([\"node\", \"optimized.js\"], check=True, text=True, capture_output=True)\n",
" return run_result.stdout\n",
" except subprocess.CalledProcessError as e:\n",
" return f\"An error occurred:\\n{e.stderr}\"\n",
"\n",
" return f\"An error occurred:\\n{e.stderr}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b9006f67-f631-4ad4-bf45-b9366c822a04",
"metadata": {},
"outputs": [],
"source": [
"def execute_php(code):\n",
" write_output(code, \"php\")\n",
" try:\n",
" run_result = subprocess.run([\"php\", \"optimized.php\"], shell=True, text=True, capture_output=True)\n",
" return run_result.stdout or run_result.stderr\n",
" run_result = subprocess.run([\"php\", \"optimized.php\"], check=True, text=True, capture_output=True)\n",
" return run_result.stdout\n",
" except subprocess.CalledProcessError as e:\n",
" return f\"An error occurred:\\n{e.stderr}\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b3991a09-f60d-448a-8e92-2561296d05cf",
"metadata": {},
"outputs": [],
"source": [
"def handle_execution(code, prog_lang):\n",
" write_output(code, prog_lang)\n",
"\n",
" index = next((i for i, lang in enumerate(programming_languages) if lang[\"extension\"] == prog_lang), -1)\n",
" return programming_languages[index][\"fn\"](code)"
]
},
{
"cell_type": "code",
"execution_count": null,
@ -363,17 +390,10 @@
" current_selected = gr.Dropdown([extension], value=extension, visible=False)\n",
" \n",
" convert.click(optimize, inputs=[python, model, current_selected], outputs=[converted_code])\n",
" \n",
" match extension:\n",
" case \"cpp\":\n",
" prog_run.click(execute_cpp, inputs=[converted_code], outputs=[prog_out])\n",
" case \"js\":\n",
" prog_run.click(execute_js, inputs=[converted_code], outputs=[prog_out])\n",
" case \"php\":\n",
" prog_run.click(execute_php, inputs=[converted_code], outputs=[prog_out])\n",
" prog_run.click(handle_execution, inputs=[converted_code, current_selected], outputs=[prog_out])\n",
"\n",
"with gr.Blocks(css=css) as ui:\n",
" gr.Markdown(\"## Convert code from Python to selected Programming Language\")\n",
" gr.Markdown(\"# Convert code from Python to any Programming Language\")\n",
" with gr.Row():\n",
" with gr.Column():\n",
" python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
@ -391,7 +411,7 @@
" create_prog_lang_ui(lang, model)\n",
"\n",
"ui.launch(\n",
" # inbrowser=True\n",
" inbrowser=True\n",
")"
]
}

Loading…
Cancel
Save