Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
|
| 6 |
+
# Load your model
|
| 7 |
+
model_path = "best_model_final"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 10 |
+
model.eval()
|
| 11 |
+
|
| 12 |
+
# Prediction function
|
| 13 |
+
def predict_cpu_memory(code):
|
| 14 |
+
inputs = tokenizer(code, return_tensors="pt", padding=True, truncation=True)
|
| 15 |
+
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
outputs = model(**inputs)
|
| 18 |
+
preds = F.sigmoid(outputs.logits).numpy()
|
| 19 |
+
|
| 20 |
+
cpu_time, memory_usage = preds[0]
|
| 21 |
+
return f"CPU Time: {cpu_time:.4f}\nMemory Usage: {memory_usage:.4f}"
|
| 22 |
+
|
| 23 |
+
# Gradio Interface
|
| 24 |
+
iface = gr.Interface(
|
| 25 |
+
fn=predict_cpu_memory,
|
| 26 |
+
inputs=gr.Textbox(lines=10, placeholder="Paste your code here..."),
|
| 27 |
+
outputs="text",
|
| 28 |
+
title="Code Resource Usage Predictor"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
iface.launch()
|