Spaces:
Runtime error
Runtime error
import json | |
import gradio as gr | |
import requests as req | |
code_nl = "function for db connection" | |
CT5_URL = "https://api-inference.huggingface.co/models/nielsr/codet5-small-code-summarization-ruby" | |
CT5_METHOD = 'POST' | |
API_URL = CT5_URL | |
headers = {"Authorization": "Bearer api_UhCKXKyqxJOpOcbvrZurQFqmVNZRTtxVfl"} | |
def query(payload): | |
response = req.post(API_URL, headers=headers, json=payload) | |
return response.json() | |
function_code = r""" | |
def fetch_archive_from_http(url: str, output_dir: str, proxies: Optional[dict] = None) -> bool: | |
path = Path(output_dir) | |
if not path.exists(): | |
path.mkdir(parents=True) | |
is_not_empty = len(list(Path(path).rglob("*"))) > 0 | |
if is_not_empty: | |
return False | |
else: | |
_, _, archive_extension = url.rpartition(".") | |
request_data = requests.get(url, proxies=proxies) | |
if archive_extension == "zip": | |
zip_archive = zipfile.ZipFile(io.BytesIO(request_data.content)) | |
zip_archive.extractall(output_dir) | |
elif archive_extension in ["gz", "bz2", "xz"]: | |
tar_archive = tarfile.open(fileobj=io.BytesIO(request_data.content), mode="r|*") | |
tar_archive.extractall(output_dir) | |
else: | |
pass | |
return True | |
""" | |
task_code = f'Summarize Python: {function_code}' | |
real_docstring = r""" | |
Fetch an archive (zip or tar.gz) from a url via http and extract content to an output directory. | |
:param url: http address | |
:param output_dir: local path | |
:param proxies: proxies details as required by requests library | |
:return: if anything got fetched | |
""" | |
def pygen_func(function_code): | |
req_data = {"inputs": task_code} | |
output = query(req_data) | |
return str(output) | |
# inputs = {'code_nl': code_nl} | |
# payload = json.dumps(inputs) | |
# prediction = req.request(CT5_METHOD, CT5_URL, data=payload) | |
# prediction = req.request(CT5_METHOD, CT5_URL, json=req_data) | |
# answer = json.loads(prediction.content.decode("utf-8")) | |
# return str(answer) | |
iface = gr.Interface(pygen_func, | |
[ | |
# gr.inputs.Textbox(lines=7, label="Code Intent (NL)", default=task_code), | |
gr.inputs.Textbox(lines=7, label="Task + Code (PL)", default=task_code), | |
], | |
# gr.outputs.Textbox(label="Code Generated PL")) | |
gr.outputs.Textbox(label="Docstring Generated (NL)")) | |
iface.launch(share=True) |