Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ss_team5 into jeongmin
  • Loading branch information
yaongmeow committed Jun 7, 2024
2 parents 95020b0 + 101d112 commit 4ee7e70
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 26 deletions.
55 changes: 54 additions & 1 deletion BE/green_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
import openai
import requests
import os
from datetime import date
import subprocess
import time

PUE=1.67
PSF=1.0
CARBON_INTENSITY=500
USAGE = 1
POWER_DRAW_FOR_CPU = 12 # Any
POWER_DRAW_FOR_MEMORY_PER_GB = 0.3725 # how many GBs??


def get_LLM_response(code_data: str):
api_key = 'api key'
Expand Down Expand Up @@ -32,7 +43,37 @@ def get_LLM_response(code_data: str):
point = response.json()["choices"][0]["message"]["content"]
return point



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

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

execution_time = end_time - start_time

os.remove("TempJavaProgram.java")
os.remove("TempJavaProgram.class")
return {
"output": execute_process.stdout,
"execution_time": execution_time
}


def calculate_carbon_footprint(runtime: float):
energy_needed = runtime * (POWER_DRAW_FOR_CPU * USAGE + POWER_DRAW_FOR_MEMORY_PER_GB ) * PUE * PSF
carbon_footprint = energy_needed * CARBON_INTENSITY
return carbon_footprint




class RequestModel(BaseModel):
session: str
Expand All @@ -43,3 +84,15 @@ class FixedCode(BaseModel):
id: str
fixed_code: str


class CodeCreateRequest(BaseModel):
original_code: str
merged_code: str

class CodeResponse(BaseModel):
id: str
original_code: str
merged_code: str
original_fp: float
merged_fp: float
date: date
41 changes: 16 additions & 25 deletions BE/routes/green.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,34 @@
from fastapi import APIRouter
from green_function import *
#import pymysql
from database import create_session

import pymysql
import subprocess
import os
import time

green_router = APIRouter(
tags=["Green"],
)


# conn = pymysql.connect(host = 'localhost', user='wonyeong', password = 'your_password', db = 'sample', charset = 'utf8')
# cur = conn.cursor()

conn, cur = create_session()
conn = pymysql.connect(host = 'localhost', user='wonyeong', password = 'your_password', db = 'backend', charset = 'utf8')
cur = conn.cursor()


@green_router.post("/")
async def get_green(request: RequestModel):
response = get_LLM_response(request.data)
return {response}

@green_router.post("/save")
async def get_data(request: FixedCode):
# @green_router.post("/save")
# async def get_data(request: FixedCode):

# code = request.fixed_code
# user_id = request.id
# sql = "INSERT INTO codes (user_id, code) VALUES (%s, %s)"
# cur.execute(sql, (user_id, code))
# conn.commit()
# return {"message": "Data saved successfully"}

code = request.fixed_code
user_id = request.id
sql = "INSERT INTO codes (user_id, code) VALUES (%s, %s)"
cur.execute(sql, (user_id, code))
conn.commit()
return {"message": "Data saved successfully"}



'''
TABLE `codes` (
`code_id` INT NOT NULL AUTO_INCREMENT,
`user_id` CHAR(10) NOT NULL,
`code` VARCHAR(1000) NOT NULL,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`code_id`, `user_id`)
);
'''

0 comments on commit 4ee7e70

Please sign in to comment.