File size: 2,586 Bytes
30856e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
from cProfile import label
from code import interact
from multiprocessing.util import ForkAwareThreadLock
import os
import requests
import gradio as gr


# ENV vars
API_URL = os.environ["API_URL"]
HF_TOKEN = os.environ["HF_TOKEN"]
headers = {"Authorization": f"Bearer {HF_TOKEN}"}

comment_syntaxes = {
    "C": "/* {} */",
    "C++": "/* {} */",
    "Java": "/* {} */",
    "Golang": "/* {} */",
    "Rust": "/* {} */",
    "Javascript": "/* {} */",
    "PHP": "/* {} */",
    "Kotlin": "/* {} */",
    "HTML": "<!--  {} -->",
    "Python": "#{}",
    "Bash": "#{}",
    "Ruby": "=begin {} =end",
}

jsn_trail = {"parameters":
             {
                 "top_p": 0.9,
                 "max_new_tokens": 4096,
                 "return_full_text": True,
                 "do_sample": True,
             },
             "options":
             {"use_cache": True,
              "wait_for_model": True,
              }, }


def post(jsn):
    response = requests.post(API_URL, headers=headers, json=jsn)
    return response.json()[0]["generated_text"]


def get_script(lang, instruction):
    jsn = {"inputs": comment_syntaxes[lang].format("Programming Language: " + lang) + "\n" + comment_syntaxes[lang].format("Instruction: " + instruction.replace(
        '\n', '')) + '\n', **jsn_trail}
    return post(jsn)


def feedback(opt):
    return post({"inputs": opt, **jsn_trail})


demo = gr.Blocks()

with demo:
    gr.Markdown(
        "<h1><center>Give Instructions to Generate a Program</center></h1>")
    gr.Markdown(
        "<p>This project aims to prepare a prompt for BLOOM to generate scripts</p>")
    with gr.Row():

        dropdown = gr.Dropdown(value="Python",
                               choices=list(comment_syntaxes.keys()), label="Choose the language")

        # with gr.Column:
        instruction = gr.Textbox(label="Write an instruction",
                                 value="Create a python function that generates random password with given length using ascii characters ", lines=6)

    with gr.Row():
        generated_txt = gr.Textbox(lines=5, interactive=False, label="Output")

    btn = gr.Button("Generate")
    btn.click(get_script, inputs=[dropdown,
                                  instruction], outputs=generated_txt)
    feeedback_btn = gr.Button("Feedback")
    feeedback_btn.click(
        feedback, inputs=[generated_txt], outputs=generated_txt)
    with gr.Row():
        gr.Markdown(
            "![visitor badge](https://visitor-badge.glitch.me/badge?page_id=abdulmeLINK.programmer-bloom)")

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