Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -65,26 +65,27 @@ def predict_single(code):
|
|
| 65 |
preds = torch.sigmoid(outputs.logits).cpu().numpy()
|
| 66 |
cpu_time, memory_usage = preds[0]
|
| 67 |
|
|
|
|
| 68 |
return {
|
| 69 |
-
"
|
| 70 |
-
"
|
| 71 |
}
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
print(f"Single prediction error: {e}")
|
| 75 |
-
return {"
|
| 76 |
|
| 77 |
def predict_with_chunking(code, chunk_size=400, overlap=50):
|
| 78 |
try:
|
| 79 |
if not code or len(code.strip()) == 0:
|
| 80 |
-
return {"
|
| 81 |
|
| 82 |
tokens = tokenizer.encode(code, add_special_tokens=False)
|
| 83 |
if len(tokens) <= 450:
|
| 84 |
return predict_single(code)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
for start in range(0, len(tokens), chunk_size - overlap):
|
| 90 |
end = min(start + chunk_size, len(tokens))
|
|
@@ -93,20 +94,20 @@ def predict_with_chunking(code, chunk_size=400, overlap=50):
|
|
| 93 |
|
| 94 |
if chunk_code.strip():
|
| 95 |
result = predict_single(chunk_code)
|
| 96 |
-
|
| 97 |
-
|
| 98 |
|
| 99 |
if end >= len(tokens):
|
| 100 |
break
|
| 101 |
|
| 102 |
return {
|
| 103 |
-
"
|
| 104 |
-
"
|
| 105 |
}
|
| 106 |
|
| 107 |
except Exception as e:
|
| 108 |
print(f"Chunking prediction error: {e}")
|
| 109 |
-
return {"
|
| 110 |
|
| 111 |
@app.route("/", methods=['GET'])
|
| 112 |
def home():
|
|
|
|
| 65 |
preds = torch.sigmoid(outputs.logits).cpu().numpy()
|
| 66 |
cpu_time, memory_usage = preds[0]
|
| 67 |
|
| 68 |
+
# Invert values so higher = better
|
| 69 |
return {
|
| 70 |
+
"cpu_efficiency": round(1.0 - float(cpu_time), 4),
|
| 71 |
+
"memory_efficiency": round(1.0 - float(memory_usage), 4)
|
| 72 |
}
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
print(f"Single prediction error: {e}")
|
| 76 |
+
return {"cpu_efficiency": 0.0, "memory_efficiency": 0.0}
|
| 77 |
|
| 78 |
def predict_with_chunking(code, chunk_size=400, overlap=50):
|
| 79 |
try:
|
| 80 |
if not code or len(code.strip()) == 0:
|
| 81 |
+
return {"cpu_efficiency": 0.0, "memory_efficiency": 0.0}
|
| 82 |
|
| 83 |
tokens = tokenizer.encode(code, add_special_tokens=False)
|
| 84 |
if len(tokens) <= 450:
|
| 85 |
return predict_single(code)
|
| 86 |
|
| 87 |
+
max_cpu_efficiency = 0.0
|
| 88 |
+
max_memory_efficiency = 0.0
|
| 89 |
|
| 90 |
for start in range(0, len(tokens), chunk_size - overlap):
|
| 91 |
end = min(start + chunk_size, len(tokens))
|
|
|
|
| 94 |
|
| 95 |
if chunk_code.strip():
|
| 96 |
result = predict_single(chunk_code)
|
| 97 |
+
max_cpu_efficiency = max(max_cpu_efficiency, result["cpu_efficiency"])
|
| 98 |
+
max_memory_efficiency = max(max_memory_efficiency, result["memory_efficiency"])
|
| 99 |
|
| 100 |
if end >= len(tokens):
|
| 101 |
break
|
| 102 |
|
| 103 |
return {
|
| 104 |
+
"cpu_efficiency": round(max_cpu_efficiency, 4),
|
| 105 |
+
"memory_efficiency": round(max_memory_efficiency, 4)
|
| 106 |
}
|
| 107 |
|
| 108 |
except Exception as e:
|
| 109 |
print(f"Chunking prediction error: {e}")
|
| 110 |
+
return {"cpu_efficiency": 0.0, "memory_efficiency": 0.0}
|
| 111 |
|
| 112 |
@app.route("/", methods=['GET'])
|
| 113 |
def home():
|