-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleprocess.py
More file actions
180 lines (167 loc) · 6.55 KB
/
singleprocess.py
File metadata and controls
180 lines (167 loc) · 6.55 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import os
import sys
import random
import string
import subprocess
from openai import OpenAI
APIKey = "API-KEY"
APIUrl = "https://api.deepseek.com"
AIModel = "deepseek-chat"
def DeepseekAPI(input, instructions, api_key = APIKey, base_url = APIUrl, model = AIModel):
client = OpenAI(api_key = api_key, base_url = base_url)
response = client.chat.completions.create(
model = model,
messages=[
{"role": "system", "content": instructions},
{"role": "user", "content": input},
],
stream=False
)
APIResponse = response.choices[0].message.content
return APIResponse
def PromptRefining(input):
promptinstructions = "Rewrite the given input as the perfect ai-ready prompt to obtain the maximum of prompt-engeneering, you have to write no code, but in the prompt ask for full code in one single python code, just the roadmap for another ai, do not add your extenal text, just a ready copy and paste full detailed prompt."
PromptResponse = DeepseekAPI(input, promptinstructions)
return PromptResponse
def CodeLibSeparate(text):
lines = text.split('\n')
state = None
current_block = []
code = None
libs = None
for line in lines:
stripped = line.strip()
if stripped.startswith('```'):
marker = stripped[3:].strip()
if marker.startswith('python') and not code:
state = 'python'
current_block = []
elif marker.startswith('bash') and not libs:
state = 'bash'
current_block = []
elif state:
if state == 'python' and not code:
code = '\n'.join(current_block)
elif state == 'bash' and not libs:
if any(l.strip().startswith('pip install') for l in current_block):
libs = '\n'.join(current_block)
state = None
continue
if state:
current_block.append(line)
return code, libs
def CodeDevelop(input):
codeinput = PromptRefining(input)
codeinstructions = "Write the code, first give the code in the form of main.py and then code, then give the pip install requirements in the form of pip install *** in a separated codeblock, avoid introductions or everything else than code and libs"
codeandlibs = DeepseekAPI(input, codeinstructions)
code, libs = CodeLibSeparate(codeandlibs)
return code, libs
def rand(lunghezza=10):
caratteri = string.ascii_letters + string.digits
return ''.join(random.choice(caratteri) for _ in range(lunghezza))
def Runner(code, libs):
folder = rand()
path = os.path.join(os.getcwd(), folder)
os.makedirs(path, exist_ok=True)
os.chdir(folder)
filename = rand()+".py"
with open(filename, 'w', encoding='utf-8') as f:
f.write(code)
subprocess.run([sys.executable, "-m", "venv", "venv"])
log_file = "log.txt"
command = f"""
source venv/bin/activate &&
{libs} &&
python {filename}
"""
with open(log_file, "w") as log:
result = subprocess.run(
command,
shell=True,
executable="/bin/zsh",
check=False,
text=True,
stdout=log,
stderr=subprocess.STDOUT
)
with open(log_file, "a") as log:
log.write(f"\n\nProcesso terminato con codice: {result.returncode}")
filewrite = open(log_file, 'r', encoding='utf-8')
logresult = filewrite.read()
filewrite.close()
status = 0
if result.returncode == 0:
status = 200
elif result.returncode == 1:
status = 400
else:
status = 5000
return status, logresult, filename, code, libs
def FixRunner(fixedcode, fixedlibs, count):
mid = rand()
countn = str(count)
filename = countn + "__" + "temp" + mid + ".py"
with open(filename, 'w', encoding='utf-8') as f:
f.write(fixedcode)
subprocess.run([sys.executable, "-m", "venv", "venv"])
log_file = "temp" + mid + "_log.txt"
command = f"""
{fixedlibs} &&
python {filename}
"""
with open(log_file, "w") as log:
result = subprocess.run(
command,
shell=True,
executable="/bin/zsh",
check=False,
text=True,
stdout=log,
stderr=subprocess.STDOUT
)
with open(log_file, "a") as log:
log.write(f"\n\nProcesso terminato con codice: {result.returncode}")
status = 0
if result.returncode == 0:
status = 200
elif result.returncode == 1:
status = 400
else:
status = 5000
return status
def CodeFixer(logresult, filename, code, libs):
status = 400
count = 0
while status == 400:
prompt = code + "\n" + libs + "\n" + logresult + "\n GIVE BACK FULL CODE UPDATED"
instructions = "Fix it and give back full code updated. Give the code in the form of main.py and then code, then give the pip install requirements in the form of pip install *** in a separated codeblock, avoid introductions or everything else than code and libs"
fixedcode, fixedlibs = CodeLibSeparate(DeepseekAPI(prompt, instructions))
status = FixRunner(fixedcode, fixedlibs, count)
count = count + 1
if count > 3:
instructions2 = "REWRITE COMPLETELY THE CODE CHANGING APROACH. Give the code in the form of main.py and then code, then give the pip install requirements in the form of pip install *** in a separated codeblock, avoid introductions or everything else than code and libs"
fixedcode, fixedlibs = CodeLibSeparate(DeepseekAPI(prompt, instructions2))
status = FixRunner(fixedcode, fixedlibs, count)
if status == 200:
with open(filename, 'w', encoding='utf-8') as f:
f.write(fixedcode)
return fixedcode
def WriteMD(code):
input = code
instructions = "write a GitHub ready MD file of the code. Give back only the readme file. DO NOT INCLUDE CODE FROM THE PYTHON CODE PROVIDED. Exclude licence and user references like github-usernames and github-profiles-links etc."
TextMD = DeepseekAPI(input, instructions)
filename = "README.md"
with open(filename, 'w', encoding='utf-8') as f:
f.write(TextMD)
def Run():
userinput = input("Prompt: ")
code, libs = CodeDevelop(userinput)
status, logresult, filename, code, libs = Runner(code, libs)
if status == 400:
fixedcode = CodeFixer(logresult, filename, code, libs)
WriteMD(fixedcode)
if status == 200:
WriteMD(code)
return
if __name__ == "__main__":
Run()