File size: 1,093 Bytes
d407fa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import gradio as gr
from transformers import AutoTokenizer
import torch
from model import EnergySmellsDetector
from config import SMELLS, BEST_THRESHOLD

TOKENIZER = "microsoft/graphcodebert-base"
tokenizer = AutoTokenizer.from_pretrained(TOKENIZER)
model = EnergySmellsDetector.load_model_from_hf()


def round_logit(logits, threshold):
    logits = (logits > threshold).to(int)
    return logits.cpu().numpy()


def greet(code_snippet):
    inputs = tokenizer(code_snippet, return_tensors="pt", truncation=True)
    with torch.no_grad():
        logits = model(**inputs)[0]
        rounded_logits = round_logit(logits, BEST_THRESHOLD)
        return f"{dict(zip(SMELLS, map(int, rounded_logits)))}"



textbox = gr.Textbox(label="Enter your code snippet", placeholder="Here goes your code")
description = "An application to identify whether your code has energy smells or not. It predicts the presence of 9 different energy smells."
title = "Energy Smells Detector"

gr.Interface(
    title=title,
    description=description,
    inputs=textbox,
    fn=greet,
    outputs="text"
).launch()