awacke1/programmer-bloom
0
1from cProfile import label2from code import interact3from multiprocessing.util import ForkAwareThreadLock4import os5import requests6import gradio as gr7 8 9# ENV vars10API_URL = os.environ["API_URL"]11HF_TOKEN = os.environ["HF_TOKEN"]12headers = {"Authorization": f"Bearer {HF_TOKEN}"}13 14comment_syntaxes = {15 "C": "/* {} */",16 "C++": "/* {} */",17 "Java": "/* {} */",18 "Golang": "/* {} */",19 "Rust": "/* {} */",20 "Javascript": "/* {} */",21 "PHP": "/* {} */",22 "Kotlin": "/* {} */",23 "HTML": "<!-- {} -->",24 "Python": "#{}",25 "Bash": "#{}",26 "Ruby": "=begin {} =end",27}28 29jsn_trail = {"parameters":30 {31 "top_p": 0.9,32 "max_new_tokens": 64,33 "return_full_text": True,34 "do_sample": True,35 },36 "options":37 {"use_cache": True,38 "wait_for_model": True,39 }, }40 41 42def post(jsn):43 response = requests.post(API_URL, headers=headers, json=jsn)44 return response.json()[0]["generated_text"]45 46 47def get_script(lang, instruction):48 jsn = {"inputs": comment_syntaxes[lang].format("Programming Language: " + lang) + "\n" + comment_syntaxes[lang].format("Instruction: " + instruction.replace(49 '\n', '')) + '\n', **jsn_trail}50 return post(jsn)51 52 53def feedback(opt):54 return post({"inputs": opt, **jsn_trail})55 56 57demo = gr.Blocks()58 59with demo:60 gr.Markdown(61 "<h1><center>Give Instructions to Generate a Program</center></h1>")62 gr.Markdown(63 "<p>This project aims to prepare a prompt for BLOOM to generate scripts</p>")64 with gr.Row():65 66 dropdown = gr.Dropdown(value="Python",67 choices=list(comment_syntaxes.keys()), label="Choose the language")68 69 # with gr.Column:70 instruction = gr.Textbox(label="Write an instruction",71 value="Create a python function that generates random password with given length using ascii characters ", lines=6)72 73 with gr.Row():74 generated_txt = gr.Textbox(lines=5, interactive=False, label="Output")75 76 btn = gr.Button("Generate")77 btn.click(get_script, inputs=[dropdown,78 instruction], outputs=generated_txt)79 feeedback_btn = gr.Button("Feedback")80 feeedback_btn.click(81 feedback, inputs=[generated_txt], outputs=generated_txt)82 with gr.Row():83 gr.Markdown(84 "")85 86demo.launch(enable_queue=True, debug=True)87 