Spaces:
Sleeping
Sleeping
Uploaded source files
Browse filesFiles changed (4)
- .gitignoreAdded+174
- app.pyAdded+290
- requirements.txtAdded+8
- squares.plAdded+34
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174# Byte-compiled / optimized / DLL files__pycache__/*.py[cod]*$py.class
# C extensions*.so
# Distribution / packaging.Pythonbuild/develop-eggs/dist/downloads/eggs/.eggs/lib/lib64/parts/sdist/var/wheels/share/python-wheels/*.egg-info/.installed.cfg*.eggMANIFEST
# PyInstaller# Usually these files are written by a python script from a template# before PyInstaller builds the exe, so as to inject date/other infos into it.*.manifest*.spec
# Installer logspip-log.txtpip-delete-this-directory.txt
# Unit test / coverage reportshtmlcov/.tox/.nox/.coverage.coverage.*.cachenosetests.xmlcoverage.xml*.cover*.py,cover.hypothesis/.pytest_cache/cover/
# Translations*.mo*.pot
# Django stuff:*.loglocal_settings.pydb.sqlite3db.sqlite3-journal
# Flask stuff:instance/.webassets-cache
# Scrapy stuff:.scrapy
# Sphinx documentationdocs/_build/
# PyBuilder.pybuilder/target/
# Jupyter Notebook.ipynb_checkpoints
# IPythonprofile_default/ipython_config.py
# pyenv# For a library or package, you might want to ignore these files since the code is# intended to run in multiple environments; otherwise, check them in:# .python-version
# pipenv# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.# However, in case of collaboration, if having platform-specific dependencies or dependencies# having no cross-platform support, pipenv may install dependencies that don't work, or not# install all needed dependencies.#Pipfile.lock
# UV# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.# This is especially recommended for binary packages to ensure reproducibility, and is more# commonly ignored for libraries.#uv.lock
# poetry# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.# This is especially recommended for binary packages to ensure reproducibility, and is more# commonly ignored for libraries.# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control#poetry.lock
# pdm# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.#pdm.lock# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it# in version control.# https://pdm.fming.dev/latest/usage/project/#working-with-version-control.pdm.toml.pdm-python.pdm-build/
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm__pypackages__/
# Celery stuffcelerybeat-schedulecelerybeat.pid
# SageMath parsed files*.sage.py
# Environments.env.venvenv/venv/ENV/env.bak/venv.bak/
# Spyder project settings.spyderproject.spyproject
# Rope project settings.ropeproject
# mkdocs documentation/site
# mypy.mypy_cache/.dmypy.jsondmypy.json
# Pyre type checker.pyre/
# pytype static type analyzer.pytype/
# Cython debug symbolscython_debug/
# PyCharm# JetBrains specific template is maintained in a separate JetBrains.gitignore that can# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore# and can be added to the global gitignore or merged into this file. For a more nuclear# option (not recommended) you can uncomment the following to ignore the entire idea folder.#.idea/
# Ruff stuff:.ruff_cache/
# PyPI configuration file.pypirc123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290#!/usr/bin/env python# coding: utf-8
# # Perl to Python Code Generator# # The requirement: use a Frontier model to generate high performance Python code from Perl code#
# In[ ]:
#get_ipython().system('pip install -q transformers huggingface_hub')
# In[ ]:
# imports
import osimport ioimport sysfrom dotenv import load_dotenvfrom openai import OpenAIimport google.generativeaiimport anthropicfrom IPython.display import Markdown, display, update_displayimport gradio as grimport subprocessimport json#for Hugging face end pointsfrom huggingface_hub import login, InferenceClientfrom transformers import AutoTokenizer
# In[ ]:
# environment
load_dotenv(override=True)os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')##for connecting to HF End pointhf_token = os.environ['HF_TOKEN']login(hf_token, add_to_git_credential=True)
# In[ ]:
# initialize# NOTE - option to use ultra-low cost models by uncommenting last 2 lines
openai = OpenAI()claude = anthropic.Anthropic()OPENAI_MODEL = "gpt-4o"CLAUDE_MODEL = "claude-3-5-sonnet-20240620"
# Want to keep costs ultra-low? Uncomment these lines:#OPENAI_MODEL = "gpt-4o-mini"#CLAUDE_MODEL = "claude-3-haiku-20240307"
#To access open source models from Hugging face end pointscode_qwen = "Qwen/CodeQwen1.5-7B-Chat"code_gemma = "google/codegemma-7b-it"CODE_QWEN_URL = "https://u9pv0u7a6uxrjubt.us-east-1.aws.endpoints.huggingface.cloud" #Paste your end point URL from Hugging faceCODE_GEMMA_URL = "https://c5hggiyqachmgnqg.us-east-1.aws.endpoints.huggingface.cloud" #Paste your end point URL from Hugging face
# In[ ]:
system_message = "You are an assistant that reimplements Perl scripts code into a high performance Python for a Windows 11 PC. "system_message += "Respond only with Python code; use comments sparingly and do not provide any explanation other than occasional comments preceded by a # tag."system_message += "The Python response needs to produce an identical output in the fastest possible time."
# In[ ]:
def user_prompt_for(perl): user_prompt = "Rewrite this Perl scripts code in Python with the fastest possible implementation that produces identical output in the least time. " user_prompt += "Respond only with Python 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 python libraries as needed,\ such as requests, os, json etc.\n\n" user_prompt += perl return user_prompt
# In[ ]:
def messages_for(perl): return [ {"role": "system", "content": system_message}, {"role": "user", "content": user_prompt_for(perl)} ]
# In[ ]:
# write to a file
def write_output(python, file_path): # Get the base filename from the path base_name = os.path.basename(file_path) filename = os.path.splitext(base_name)[0] code = python.replace("```python","").replace("```","") output_file = f"{filename}.py" with open(output_file, "w") as f: f.write(code) return output_file
# In[ ]:
def stream_gpt(perl, file_path): stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(perl), stream=True) reply = "" for chunk in stream: fragment = chunk.choices[0].delta.content or "" reply += fragment cleaned_reply = reply.replace('```python\n','').replace('```','') yield cleaned_reply, None yield cleaned_reply, write_output(cleaned_reply, file_path)
# In[ ]:
def stream_claude(perl, file_path): result = claude.messages.stream( model=CLAUDE_MODEL, max_tokens=2000, system=system_message, messages=[{"role": "user", "content": user_prompt_for(perl)}], ) reply = "" with result as stream: for text in stream.text_stream: reply += text cleaned_reply = reply.replace('```python\n','').replace('```','') yield cleaned_reply, None yield cleaned_reply, write_output(cleaned_reply, file_path)
# In[ ]:
def stream_code_qwen(perl, file_path): tokenizer = AutoTokenizer.from_pretrained(code_qwen) messages = messages_for(perl) text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) client = InferenceClient(CODE_QWEN_URL, token=hf_token) stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000) result = "" for r in stream: result += r.token.text cleaned_reply = result.replace('```python\n','').replace('```','') yield cleaned_reply, None yield cleaned_reply, write_output(cleaned_reply, file_path)
# In[ ]:
def generate(perl_script, model, file_path): if model=="GPT": for result, file in stream_gpt(perl_script, file_path): yield result, file yield result, file elif model=="Claude": for result, file in stream_claude(perl_script, file_path): yield result, file yield result, file elif model=="CodeQwen": for result, file in stream_code_qwen(perl_script, file_path): yield result, file yield result, file else: raise ValueError("Unknown model")
# In[ ]:
def execute_perl(perl_code):
import subprocess #print(perl_file) #perl_path = r"E:\Softwares\Perl\perl\bin\perl.exe" # Run Perl script from Jupyter Lab result = subprocess.run(["perl", '-e', perl_code], capture_output=True, text=True)
# Return the output of the Perl script return result.stdout
# In[ ]:
def execute_python(code): try: output = io.StringIO() sys.stdout = output exec(code) finally: sys.stdout = sys.__stdout__ return output.getvalue()
# In[ ]:
css = """.perl {background-color: #093645;}.python {background-color: #0948;}"""
force_dark_mode = """function refresh() { const url = new URL(window.location); if (url.searchParams.get('__theme') !== 'dark') { url.searchParams.set('__theme', 'dark'); window.location.href = url.href; }}"""
# In[ ]:
with gr.Blocks(css=css, js=force_dark_mode) as ui:
gr.HTML("<h2 style='text-align: center; color: white;'> PERL to Python Code Generator</h2>") with gr.Row(scale=0, equal_height=True): model = gr.Dropdown(["GPT", "Claude", "CodeQwen"], label="Select model", value="GPT") perl_file = gr.File(label="Upload Perl Script:") convert = gr.Button("Convert to Python") file_output = gr.File(label="Download Python script", visible=False) with gr.Row(): perl_script = gr.Textbox(label="Perl Script:") python_script = gr.Textbox(label="Converted Python Script:") with gr.Row(): perl_run = gr.Button("Run PERL") python_run = gr.Button("Run Python") with gr.Row(): perl_out = gr.TextArea(label="PERL Result:", elem_classes=["perl"]) python_out = gr.TextArea(label="Python Result:", elem_classes=["python"]) with gr.Row(): clear_button = gr.Button("Clear") def extract_perl_code(file): if file is None: return "No file uploaded." with open(file.name, "r", encoding="utf-8") as f: perl_code = f.read() return perl_code
convert.click(extract_perl_code, inputs=[perl_file], outputs=[perl_script]).then( generate, inputs=[perl_script, model, perl_file], outputs=[python_script, file_output]).then( lambda file_output: gr.update(visible=True), inputs=[file_output], outputs=[file_output] )
perl_run.click(execute_perl, inputs=[perl_script], outputs=[perl_out]) python_run.click(execute_python, inputs=[python_script], outputs=[python_out])
def clear_all(): return None, "", "", gr.update(visible=False), "", ""
clear_button.click( clear_all, outputs=[perl_file, perl_script, python_script, file_output, perl_out, python_out] )
ui.launch(inbrowser=True)
# In[ ]:
12345678openaigoogle-generativeaianthropicgradiopython-dotenvtransformershuggingface_hub
12345678910111213141516171819202122232425262728293031323334use Time::HiRes qw(time); # More precise timing
$start_time = time();
@numbers = (1, 2, 3, 4, 5);@squares = ();
$i = 0;while ($i < scalar(@numbers)) { $num = $numbers[$i];
$square = 0; $k = 0; while ($k < $num) { $square = $square + $num; $k = $k + 1; }
push(@squares, $square); $i = $i + 1;}
$j = 0;print "Squares:\n";while ($j < scalar(@squares)) { print $squares[$j], "\n"; $j = $j + 1;}
$end_time = time();$duration = $end_time - $start_time;
# Print with 6 decimal placesprintf("Execution Time: %.6f seconds\n", $duration);.gitignoreAdded+174
app.pyAdded+290
requirements.txtAdded+8
squares.plAdded+34