File size: 2,256 Bytes
8f41281
05e6c2e
8f41281
 
 
2cfd160
539c1e5
2cfd160
6d07490
 
797eb91
 
 
8f41281
 
d30c81a
 
8f41281
83b280c
 
b69dd7f
83b280c
 
5224d65
8f41281
b6ee2c4
 
83b280c
b6ee2c4
 
8f41281
 
 
 
 
4da495c
8f41281
b6ee2c4
 
 
 
8f41281
 
0b13087
8f41281
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
from datasets import ClassLabel
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline


title = "BigO"
description = "In this space we predict the complexity of Java code with [UniXcoder-java-complexity-prediction](https://huggingface.co/codeparrot/unixcoder-java-complexity-prediction),\
    a multilingual model for code, fine-tuned on [CodeComplex](https://huggingface.co/datasets/codeparrot/codecomplex), a dataset for complexity prediction of Java code."

#add examples
example = [['int n = 1000;\nSystem.out.println("Hey - your input is: " + n);'],
    ['class GFG {\n \n    public static void main(String[] args)\n    {\n        int i, n = 8;\n        for (i = 1; i <= n; i++) {\n            System.out.printf("Hello World !!!\n");\n        }\n    }\n}'],
    ['import java.io.*;\nimport java.util.*;\n\npublic class C125 {\n\tpublic static void main(String[] args) throws IOException {\n\t\tBufferedReader r = new BufferedReader(new InputStreamReader(System.in));\n\t\tString s = r.readLine();\n\t\tint n = new Integer(s);\n\t\tSystem.out.println("0 0 "+n);\n\t}\n}\n']]

# model to be changed to the finetuned one
tokenizer = AutoTokenizer.from_pretrained("codeparrot/unixcoder-java-complexity-prediction")
model = AutoModelForSequenceClassification.from_pretrained("codeparrot/unixcoder-java-complexity-prediction", num_labels=7)

def get_label(output):
    label = int(output[-1])
    labels = ClassLabel(num_classes=7, names=['constant', 'cubic', 'linear', 'logn', 'nlogn', 'np', 'quadratic'])
    return labels.int2str(label)
    
def complexity_estimation(gen_prompt):
    pipe = pipeline("text-classification", model=model, tokenizer=tokenizer)
    output = pipe(gen_prompt)[0]
    # add label conversion to class
    label = get_label(output['label'])
    score = output['score']
    return label, score


iface = gr.Interface(
    fn=complexity_estimation, 
    inputs=[
        gr.Code(lines=10, label="Input code"),
    ],
    outputs=[
    gr.Textbox(label="Predicted complexity", lines=1) ,
    gr.Textbox(label="Corresponding probability", lines=1) ,
],
    examples=example,
    layout="vertical",
    theme="darkpeach",
    description=description,
    title=title
)
iface.launch()