Spaces:
Runtime error
Runtime error
Commit
·
30856e5
1
Parent(s):
eb79551
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from cProfile import label
|
2 |
+
from code import interact
|
3 |
+
from multiprocessing.util import ForkAwareThreadLock
|
4 |
+
import os
|
5 |
+
import requests
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
|
9 |
+
# ENV vars
|
10 |
+
API_URL = os.environ["API_URL"]
|
11 |
+
HF_TOKEN = os.environ["HF_TOKEN"]
|
12 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
13 |
+
|
14 |
+
comment_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 |
+
|
29 |
+
jsn_trail = {"parameters":
|
30 |
+
{
|
31 |
+
"top_p": 0.9,
|
32 |
+
"max_new_tokens": 4096,
|
33 |
+
"return_full_text": True,
|
34 |
+
"do_sample": True,
|
35 |
+
},
|
36 |
+
"options":
|
37 |
+
{"use_cache": True,
|
38 |
+
"wait_for_model": True,
|
39 |
+
}, }
|
40 |
+
|
41 |
+
|
42 |
+
def post(jsn):
|
43 |
+
response = requests.post(API_URL, headers=headers, json=jsn)
|
44 |
+
return response.json()[0]["generated_text"]
|
45 |
+
|
46 |
+
|
47 |
+
def 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 |
+
|
53 |
+
def feedback(opt):
|
54 |
+
return post({"inputs": opt, **jsn_trail})
|
55 |
+
|
56 |
+
|
57 |
+
demo = gr.Blocks()
|
58 |
+
|
59 |
+
with 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 |
+
|
86 |
+
demo.launch(enable_queue=True, debug=True)
|