-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheCodeMachine.py
67 lines (53 loc) · 1.69 KB
/
TheCodeMachine.py
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
import re
import ollama
# Initialize the Ollama client
client = ollama.Client()
# Read the Java source file
with open('D:/TheCodeMachine/HelloWorld.java', 'r') as file:
java_code = file.read()
language_extensions = {
"Cobol":"cbl",
"RPG iSeries RPG (Report Program Generator) on IBM iSeries (AS/400)":"RPG",
"Python": "py",
"Dart": "dart",
"JavaScript": "js",
"C": "c",
"C++": "cpp",
"C#": "cs",
"Ruby": "rb",
"Go": "go",
"Swift": "swift",
"Kotlin": "kt",
"PHP": "php",
"HTML": "html",
"CSS": "css",
"TypeScript": "ts",
"R": "r",
"Perl": "pl",
"Scala": "scala",
"Rust": "rs",
"Objective-C": "m",
"Visual Basic": "vb",
"Pascal": "pas",
}
# Iterate over the dictionary
for language, extension in language_extensions.items():
print(f"The file extension for {language} is .{extension}")
# Define the prompt for the model
prompt = f"Convert the following Java code to {language}:\n\n{java_code}"
# Call the Qwen2.5-Coder:32B model
response = client.generate(model="qwen2.5-coder:32b", prompt=prompt)
# Convert the response to a string if it's not already
response_str = str(response)
# Use regular expressions to find code blocks
code_blocks = re.findall(r'```(.*?)```', response_str, re.DOTALL)
# Join the extracted code blocks
extracted_code = "\n\n".join(code_blocks).replace("\\n", "\n")
rows=extracted_code.split("\n")
rows=rows[1:]
extracted_code="\n\n".join(rows)
print(extracted_code)
# Write the Dart code to a file
with open(f'D:/TheCodeMachine/HelloWorld.{extension}', 'w') as file:
file.write(extracted_code)
print("Conversion completed!")