|
|
import gradio as gr
|
|
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
import torch
|
|
|
import torch.nn.functional as F
|
|
|
|
|
|
|
|
|
model_path = "best_model_final"
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
|
|
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
|
|
model.eval()
|
|
|
|
|
|
|
|
|
def predict_cpu_memory(code):
|
|
|
inputs = tokenizer(code, return_tensors="pt", padding=True, truncation=True)
|
|
|
|
|
|
with torch.no_grad():
|
|
|
outputs = model(**inputs)
|
|
|
preds = F.sigmoid(outputs.logits).numpy()
|
|
|
|
|
|
cpu_time, memory_usage = preds[0]
|
|
|
return f"CPU Time: {cpu_time:.4f}\nMemory Usage: {memory_usage:.4f}"
|
|
|
|
|
|
|
|
|
iface = gr.Interface(
|
|
|
fn=predict_cpu_memory,
|
|
|
inputs=gr.Textbox(lines=10, placeholder="Paste your code here..."),
|
|
|
outputs="text",
|
|
|
title="Code Resource Usage Predictor"
|
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
iface.launch()
|
|
|
|