Browse Source

add java as a conversion option

pull/175/head
Dany.Makhoul 3 months ago
parent
commit
5849a64d1e
  1. 386
      week4/community-contributions/day4_with_inference_provider.ipynb

386
week4/community-contributions/day4_with_inference_provider.ipynb

@ -16,12 +16,37 @@
"Added the use of inference providers that was introduced recently by Hugging Face to convert the code.\n", "Added the use of inference providers that was introduced recently by Hugging Face to convert the code.\n",
"Improved the user prompt to include algorithic efficeiny and performance optimization.\n", "Improved the user prompt to include algorithic efficeiny and performance optimization.\n",
"\n", "\n",
"Note: C++ commands work on windows environment." "Added Java as a conversion option.\n",
"\n",
"Note: C++ commands work on windows environment.\n"
]
},
{
"cell_type": "markdown",
"id": "22e1567b-33fd-49e7-866e-4b635d15715a",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left;\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../important.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h1 style=\"color:#900;\">Important - Pause Endpoints when not in use</h1>\n",
" <span style=\"color:#900;\">\n",
" If you do decide to use HuggingFace endpoints for this project, you should stop or pause the endpoints when you are done to avoid accruing unnecessary running cost. The costs are very low as long as you only run the endpoint when you're using it. Navigate to the HuggingFace endpoint UI <a href=\"https://ui.endpoints.huggingface.co/\">here,</a> open your endpoint, and click Pause to put it on pause so you no longer pay for it. \n",
"Many thanks to student John L. for raising this.\n",
"<br/><br/>\n",
"In week 8 we will use Modal instead of HuggingFace endpoints; with Modal you only pay for the time that you use it and you should get free credits.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 44, "execution_count": 231,
"id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3", "id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -39,12 +64,12 @@
"import anthropic\n", "import anthropic\n",
"from IPython.display import Markdown, display, update_display\n", "from IPython.display import Markdown, display, update_display\n",
"import gradio as gr\n", "import gradio as gr\n",
"import subprocess" "import subprocess, re"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 45, "execution_count": 198,
"id": "4f672e1c-87e9-4865-b760-370fa605e614", "id": "4f672e1c-87e9-4865-b760-370fa605e614",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -59,7 +84,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 46, "execution_count": 199,
"id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da", "id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -74,119 +99,155 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 47, "execution_count": 200,
"id": "6896636f-923e-4a2c-9d6c-fac07828a201", "id": "2db60a72-d098-42ca-8ce2-1e037c86b718",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"system_message = \"You are an assistant that reimplements Python code in high performance C++ for an Windows intel i7. \"\n", "def system_prompt_for(language: str) -> str:\n",
"system_message += \"Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments. \"\n", " system_prompt = (\n",
"system_message += \"The C++ response needs to produce an identical output in the fastest possible time. Keep implementations of random number generators identical so that results match exactly.\"" " f\"You are an assistant that reimplements Python code in high performance {language.upper()} for an Windows intel i7.\"\n",
" f\"Respond only with {language.upper()} code; use comments sparingly and do not provide any explanation other than occasional comments.\"\n",
" f\"The {language.upper()} response needs to produce an identical output in the fastest possible time. Keep implementations of random number generators identical so that results match exactly.\"\n",
" )\n",
" return system_prompt"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 72, "execution_count": 243,
"id": "70583432-e851-40d1-a219-2fb32b830dc8", "id": "70583432-e851-40d1-a219-2fb32b830dc8",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"#updated the original prompt to include algorithic efficeiny and performance optimization\n", "#updated the original prompt to include algorithic efficeiny and performance optimization\n",
"def user_prompt_for(python: str) -> str:\n", "def user_prompt_for(python: str, language: str) -> str:\n",
" if language.lower() not in {\"cpp\", \"java\"}:\n",
" raise ValueError(\"Unsupported language. Please choose 'C++' or 'Java'.\")\n",
" \n",
" optimization_notes = {\n",
" \"cpp\": (\n",
" \"- Use `int64_t` instead of `int` where necessary to prevent overflows.\\n\"\n",
" \"- Ensure random number generation in C++ matches Python's output as closely as possible.\\n\"\n",
" \"- Avoid undefined behavior, such as bit shifts that exceed type width (`1UL << 32` is incorrect for `uint32_t`).\\n\"\n",
" \"- Utilize `std::vector` for dynamic arrays and prefer preallocation for efficiency.\\n\"\n",
" \"- Consider `std::array` or `std::span` when fixed-size arrays are sufficient.\\n\"\n",
" \"- Optimize with **SIMD**, cache-friendly structures, and memory alignment where necessary.\\n\"\n",
" ),\n",
" \"java\": (\n",
" \"- Use `long` instead of `int` where necessary to prevent overflows.\\n\"\n",
" \"- Ensure random number generation in Java matches Python's output as closely as possible.\\n\"\n",
" \"- Use `ArrayList` instead of primitive arrays if dynamic resizing is needed.\\n\"\n",
" \"- Utilize `BigInteger` if handling large numbers that could exceed `long`.\\n\"\n",
" \"- Optimize with **parallel streams** (`IntStream.parallel()`) and **efficient data structures** (`HashMap`, `LinkedList`, etc.).\\n\"\n",
" )\n",
" }\n",
"\n",
" user_prompt = (\n", " user_prompt = (\n",
" \"First, analyze the given Python code to understand its core purpose and algorithmic approach. \"\n", " f\"First, analyze the given Python code to understand its core purpose and algorithmic approach. \"\n",
" \"Then, implement a C++ solution that achieves the same output while prioritizing:\\n\"\n", " f\"Then, implement a {language} solution that achieves the same output while prioritizing:\\n\"\n",
" \"1. **Algorithmic Efficiency** - Optimize time and space complexity, even if it means using a different approach.\\n\"\n", " \"1. **Algorithmic Efficiency** - Optimize time and space complexity, even if it means using a different approach.\\n\"\n",
" \"2. **Numerical Correctness** - Prevent integer overflows, use appropriate data types (`int64_t`, `uint64_t`, `double`, etc.), \"\n", " \"2. **Numerical Correctness** - Prevent integer overflows, use appropriate data types (`long`, `BigInteger`, etc.), \"\n",
" \"and ensure correct handling of edge cases.\\n\"\n", " \"and ensure correct handling of edge cases.\\n\"\n",
" \"3. **Performance Optimization** - Utilize C++-specific features (e.g., `std::vector` with preallocation, SIMD optimizations, cache-friendly structures).\\n\\n\"\n", " \"3. **Performance Optimization** - Utilize language-specific features for efficiency.\\n\\n\"\n",
" \n", " \n",
" \"### **Important Notes:**\\n\"\n", " \"### **Important Notes:**\\n\"\n",
" \"- Use `int64_t` instead of `int` where necessary to prevent overflows.\\n\"\n", " + optimization_notes[language.lower()] +\n",
" \"- Ensure random number generation in C++ matches Python's output as closely as possible.\\n\"\n", " \"\\n### **Expected Response:**\\n\"\n",
" \"- Avoid undefined behavior, such as bit shifts that exceed type width (`1UL << 32` is incorrect for `uint32_t`).\\n\"\n", " f\"Respond **only with {language} code**, including all necessary imports and ensuring the output matches the Python version exactly.\\n\\n\"\n",
" \"- Comment on key optimizations and complexity improvements in the C++ code.\\n\\n\"\n",
" \n",
" \"### **Expected Response:**\\n\"\n",
" \"Respond **only with C++ code**, including all necessary headers and ensuring the output matches the Python version exactly.\\n\\n\"\n",
" \n", " \n",
" \"Here's the Python code to analyze and optimize:\\n\\n\"\n", " \"Here's the Python code to analyze and optimize:\\n\\n\"\n",
" + python\n", " + python\n",
" )\n", " )\n",
" return user_prompt" " \n",
" return user_prompt\n"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 49, "execution_count": 202,
"id": "c6190659-f54c-4951-bef4-4960f8e51cc4", "id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def messages_for(python):\n", "def messages_for(python, language=\"cpp\"):\n",
" return [\n", " return [\n",
" {\"role\": \"system\", \"content\": system_message},\n", " {\"role\": \"system\", \"content\": system_prompt_for(language)},\n",
" {\"role\": \"user\", \"content\": user_prompt_for(python)}\n", " {\"role\": \"user\", \"content\": user_prompt_for(python, language)}\n",
" ]" " ]"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 50, "execution_count": 241,
"id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b", "id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# write to a file called optimized.cpp\n", "# write to a file called optimized.cpp\n",
"\n", "\n",
"def write_output(cpp):\n", "def write_output(code, file_name):\n",
" code = cpp.replace(\"```cpp\",\"\").replace(\"```\",\"\")\n", " with open(file_name, \"w\") as f:\n",
" with open(\"optimized.cpp\", \"w\") as f:\n",
" f.write(code)" " f.write(code)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 51, "execution_count": 226,
"id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9", "id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def optimize_gpt(python): \n", "def optimize_gpt(python, language=\"cpp\"): \n",
" stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n", " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python, language), stream=True)\n",
" reply = \"\"\n", " reply = \"\"\n",
" for chunk in stream:\n", " for chunk in stream:\n",
" fragment = chunk.choices[0].delta.content or \"\"\n", " fragment = chunk.choices[0].delta.content or \"\"\n",
" reply += fragment\n", " reply += fragment\n",
" print(fragment, end='', flush=True)\n", " print(fragment, end='', flush=True)\n",
" write_output(reply)" " file_name= f\"optimized.{language}\"\n",
" if language == \"java\":\n",
" # Extract class name from Java code\n",
" match = re.search(r\"\\b(public\\s+)?class\\s+(\\w+)\", reply)\n",
" class_name = match.group(2) if match else \"OptimizedJava\"\n",
" file_name = f\"{class_name}.java\"\n",
" else:\n",
" file_name = f\"optimized.{language}\"\n",
" write_output(reply, file_name)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 52, "execution_count": 227,
"id": "7cd84ad8-d55c-4fe0-9eeb-1895c95c4a9d", "id": "7cd84ad8-d55c-4fe0-9eeb-1895c95c4a9d",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def optimize_claude(python):\n", "def optimize_claude(python, language=\"cpp\"):\n",
" result = claude.messages.stream(\n", " result = claude.messages.stream(\n",
" model=CLAUDE_MODEL,\n", " model=CLAUDE_MODEL,\n",
" max_tokens=2000,\n", " max_tokens=2000,\n",
" system=system_message,\n", " system=system_message,\n",
" messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n", " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python, language)}],\n",
" )\n", " )\n",
" reply = \"\"\n", " reply = \"\"\n",
" with result as stream:\n", " with result as stream:\n",
" for text in stream.text_stream:\n", " for text in stream.text_stream:\n",
" reply += text\n", " reply += text\n",
" print(text, end=\"\", flush=True)\n", " print(text, end=\"\", flush=True)\n",
" write_output(reply)" " if language == \"java\":\n",
" # Extract class name from Java code\n",
" match = re.search(r\"\\b(public\\s+)?class\\s+(\\w+)\", reply)\n",
" class_name = match.group(2) if match else \"OptimizedJava\"\n",
" file_name = f\"{class_name}.java\"\n",
" else:\n",
" file_name = f\"optimized.{language}\"\n",
" write_output(reply, file_name)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 53, "execution_count": 206,
"id": "a1cbb778-fa57-43de-b04b-ed523f396c38", "id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -224,12 +285,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 91,
"id": "105db6f9-343c-491d-8e44-3a5328b81719", "id": "105db6f9-343c-491d-8e44-3a5328b81719",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"optimize_gpt(pi)" "optimize_gpt(pi, \"java\")"
] ]
}, },
{ {
@ -276,7 +337,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 54, "execution_count": 207,
"id": "c3b497b3-f569-420e-b92e-fb0f49957ce0", "id": "c3b497b3-f569-420e-b92e-fb0f49957ce0",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -380,44 +441,48 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 55, "execution_count": 240,
"id": "0be9f47d-5213-4700-b0e2-d444c7c738c0", "id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def stream_gpt(python): \n", "def stream_gpt(python, language=\"cpp\"): \n",
" stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n", " stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python, language), stream=True)\n",
" reply = \"\"\n", " reply = \"\"\n",
" code_block = f\"```{language}\\n\"\n",
" for chunk in stream:\n", " for chunk in stream:\n",
" fragment = chunk.choices[0].delta.content or \"\"\n", " fragment = chunk.choices[0].delta.content or \"\"\n",
" reply += fragment\n", " reply += fragment\n",
" yield reply.replace('```cpp\\n','').replace('```','')" " cleaned_reply = reply.replace(code_block,'').replace('```','')\n",
" yield cleaned_reply"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 56, "execution_count": 239,
"id": "8669f56b-8314-4582-a167-78842caea131", "id": "8669f56b-8314-4582-a167-78842caea131",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def stream_claude(python):\n", "def stream_claude(python, language=\"cpp\"):\n",
" result = claude.messages.stream(\n", " result = claude.messages.stream(\n",
" model=CLAUDE_MODEL,\n", " model=CLAUDE_MODEL,\n",
" max_tokens=2000,\n", " max_tokens=2000,\n",
" system=system_message,\n", " system=system_message,\n",
" messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n", " messages=[{\"role\": \"user\", \"content\": user_prompt_for(python, language)}],\n",
" )\n", " )\n",
" reply = \"\"\n", " reply = \"\"\n",
" code_block = f\"```{language}\\n\"\n",
" with result as stream:\n", " with result as stream:\n",
" for text in stream.text_stream:\n", " for text in stream.text_stream:\n",
" reply += text\n", " reply += text\n",
" yield reply.replace('```cpp\\n','').replace('```','')" " cleaned_reply = reply.replace(code_block,'').replace('```','')\n",
" yield cleaned_reply"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 57, "execution_count": 186,
"id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d", "id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -435,14 +500,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24, "execution_count": 189,
"id": "f1ddb38e-6b0a-4c37-baa4-ace0b7de887a", "id": "f1ddb38e-6b0a-4c37-baa4-ace0b7de887a",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/html": [ "text/html": [
"<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>" "<div><iframe src=\"http://127.0.0.1:7888/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
], ],
"text/plain": [ "text/plain": [
"<IPython.core.display.HTML object>" "<IPython.core.display.HTML object>"
@ -455,7 +520,7 @@
"data": { "data": {
"text/plain": [] "text/plain": []
}, },
"execution_count": 24, "execution_count": 189,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -476,7 +541,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 58, "execution_count": 210,
"id": "19bf2bff-a822-4009-a539-f003b1651383", "id": "19bf2bff-a822-4009-a539-f003b1651383",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -493,25 +558,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 59, "execution_count": 211,
"id": "77f3ab5d-fcfb-4d3f-8728-9cacbf833ea6",
"metadata": {},
"outputs": [],
"source": [
"def execute_cpp(code):\n",
" write_output(code)\n",
" try:\n",
" compile_result = subprocess.run(compiler_cmd[2], check=True, text=True, capture_output=True)\n",
" run_cmd = [\"optimized.exe\"]\n",
" 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}\""
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "9a2274f1-d03b-42c0-8dcc-4ce159b18442", "id": "9a2274f1-d03b-42c0-8dcc-4ce159b18442",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -519,19 +566,20 @@
"css = \"\"\"\n", "css = \"\"\"\n",
".python {background-color: #306998;}\n", ".python {background-color: #306998;}\n",
".cpp {background-color: #050;}\n", ".cpp {background-color: #050;}\n",
".java {background-color: #306775;}\n",
"\"\"\"" "\"\"\""
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 28, "execution_count": 97,
"id": "f1303932-160c-424b-97a8-d28c816721b2", "id": "f1303932-160c-424b-97a8-d28c816721b2",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/html": [ "text/html": [
"<div><iframe src=\"http://127.0.0.1:7863/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>" "<div><iframe src=\"http://127.0.0.1:7868/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
], ],
"text/plain": [ "text/plain": [
"<IPython.core.display.HTML object>" "<IPython.core.display.HTML object>"
@ -544,7 +592,7 @@
"data": { "data": {
"text/plain": [] "text/plain": []
}, },
"execution_count": 28, "execution_count": 97,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -575,7 +623,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 61, "execution_count": 191,
"id": "bb8c5b4e-ec51-4f21-b3f8-6aa94fede86d", "id": "bb8c5b4e-ec51-4f21-b3f8-6aa94fede86d",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -585,7 +633,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 62, "execution_count": 117,
"id": "13347633-4606-4e38-9927-80c39e65c1f1", "id": "13347633-4606-4e38-9927-80c39e65c1f1",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -604,7 +652,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 63, "execution_count": 118,
"id": "ef60a4df-6267-4ebd-8eed-dcb917af0a5e", "id": "ef60a4df-6267-4ebd-8eed-dcb917af0a5e",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -616,10 +664,30 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 64, "execution_count": 119,
"id": "3825d77a-03c6-42b2-89bc-ccbcb1585740", "id": "3825d77a-03c6-42b2-89bc-ccbcb1585740",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"ename": "HfHubHTTPError",
"evalue": "402 Client Error: Payment Required for url: https://huggingface.co/api/inference-proxy/sambanova/v1/chat/completions (Request ID: Root=1-67afb729-1eb9aff1704314144ef14e59;2df843ad-b7d2-4145-bb7b-1cfd94ae19ef)\n\nYou have exceeded your monthly included credits for Inference Endpoints. Subscribe to PRO to get 20x more monthly allowance.",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mHTTPError\u001b[0m Traceback (most recent call last)",
"File \u001b[1;32m~\\anaconda3\\envs\\llms\\Lib\\site-packages\\huggingface_hub\\utils\\_http.py:406\u001b[0m, in \u001b[0;36mhf_raise_for_status\u001b[1;34m(response, endpoint_name)\u001b[0m\n\u001b[0;32m 405\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 406\u001b[0m \u001b[43mresponse\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraise_for_status\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 407\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m HTTPError \u001b[38;5;28;01mas\u001b[39;00m e:\n",
"File \u001b[1;32m~\\anaconda3\\envs\\llms\\Lib\\site-packages\\requests\\models.py:1024\u001b[0m, in \u001b[0;36mResponse.raise_for_status\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 1023\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m http_error_msg:\n\u001b[1;32m-> 1024\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HTTPError(http_error_msg, response\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m)\n",
"\u001b[1;31mHTTPError\u001b[0m: 402 Client Error: Payment Required for url: https://huggingface.co/api/inference-proxy/sambanova/v1/chat/completions",
"\nThe above exception was the direct cause of the following exception:\n",
"\u001b[1;31mHfHubHTTPError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[119], line 5\u001b[0m\n\u001b[0;32m 1\u001b[0m client \u001b[38;5;241m=\u001b[39m InferenceClient(\n\u001b[0;32m 2\u001b[0m \tprovider\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msambanova\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m 3\u001b[0m \tapi_key\u001b[38;5;241m=\u001b[39mhf_token\n\u001b[0;32m 4\u001b[0m )\n\u001b[1;32m----> 5\u001b[0m stream \u001b[38;5;241m=\u001b[39m \u001b[43mclient\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mchat\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcompletions\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreate\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 6\u001b[0m \u001b[43m\t\u001b[49m\u001b[43mmodel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mQwen/Qwen2.5-Coder-32B-Instruct\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\n\u001b[0;32m 7\u001b[0m \u001b[43m\t\u001b[49m\u001b[43mmessages\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmessages\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\n\u001b[0;32m 8\u001b[0m \u001b[43m\t\u001b[49m\u001b[43mmax_tokens\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m500\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m 9\u001b[0m \u001b[43m\t\u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\n\u001b[0;32m 10\u001b[0m \u001b[43m)\u001b[49m\n\u001b[0;32m 12\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m chunk \u001b[38;5;129;01min\u001b[39;00m stream:\n\u001b[0;32m 13\u001b[0m \u001b[38;5;28mprint\u001b[39m(chunk\u001b[38;5;241m.\u001b[39mchoices[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m.\u001b[39mdelta\u001b[38;5;241m.\u001b[39mcontent, end\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
"File \u001b[1;32m~\\anaconda3\\envs\\llms\\Lib\\site-packages\\huggingface_hub\\inference\\_client.py:970\u001b[0m, in \u001b[0;36mInferenceClient.chat_completion\u001b[1;34m(self, messages, model, stream, frequency_penalty, logit_bias, logprobs, max_tokens, n, presence_penalty, response_format, seed, stop, stream_options, temperature, tool_choice, tool_prompt, tools, top_logprobs, top_p)\u001b[0m\n\u001b[0;32m 943\u001b[0m parameters \u001b[38;5;241m=\u001b[39m {\n\u001b[0;32m 944\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m\"\u001b[39m: payload_model,\n\u001b[0;32m 945\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrequency_penalty\u001b[39m\u001b[38;5;124m\"\u001b[39m: frequency_penalty,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 961\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstream_options\u001b[39m\u001b[38;5;124m\"\u001b[39m: stream_options,\n\u001b[0;32m 962\u001b[0m }\n\u001b[0;32m 963\u001b[0m request_parameters \u001b[38;5;241m=\u001b[39m provider_helper\u001b[38;5;241m.\u001b[39mprepare_request(\n\u001b[0;32m 964\u001b[0m inputs\u001b[38;5;241m=\u001b[39mmessages,\n\u001b[0;32m 965\u001b[0m parameters\u001b[38;5;241m=\u001b[39mparameters,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 968\u001b[0m api_key\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtoken,\n\u001b[0;32m 969\u001b[0m )\n\u001b[1;32m--> 970\u001b[0m data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_inner_post\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequest_parameters\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 972\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m stream:\n\u001b[0;32m 973\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _stream_chat_completion_response(data) \u001b[38;5;66;03m# type: ignore[arg-type]\u001b[39;00m\n",
"File \u001b[1;32m~\\anaconda3\\envs\\llms\\Lib\\site-packages\\huggingface_hub\\inference\\_client.py:327\u001b[0m, in \u001b[0;36mInferenceClient._inner_post\u001b[1;34m(self, request_parameters, stream)\u001b[0m\n\u001b[0;32m 324\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m InferenceTimeoutError(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInference call timed out: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrequest_parameters\u001b[38;5;241m.\u001b[39murl\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merror\u001b[39;00m \u001b[38;5;66;03m# type: ignore\u001b[39;00m\n\u001b[0;32m 326\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 327\u001b[0m \u001b[43mhf_raise_for_status\u001b[49m\u001b[43m(\u001b[49m\u001b[43mresponse\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 328\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m response\u001b[38;5;241m.\u001b[39miter_lines() \u001b[38;5;28;01mif\u001b[39;00m stream \u001b[38;5;28;01melse\u001b[39;00m response\u001b[38;5;241m.\u001b[39mcontent\n\u001b[0;32m 329\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m HTTPError \u001b[38;5;28;01mas\u001b[39;00m error:\n",
"File \u001b[1;32m~\\anaconda3\\envs\\llms\\Lib\\site-packages\\huggingface_hub\\utils\\_http.py:477\u001b[0m, in \u001b[0;36mhf_raise_for_status\u001b[1;34m(response, endpoint_name)\u001b[0m\n\u001b[0;32m 473\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m _format(HfHubHTTPError, message, response) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n\u001b[0;32m 475\u001b[0m \u001b[38;5;66;03m# Convert `HTTPError` into a `HfHubHTTPError` to display request information\u001b[39;00m\n\u001b[0;32m 476\u001b[0m \u001b[38;5;66;03m# as well (request id and/or server error message)\u001b[39;00m\n\u001b[1;32m--> 477\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m _format(HfHubHTTPError, \u001b[38;5;28mstr\u001b[39m(e), response) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n",
"\u001b[1;31mHfHubHTTPError\u001b[0m: 402 Client Error: Payment Required for url: https://huggingface.co/api/inference-proxy/sambanova/v1/chat/completions (Request ID: Root=1-67afb729-1eb9aff1704314144ef14e59;2df843ad-b7d2-4145-bb7b-1cfd94ae19ef)\n\nYou have exceeded your monthly included credits for Inference Endpoints. Subscribe to PRO to get 20x more monthly allowance."
]
}
],
"source": [ "source": [
"client = InferenceClient(\n", "client = InferenceClient(\n",
"\tprovider=\"sambanova\",\n", "\tprovider=\"sambanova\",\n",
@ -665,18 +733,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 66, "execution_count": 212,
"id": "a82387d1-7651-4923-995b-fe18356fcaa6", "id": "a82387d1-7651-4923-995b-fe18356fcaa6",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def optimize(python, model):\n", "def optimize(python, model, language):\n",
" if model==\"GPT\":\n", " if model==\"GPT\":\n",
" result = stream_gpt(python)\n", " result = stream_gpt(python, language)\n",
" elif model==\"Claude\":\n", " elif model==\"Claude\":\n",
" result = stream_claude(python)\n", " result = stream_claude(python, language)\n",
" elif model==\"CodeQwen\":\n", " elif model==\"CodeQwen\":\n",
" result = stream_code_qwen(python)\n", " result = stream_code_qwen(python, language)\n",
" else:\n", " else:\n",
" raise ValueError(\"Unknown model\")\n", " raise ValueError(\"Unknown model\")\n",
" for stream_so_far in result:\n", " for stream_so_far in result:\n",
@ -685,7 +753,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 67, "execution_count": 213,
"id": "4ba311ec-c16a-4fe0-946b-4b940704cf65", "id": "4ba311ec-c16a-4fe0-946b-4b940704cf65",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -701,13 +769,13 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 68, "execution_count": 214,
"id": "06148e88-501b-4686-a41d-c3be528d8e6f", "id": "06148e88-501b-4686-a41d-c3be528d8e6f",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def execute_cpp(code):\n", "def execute_cpp(code):\n",
" write_output(code)\n", " write_output(code, \"optimized.exe\")\n",
" try:\n", " try:\n",
" compile_cmd = [\"g++\", \"-Ofast\", \"-std=c++17\", \"-march=native\", \"-mtune=intel\", \"-o\", \"optimized\", \"optimized.cpp\"]\n", " compile_cmd = [\"g++\", \"-Ofast\", \"-std=c++17\", \"-march=native\", \"-mtune=intel\", \"-o\", \"optimized\", \"optimized.cpp\"]\n",
" compile_result = subprocess.run(compile_cmd, check=True, text=True, capture_output=True)\n", " compile_result = subprocess.run(compile_cmd, check=True, text=True, capture_output=True)\n",
@ -720,7 +788,31 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 71, "execution_count": 236,
"id": "a42e3871-f3a5-4f14-836c-1e8ecacb56b5",
"metadata": {},
"outputs": [],
"source": [
"def execute_java(code):\n",
" # Extract the class name from the Java code\n",
" match = re.search(r\"\\b(public\\s+)?class\\s+(\\w+)\", code)\n",
" class_name = match.group(2) if match else \"OptimizedJava\"\n",
"\n",
" file_name = f\"{class_name}.java\"\n",
" write_output(code, file_name)\n",
" try:\n",
" compile_cmd =[\"javac\", file_name]\n",
" subprocess.run(compile_cmd, check=True, text=True, capture_output=True)\n",
" run_cmd = [\"java\", class_name]\n",
" 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\"Error during compilation or execution:\\n{e.stderr}\""
]
},
{
"cell_type": "code",
"execution_count": 238,
"id": "f9ca2e6f-60c1-4e5f-b570-63c75b2d189b", "id": "f9ca2e6f-60c1-4e5f-b570-63c75b2d189b",
"metadata": { "metadata": {
"scrolled": true "scrolled": true
@ -729,7 +821,7 @@
{ {
"data": { "data": {
"text/html": [ "text/html": [
"<div><iframe src=\"http://127.0.0.1:7867/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>" "<div><iframe src=\"http://127.0.0.1:7901/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
], ],
"text/plain": [ "text/plain": [
"<IPython.core.display.HTML object>" "<IPython.core.display.HTML object>"
@ -742,98 +834,60 @@
"data": { "data": {
"text/plain": [] "text/plain": []
}, },
"execution_count": 71, "execution_count": 238,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\huggingface_hub\\utils\\_http.py\", line 406, in hf_raise_for_status\n",
" response.raise_for_status()\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\requests\\models.py\", line 1024, in raise_for_status\n",
" raise HTTPError(http_error_msg, response=self)\n",
"requests.exceptions.HTTPError: 402 Client Error: Payment Required for url: https://huggingface.co/api/inference-proxy/sambanova/v1/chat/completions\n",
"\n",
"The above exception was the direct cause of the following exception:\n",
"\n",
"Traceback (most recent call last):\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\gradio\\queueing.py\", line 625, in process_events\n",
" response = await route_utils.call_process_api(\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\gradio\\route_utils.py\", line 322, in call_process_api\n",
" output = await app.get_blocks().process_api(\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\gradio\\blocks.py\", line 2088, in process_api\n",
" result = await self.call_function(\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\gradio\\blocks.py\", line 1647, in call_function\n",
" prediction = await utils.async_iteration(iterator)\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\gradio\\utils.py\", line 728, in async_iteration\n",
" return await anext(iterator)\n",
" ^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\gradio\\utils.py\", line 722, in __anext__\n",
" return await anyio.to_thread.run_sync(\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\anyio\\to_thread.py\", line 56, in run_sync\n",
" return await get_async_backend().run_sync_in_worker_thread(\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 2505, in run_sync_in_worker_thread\n",
" return await future\n",
" ^^^^^^^^^^^^\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 1005, in run\n",
" result = context.run(func, *args)\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\gradio\\utils.py\", line 705, in run_sync_iterator_async\n",
" return next(iterator)\n",
" ^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\gradio\\utils.py\", line 866, in gen_wrapper\n",
" response = next(iterator)\n",
" ^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\danym\\AppData\\Local\\Temp\\ipykernel_16896\\2223836700.py\", line 10, in optimize\n",
" for stream_so_far in result:\n",
" File \"C:\\Users\\danym\\AppData\\Local\\Temp\\ipykernel_16896\\2217507934.py\", line 8, in stream_code_qwen\n",
" stream = client.chat.completions.create(\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\huggingface_hub\\inference\\_client.py\", line 970, in chat_completion\n",
" data = self._inner_post(request_parameters, stream=stream)\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\huggingface_hub\\inference\\_client.py\", line 327, in _inner_post\n",
" hf_raise_for_status(response)\n",
" File \"C:\\Users\\danym\\anaconda3\\envs\\llms\\Lib\\site-packages\\huggingface_hub\\utils\\_http.py\", line 477, in hf_raise_for_status\n",
" raise _format(HfHubHTTPError, str(e), response) from e\n",
"huggingface_hub.errors.HfHubHTTPError: 402 Client Error: Payment Required for url: https://huggingface.co/api/inference-proxy/sambanova/v1/chat/completions (Request ID: Root=1-67af964d-18ce264b79019ea460d62fd1;041b0bf3-9206-4a8a-aa61-f493ff9b1f8a)\n",
"\n",
"You have exceeded your monthly included credits for Inference Endpoints. Subscribe to PRO to get 20x more monthly allowance.\n"
]
} }
], ],
"source": [ "source": [
"with gr.Blocks(css=css) as ui:\n", "with gr.Blocks(css=css) as ui:\n",
" gr.Markdown(\"## Convert code from Python to C++\")\n", " gr.Markdown(\"## Convert code from Python to C++ or Java\")\n",
" #input and output\n",
" with gr.Row():\n", " with gr.Row():\n",
" python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n", " python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
" cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n", " converted_code = gr.Textbox(label=\"Converted code:\", lines=10)\n",
" # java = gr.Textbox(label=\"Java code:\", lines=10)\n",
" #sample programs\n",
" with gr.Row():\n", " with gr.Row():\n",
" with gr.Column():\n", " with gr.Column():\n",
" sample_program = gr.Radio([\"pi\", \"python_hard\"], label=\"Sample program\", value=\"python_hard\")\n", " sample_program = gr.Radio([\"pi\", \"python_hard\"], label=\"Sample program\", value=\"python_hard\")\n",
" #select model and language\n",
" with gr.Row():\n",
" with gr.Column():\n",
" model = gr.Dropdown([\"GPT\", \"Claude\", \"CodeQwen\"], label=\"Select model\", value=\"GPT\")\n", " model = gr.Dropdown([\"GPT\", \"Claude\", \"CodeQwen\"], label=\"Select model\", value=\"GPT\")\n",
" language = gr.Dropdown([\"C++\",\"Java\"], label=\"Select language\", value=\"C++\")\n",
" with gr.Row():\n", " with gr.Row():\n",
" convert = gr.Button(\"Convert code\")\n", " convert = gr.Button(\"Convert\")\n",
" #Code execution\n",
" with gr.Row():\n", " with gr.Row():\n",
" python_run = gr.Button(\"Run Python\")\n", " python_run = gr.Button(\"Run Python\")\n",
" cpp_run = gr.Button(\"Run C++\")\n", " converted_run = gr.Button(\"Run converted code\")\n",
" with gr.Row():\n", " with gr.Row():\n",
" python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n", " python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
" cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n", " output = gr.TextArea(label=\"Converted code result:\", elem_classes=[\"cpp\"])\n",
" \n",
" # Function to convert Python code based on language\n",
" def convert_code(python_code, model, selected_language):\n",
" if selected_language == \"C++\":\n",
" for chunk in optimize(python_code, model, \"cpp\"):\n",
" yield chunk # Stream each chunk\n",
" elif selected_language == \"Java\":\n",
" for chunk in optimize(python_code, model, \"java\"):\n",
" yield chunk\n",
" return \"\"\n",
"\n",
" # Function to execute converted code\n",
" def run_code(converted_code, selected_language):\n",
" if selected_language == \"C++\":\n",
" return execute_cpp(converted_code)\n",
" elif selected_language == \"Java\":\n",
" return execute_java(converted_code)\n",
" return \"Invalid language selection\"\n",
"\n", "\n",
" sample_program.change(select_sample_program, inputs=[sample_program], outputs=[python])\n", " sample_program.change(select_sample_program, inputs=[sample_program], outputs=[python])\n",
" convert.click(optimize, inputs=[python, model], outputs=[cpp])\n", " convert.click(convert_code, inputs=[python, model, language], outputs=[converted_code])\n",
" converted_run.click(run_code, inputs=[converted_code, language], outputs=[output]) \n",
" python_run.click(execute_python, inputs=[python], outputs=[python_out])\n", " python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
" cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
"\n", "\n",
"ui.launch(inbrowser=True)" "ui.launch(inbrowser=True)"
] ]

Loading…
Cancel
Save