Spaces:
Runtime error
Runtime error
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": 64, | |
"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) | |