Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…_team5 into dev-be
  • Loading branch information
yaongmeow committed Jun 9, 2024
2 parents 9941316 + 50fb43d commit 8078d7b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
37 changes: 22 additions & 15 deletions BE/green_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import date
import subprocess
import time
import re

PUE=1.67
PSF=1.0
Expand All @@ -28,39 +29,46 @@ def get_LLM_response(code_data: str):
"content": [
{
"type": "text",
"text": '''The code I'm currently providing doesn't take into account the carbon footprint of running the code.
For example, meaningless loops increase the execution time of the code, which increases the time it takes to perform the code,
which increases the carbon footprint. Please convert the code I've provided below to code that has the same functionality but minimizes the carbon footprint.
Give me only code. Don't say anything else. \n\n''' + code_data

"text": '''"The code provided below has unnecessarily long execution time.
It can be optimized to reduce the execution time without changing the output.
Please optimize the code to make it more efficient while keeping the output the same.
Just provide the optimized code without any additional explanation." \n\n''' + code_data
},
]
}
],
"max_tokens": 500
"max_tokens": 1000,
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
point = response.json()["choices"][0]["message"]["content"]
return point


def execute_java_code(code: str):
with open("TempJavaProgram.java", "w") as file:
match = re.search(r'public\s+class\s+(\w+)', code)
if not match:
class_name = "NoClassName"
elif match:
class_name = match.group(1)

with open(class_name + ".java", "w") as file:
file.write(code)
compile_process = subprocess.run(["javac", "TempJavaProgram.java"], capture_output=True, text=True)
compile_process = subprocess.run(["javac", class_name+".java"], capture_output=True, text=True)
if compile_process.returncode != 0:
return {"error": "Compilation Failed", "details": compile_process.stderr}
print('Compilation Failed')
return ("Compilation Failed", compile_process.stderr)
start_time = time.time()
execute_process = subprocess.run(["java", "TempJavaProgram"], capture_output=True, text=True)
execute_process = subprocess.run(["java", class_name], capture_output=True, text=True)
end_time = time.time()

if execute_process.returncode != 0:
return {"error": "Execution Failed", "details": execute_process.stderr}
print('Execution Failed')
return ("Execution Failed", execute_process.stderr)

execution_time = end_time - start_time

os.remove("TempJavaProgram.java")
os.remove("TempJavaProgram.class")
os.remove(class_name+".java")
os.remove(class_name+".class")
return (execute_process.stdout, execution_time)


Expand All @@ -75,8 +83,7 @@ def calculate_carbon_footprint(runtime: float):


class RequestModel(BaseModel):
session: str
data: str
code: str


class FixedCode(BaseModel):
Expand Down
21 changes: 19 additions & 2 deletions BE/routes/green.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@ async def get_green(request: RequestModel):
@green_router.post("/codes")
async def process_code(request: CodeCreateRequest, request1: Request):
conn, cur = create_session()
token = request1.headers.get('Bearer')
message, user = jwt_decoder(token, os.environ.get('JWT_SECRET_KEY_ACCESS'))
#token = request1.headers.get('Bearer')
#message, user = jwt_decoder(token, os.environ.get('JWT_SECRET_KEY_ACCESS'))
#user_id = user.get('user_id')
#user_id = uuid.uuid4()
#print(user_id)
auth_header = request1.headers.get('Authorization')
access_token = auth_header.split(' ')[1]
message, user = jwt_decoder(access_token, os.environ.get('JWT_SECRET_KEY_ACCESS'))
user_id = user.get('user_id')
#user_id = '2309f0e1-c49d-49df-81e5-dee742fcda85'
original_code = request.original_code
merged_code = request.merged_code
code_id = uuid.uuid4()
Expand All @@ -40,7 +47,17 @@ async def process_code(request: CodeCreateRequest, request1: Request):
conn.commit()

stdout, original_execution_time = execute_java_code(original_code)
if stdout == 'Compilation Failed':
return {"message": "Compilation Failed at original code", 'detail': original_execution_time}
elif stdout == 'Execution Failed':
return {"message": "Execution Failed at original code", 'detail': original_execution_time}

stdout1, merged_execution_time = execute_java_code(merged_code)
if stdout == 'Compilation Failed':
return {"message": "Compilation Failed at merged code", 'detail': original_execution_time}
elif stdout == 'Execution Failed':
return {"message": "Execution Failed at merged code", 'detail': original_execution_time}

original_fp = calculate_carbon_footprint(original_execution_time)
merged_fp = calculate_carbon_footprint(merged_execution_time)

Expand Down

0 comments on commit 8078d7b

Please sign in to comment.