File size: 2,783 Bytes
7ce071b
 
 
 
fe9d921
7ce071b
 
 
 
 
04e71ea
7ce071b
a649b9c
7ce071b
 
 
04e71ea
48e8117
 
 
7ce071b
 
 
 
 
 
 
a595e1c
7ce071b
 
 
 
 
 
 
 
 
 
 
 
a595e1c
7ce071b
 
 
 
 
 
 
 
48e8117
 
 
7ce071b
afa1c51
7ce071b
 
 
 
 
 
 
48e8117
 
 
 
 
7ce071b
 
 
 
 
 
 
eef3d9f
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import gradio as gr
import os
import openai

openai.api_key = os.environ["API"]

demo = gr.Blocks()

#'spaces/Gradio-Blocks/Codex_OpenAI' did not work due to paramerts error 
#'huggingface/facebook/incoder-6B' inference error
#huggingface/Salesforce/codegen-2B-mono 500 Error 

name_list = ['huggingface/lvwerra/codeparrot','huggingface/facebook/incoder-1B','huggingface/Salesforce/codegen-2B-mono']

open_ai_name = ['openai/code-davinci-002', 'openai/code-cushman-001']

examples = [ ["import numpy as np\nfrom sklearn.ensemble import RandomForestClassifier\n# create training data\nX = np.random.randn(100, 100)\ny = np.random.randint(0, 1, 100)\n # setup train test split"], 
             ['def get_file_size(filepath):'],
            ] 

def generate_code_os(text):
    interfaces = [gr.Interface.load(name) for name in name_list]
    return [interface(text) for interface in interfaces]
     
def generate_code_cs_1(text):
    response = openai.Completion.create(
    engine="code-davinci-002",
    prompt=text,
    temperature=0,
    max_tokens=256,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
    ).choices[0].text
    response_2 = generate_code_cs_2(text)
    return [response, response_2]

def generate_code_cs_2(text):
    response_2 = openai.Completion.create(
    engine="code-cushman-001",
    prompt=text,
    temperature=0,
    max_tokens=256,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
    ).choices[0].text
    return response_2

def set_example(example: list) -> dict:
    return gr.Textbox.update(value=example[0]) 
    
with gr.Blocks() as demo:
    gr.Markdown("# Compare OpenAI and HuggingFace Code Generators")
    with gr.Box():
        with gr.Row():
            with gr.Column():
                input_text = gr.Textbox(label = "Write your code here", lines=4)
                with gr.Row():
                    btn = gr.Button("Open-Source models Generate code brrr ...")
                    btn2 = gr.Button("OpenAI models Generate code brrr ...")
                
                example_text = gr.Dataset(components=[input_text], samples=examples)
                example_text.click(fn=set_example,
                                inputs = example_text,
                                outputs= example_text.components)    
            with gr.Column():   
                gr.Markdown("Let’s Compare code generators ") 
                btn2.click(generate_code_cs_1, inputs = input_text, outputs =  [gr.Textbox(label=open_ai_name[_], lines=4) for _ in range(len(open_ai_name))])
                with gr.Column():       
                    btn.click(generate_code_os, inputs = input_text, outputs = [gr.Textbox(label=name_list[_], lines=4) for _ in range(len(name_list))])


demo.launch(enable_queue=True,debug=True)