Samhugs07 commited on
Commit
651ddf8
·
1 Parent(s): 335a11f

Uploaded source files

Browse files
Files changed (4) hide show
  1. .gitignore +174 -0
  2. app.py +290 -0
  3. requirements.txt +8 -0
  4. squares.pl +34 -0
.gitignore ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+
110
+ # pdm
111
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112
+ #pdm.lock
113
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114
+ # in version control.
115
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
116
+ .pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121
+ __pypackages__/
122
+
123
+ # Celery stuff
124
+ celerybeat-schedule
125
+ celerybeat.pid
126
+
127
+ # SageMath parsed files
128
+ *.sage.py
129
+
130
+ # Environments
131
+ .env
132
+ .venv
133
+ env/
134
+ venv/
135
+ ENV/
136
+ env.bak/
137
+ venv.bak/
138
+
139
+ # Spyder project settings
140
+ .spyderproject
141
+ .spyproject
142
+
143
+ # Rope project settings
144
+ .ropeproject
145
+
146
+ # mkdocs documentation
147
+ /site
148
+
149
+ # mypy
150
+ .mypy_cache/
151
+ .dmypy.json
152
+ dmypy.json
153
+
154
+ # Pyre type checker
155
+ .pyre/
156
+
157
+ # pytype static type analyzer
158
+ .pytype/
159
+
160
+ # Cython debug symbols
161
+ cython_debug/
162
+
163
+ # PyCharm
164
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
165
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
167
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
168
+ #.idea/
169
+
170
+ # Ruff stuff:
171
+ .ruff_cache/
172
+
173
+ # PyPI configuration file
174
+ .pypirc
app.py ADDED
@@ -0,0 +1,290 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # # Perl to Python Code Generator
5
+ #
6
+ # The requirement: use a Frontier model to generate high performance Python code from Perl code
7
+ #
8
+
9
+ # In[ ]:
10
+
11
+
12
+ #get_ipython().system('pip install -q transformers huggingface_hub')
13
+
14
+
15
+ # In[ ]:
16
+
17
+
18
+ # imports
19
+
20
+ import os
21
+ import io
22
+ import sys
23
+ from dotenv import load_dotenv
24
+ from openai import OpenAI
25
+ import google.generativeai
26
+ import anthropic
27
+ from IPython.display import Markdown, display, update_display
28
+ import gradio as gr
29
+ import subprocess
30
+ import json
31
+ #for Hugging face end points
32
+ from huggingface_hub import login, InferenceClient
33
+ from transformers import AutoTokenizer
34
+
35
+
36
+ # In[ ]:
37
+
38
+
39
+ # environment
40
+
41
+ load_dotenv(override=True)
42
+ os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')
43
+ os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')
44
+ os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')
45
+ ##for connecting to HF End point
46
+ hf_token = os.environ['HF_TOKEN']
47
+ login(hf_token, add_to_git_credential=True)
48
+
49
+
50
+ # In[ ]:
51
+
52
+
53
+ # initialize
54
+ # NOTE - option to use ultra-low cost models by uncommenting last 2 lines
55
+
56
+ openai = OpenAI()
57
+ claude = anthropic.Anthropic()
58
+ OPENAI_MODEL = "gpt-4o"
59
+ CLAUDE_MODEL = "claude-3-5-sonnet-20240620"
60
+
61
+ # Want to keep costs ultra-low? Uncomment these lines:
62
+ #OPENAI_MODEL = "gpt-4o-mini"
63
+ #CLAUDE_MODEL = "claude-3-haiku-20240307"
64
+
65
+ #To access open source models from Hugging face end points
66
+ code_qwen = "Qwen/CodeQwen1.5-7B-Chat"
67
+ code_gemma = "google/codegemma-7b-it"
68
+ CODE_QWEN_URL = "https://u9pv0u7a6uxrjubt.us-east-1.aws.endpoints.huggingface.cloud" #Paste your end point URL from Hugging face
69
+ CODE_GEMMA_URL = "https://c5hggiyqachmgnqg.us-east-1.aws.endpoints.huggingface.cloud" #Paste your end point URL from Hugging face
70
+
71
+
72
+ # In[ ]:
73
+
74
+
75
+ system_message = "You are an assistant that reimplements Perl scripts code into a high performance Python for a Windows 11 PC. "
76
+ system_message += "Respond only with Python code; use comments sparingly and do not provide any explanation other than occasional comments preceded by a # tag."
77
+ system_message += "The Python response needs to produce an identical output in the fastest possible time."
78
+
79
+
80
+ # In[ ]:
81
+
82
+
83
+ def user_prompt_for(perl):
84
+ user_prompt = "Rewrite this Perl scripts code in Python with the fastest possible implementation that produces identical output in the least time. "
85
+ user_prompt += "Respond only with Python code; do not explain your work other than a few comments. "
86
+ user_prompt += "Pay attention to number types to ensure no int overflows. Remember to #include all necessary python libraries as needed,\
87
+ such as requests, os, json etc.\n\n"
88
+ user_prompt += perl
89
+ return user_prompt
90
+
91
+
92
+ # In[ ]:
93
+
94
+
95
+ def messages_for(perl):
96
+ return [
97
+ {"role": "system", "content": system_message},
98
+ {"role": "user", "content": user_prompt_for(perl)}
99
+ ]
100
+
101
+
102
+ # In[ ]:
103
+
104
+
105
+ # write to a file
106
+
107
+ def write_output(python, file_path):
108
+ # Get the base filename from the path
109
+ base_name = os.path.basename(file_path)
110
+ filename = os.path.splitext(base_name)[0]
111
+ code = python.replace("```python","").replace("```","")
112
+ output_file = f"{filename}.py"
113
+ with open(output_file, "w") as f:
114
+ f.write(code)
115
+ return output_file
116
+
117
+
118
+ # In[ ]:
119
+
120
+
121
+ def stream_gpt(perl, file_path):
122
+ stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(perl), stream=True)
123
+ reply = ""
124
+ for chunk in stream:
125
+ fragment = chunk.choices[0].delta.content or ""
126
+ reply += fragment
127
+ cleaned_reply = reply.replace('```python\n','').replace('```','')
128
+ yield cleaned_reply, None
129
+ yield cleaned_reply, write_output(cleaned_reply, file_path)
130
+
131
+
132
+
133
+ # In[ ]:
134
+
135
+
136
+ def stream_claude(perl, file_path):
137
+ result = claude.messages.stream(
138
+ model=CLAUDE_MODEL,
139
+ max_tokens=2000,
140
+ system=system_message,
141
+ messages=[{"role": "user", "content": user_prompt_for(perl)}],
142
+ )
143
+ reply = ""
144
+ with result as stream:
145
+ for text in stream.text_stream:
146
+ reply += text
147
+ cleaned_reply = reply.replace('```python\n','').replace('```','')
148
+ yield cleaned_reply, None
149
+ yield cleaned_reply, write_output(cleaned_reply, file_path)
150
+
151
+
152
+ # In[ ]:
153
+
154
+
155
+ def stream_code_qwen(perl, file_path):
156
+ tokenizer = AutoTokenizer.from_pretrained(code_qwen)
157
+ messages = messages_for(perl)
158
+ text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
159
+ client = InferenceClient(CODE_QWEN_URL, token=hf_token)
160
+ stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)
161
+ result = ""
162
+ for r in stream:
163
+ result += r.token.text
164
+ cleaned_reply = result.replace('```python\n','').replace('```','')
165
+ yield cleaned_reply, None
166
+ yield cleaned_reply, write_output(cleaned_reply, file_path)
167
+
168
+
169
+ # In[ ]:
170
+
171
+
172
+ def generate(perl_script, model, file_path):
173
+ if model=="GPT":
174
+ for result, file in stream_gpt(perl_script, file_path):
175
+ yield result, file
176
+ yield result, file
177
+ elif model=="Claude":
178
+ for result, file in stream_claude(perl_script, file_path):
179
+ yield result, file
180
+ yield result, file
181
+ elif model=="CodeQwen":
182
+ for result, file in stream_code_qwen(perl_script, file_path):
183
+ yield result, file
184
+ yield result, file
185
+ else:
186
+ raise ValueError("Unknown model")
187
+
188
+
189
+ # In[ ]:
190
+
191
+
192
+ def execute_perl(perl_code):
193
+
194
+ import subprocess
195
+ #print(perl_file)
196
+ #perl_path = r"E:\Softwares\Perl\perl\bin\perl.exe"
197
+ # Run Perl script from Jupyter Lab
198
+ result = subprocess.run(["perl", '-e', perl_code], capture_output=True, text=True)
199
+
200
+ # Return the output of the Perl script
201
+ return result.stdout
202
+
203
+
204
+
205
+ # In[ ]:
206
+
207
+
208
+ def execute_python(code):
209
+ try:
210
+ output = io.StringIO()
211
+ sys.stdout = output
212
+ exec(code)
213
+ finally:
214
+ sys.stdout = sys.__stdout__
215
+ return output.getvalue()
216
+
217
+
218
+ # In[ ]:
219
+
220
+
221
+ css = """
222
+ .perl {background-color: #093645;}
223
+ .python {background-color: #0948;}
224
+ """
225
+
226
+ force_dark_mode = """
227
+ function refresh() {
228
+ const url = new URL(window.location);
229
+ if (url.searchParams.get('__theme') !== 'dark') {
230
+ url.searchParams.set('__theme', 'dark');
231
+ window.location.href = url.href;
232
+ }
233
+ }
234
+ """
235
+
236
+
237
+ # In[ ]:
238
+
239
+
240
+ with gr.Blocks(css=css, js=force_dark_mode) as ui:
241
+
242
+ gr.HTML("<h2 style='text-align: center; color: white;'> PERL to Python Code Generator</h2>")
243
+ with gr.Row(scale=0, equal_height=True):
244
+ model = gr.Dropdown(["GPT", "Claude", "CodeQwen"], label="Select model", value="GPT")
245
+ perl_file = gr.File(label="Upload Perl Script:")
246
+ convert = gr.Button("Convert to Python")
247
+ file_output = gr.File(label="Download Python script", visible=False)
248
+ with gr.Row():
249
+ perl_script = gr.Textbox(label="Perl Script:")
250
+ python_script = gr.Textbox(label="Converted Python Script:")
251
+ with gr.Row():
252
+ perl_run = gr.Button("Run PERL")
253
+ python_run = gr.Button("Run Python")
254
+ with gr.Row():
255
+ perl_out = gr.TextArea(label="PERL Result:", elem_classes=["perl"])
256
+ python_out = gr.TextArea(label="Python Result:", elem_classes=["python"])
257
+ with gr.Row():
258
+ clear_button = gr.Button("Clear")
259
+
260
+ def extract_perl_code(file):
261
+ if file is None:
262
+ return "No file uploaded."
263
+ with open(file.name, "r", encoding="utf-8") as f:
264
+ perl_code = f.read()
265
+ return perl_code
266
+
267
+ convert.click(extract_perl_code, inputs=[perl_file], outputs=[perl_script]).then(
268
+ generate, inputs=[perl_script, model, perl_file], outputs=[python_script, file_output]).then(
269
+ lambda file_output: gr.update(visible=True), inputs=[file_output], outputs=[file_output]
270
+ )
271
+
272
+ perl_run.click(execute_perl, inputs=[perl_script], outputs=[perl_out])
273
+ python_run.click(execute_python, inputs=[python_script], outputs=[python_out])
274
+
275
+ def clear_all():
276
+ return None, "", "", gr.update(visible=False), "", ""
277
+
278
+ clear_button.click(
279
+ clear_all,
280
+ outputs=[perl_file, perl_script, python_script, file_output, perl_out, python_out]
281
+ )
282
+
283
+ ui.launch(inbrowser=True)
284
+
285
+
286
+ # In[ ]:
287
+
288
+
289
+
290
+
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ openai
2
+ google-generativeai
3
+ anthropic
4
+ gradio
5
+ python-dotenv
6
+ transformers
7
+ huggingface_hub
8
+
squares.pl ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ use Time::HiRes qw(time); # More precise timing
2
+
3
+ $start_time = time();
4
+
5
+ @numbers = (1, 2, 3, 4, 5);
6
+ @squares = ();
7
+
8
+ $i = 0;
9
+ while ($i < scalar(@numbers)) {
10
+ $num = $numbers[$i];
11
+
12
+ $square = 0;
13
+ $k = 0;
14
+ while ($k < $num) {
15
+ $square = $square + $num;
16
+ $k = $k + 1;
17
+ }
18
+
19
+ push(@squares, $square);
20
+ $i = $i + 1;
21
+ }
22
+
23
+ $j = 0;
24
+ print "Squares:\n";
25
+ while ($j < scalar(@squares)) {
26
+ print $squares[$j], "\n";
27
+ $j = $j + 1;
28
+ }
29
+
30
+ $end_time = time();
31
+ $duration = $end_time - $start_time;
32
+
33
+ # Print with 6 decimal places
34
+ printf("Execution Time: %.6f seconds\n", $duration);