File size: 2,881 Bytes
8f41281
05e6c2e
8f41281
 
 
7d554c0
8f41281
7b41915
6d07490
 
8f41281
0b13087
 
8f41281
 
f24dcc1
 
8f41281
83b280c
 
 
 
 
5224d65
8f41281
b6ee2c4
 
83b280c
b6ee2c4
 
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 = "Code Compexity Predictor"
description = "This is a space to predict complexity of Java code with [CodeParrot-Multi-Complexity](https://huggingface.co/codeparrot/codeparrot-small-multi),\
    a multilingual model for code generation, fine-tuned on [CodeComplex](https://huggingface.co/datasets/codeparrot/codecomplex), a dataset for complexity prediction of Java code."

#add examples
example = [
    ['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'],
    ['import java.util.*;\n\npublic class ehab4 {\n    public static void main( String[] args ) {\n        Scanner in = new Scanner( System.in );\n\tint a = 0, b = 0;\n\tSystem.out.println( "? 0 0 " );\n\tSystem.out.flush();\n\tint c = in.nextInt();\n\tfor ( int i = 29; i >= 0; i-- ) {\n\t    System.out.println( "? " + ( a + ( 1 << i ) ) + " " + b );\n\t    System.out.flush();\n\t    int q1 = in.nextInt();\n\t    System.out.println( "? " + a + " " + ( b + ( 1 << i ) ) );\n\t    System.out.flush();\n\t    int q2 = in.nextInt();\n\t    if ( q1 == q2 ) {\n\t\tif ( c == 1 )\n\t\t    a += ( 1 << i );\n\t\telse if ( c == -1 )\n\t\t    b += ( 1 << i );\n\t\tc = q1;\n\t    }\n\t    else if ( q1 == -1 ) {\n\t\ta += ( 1 << i );\n\t\tb += ( 1 << i );\n\t    }\n\t    else if ( q1 == -2 )\n\t\treturn;\n\t}\n\tSystem.out.println( "! " + a + " " + b );\n\tSystem.out.flush();\n    }\n}\n']]

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

def get_label(output):
    label = int(output[-1])
    labels = ClassLabel(num_classes=7, names=['constant', 'linear', 'np', 'logn', 'quadratic', 'nlogn', 'cubic'])
    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.Textbox(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()