|
import gradio as gr |
|
import os, re, gc, time, sys, subprocess, typing, shutil, json, datetime, tempfile, safetensors, torch, threading, spaces |
|
import numpy as np |
|
from huggingface_hub import hf_hub_download, HfApi, upload_folder, create_repo |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
from accelerate import Accelerator |
|
from huggingface_hub import login |
|
HF_TOKEN=os.environ.get('HF_TOKEN') |
|
login(token=HF_TOKEN) |
|
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True" |
|
os.environ["ZERO_GPU_PATCH_TORCH_DEVICE"] = "True" |
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
accelerator=Accelerator() |
|
HfApi=HfApi() |
|
last=[] |
|
thecomponent="" |
|
TOKENIZER = accelerator.prepare(AutoTokenizer.from_pretrained("Qwen/Qwen2.5-Coder-7B-Instruct")) |
|
MODEL = accelerator.prepare(AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-Coder-7B-Instruct", torch_dtype=torch.bfloat16,)) |
|
eos_token_ids = [151645, 151643] |
|
|
|
@spaces.GPU() |
|
def helper(pmpt, input_text): |
|
model_inputs="" |
|
generated_text="" |
|
prompt = f"{pmpt}. The code: <|fim_prefix|>{input_text} <|fim_suffix|> <|fim_middle|>" |
|
messages = [{"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant. Acting as an expert AI Web Development and Programming model. As an expert AI Web Development and Programming model, correct/modify/create/generate/complete the user provided code based on user request, output the code without comments. Only output code without additional prefixed or suffixed messages/comments/wrapping or additional text."}, {"role": "user", "content": prompt}] |
|
text = TOKENIZER.apply_chat_template(messages,tokenize=False,add_generation_prompt=True) |
|
model_inputs = TOKENIZER([text], return_tensors="pt").to(device) |
|
generated_text = "" |
|
for new_text in TOKENIZER.decode(MODEL.generate(**model_inputs, max_new_tokens=2048, do_sample=False, eos_token_id=eos_token_ids,)[0][len(model_inputs.input_ids[0]):], skip_special_tokens=True): |
|
generated_text += new_text |
|
yield generated_text.strip() |
|
yield generated_text.strip() |
|
|
|
def pushy(spc_id,spc_tkn,thecomponent): |
|
tm_tkn=f"{spc_tkn}" |
|
succ=HfApi.create_repo(repo_id=f"{spc_id}", repo_type="space", space_sdk="gradio", private=False, exist_ok=True, token=f"{tm_tkn}") |
|
HfApi.upload_folder(repo_id=f"{spc_id}",repo_type="space", folder_path=f"{thecomponent}") |
|
del tm_tkn |
|
shutil.rmtree(thecomponent) |
|
gc.collect() |
|
print("Successfully pushed custom component.") |
|
return gr.Textbox(label="Step 6: Enter a new/existing space name", value=f"Success: created space {str(succ)}"), gr.Textbox(label="Your HF Token(write permission)", value="") |
|
|
|
files_list=[] |
|
def list_files(files_list,thecomponent): |
|
files_list=[] |
|
for root, dirs, files in os.walk(f"{os.getcwd()}/{thecomponent}", topdown=True): |
|
if '.git' in dirs: |
|
dirs.remove('.git') |
|
if '.gitattributes' in files: |
|
files.remove('.gitattributes') |
|
for name in files: |
|
rel_dir = os.path.relpath(root, os.getcwd()) |
|
rel_file = os.path.join(rel_dir, name) if rel_dir != '.' else name |
|
files_list.append(rel_file) |
|
print(os.path.join(root, name)) |
|
return files_list |
|
|
|
def lep(thecomponent): |
|
with tempfile.NamedTemporaryFile(delete=False) as tmpfile: |
|
tmpname = tmpfile.name |
|
os.system(f"ls {os.getcwd()}/{thecomponent} > {tmpname} 2>&1") |
|
with open(tmpname, "r") as f: |
|
output = f.read() |
|
os.remove(tmpname) |
|
return output |
|
|
|
def read_file(filename,cd,status): |
|
fauxname=filename |
|
filename=f"./{filename}" |
|
with open(filename, "r", encoding="utf-8") as f: |
|
return f.read(), f"Editing {fauxname}" |
|
|
|
def write_file(filename, thecomponent, content, lstf_dp): |
|
fauxname=filename |
|
filename=f"./{fauxname}" |
|
with open(filename, "w", encoding="utf-8") as f: |
|
f.write(content) |
|
return f"Saved {fauxname}", gr.Dropdown(choices=list_files(files_list, thecomponent), value=f"{fauxname}", type="value") |
|
|
|
def empty(cd,crea_name,lst_fls,thecomponent): |
|
return gr.Code(label="Step 5: Modify/Edit code.. Don't forget to save modifications..",interactive=True,lines=30), gr.Textbox(label="Optional: Filename to create", placeholder="eg: filename.txt"), gr.Textbox(label=f"{str(os.getcwd())}/{str(thecomponent)}",value=lep(thecomponent),visible=True) |
|
|
|
def open_file(selected_file,cd,status): |
|
return read_file(selected_file,cd,status) |
|
|
|
def save_file(selected_file, thecomponent, content, lstf_dp): |
|
return write_file(selected_file, thecomponent, content, lstf_dp) |
|
|
|
def sove_file(selected_file, thecomponent, content, lstf_dp): |
|
return write_file(f"{thecomponent}/{selected_file}", thecomponent, content, lstf_dp) |
|
|
|
def refresh_files(lstf_dp,lst_fls): |
|
return gr.Dropdown(choices=list_files(files_list), value=None, type="value"), gr.Textbox(label=f"{str(os.getcwd())}/{str(thecomponent)}",value=lep(thecomponent),visible=True) |
|
|
|
def run_command_os_system(cmd,fos,lstf_dp,lst_fls): |
|
thecomponent=f"{cmd}" |
|
cmd=f"gradio cc create {cmd} --directory {cmd} --package-name {cmd} --template {fos} --no-install --overwrite --no-configure-metadata" |
|
import tempfile |
|
with tempfile.NamedTemporaryFile(delete=False) as tmpfile: |
|
tmpname = tmpfile.name |
|
os.system(f"{cmd} > {tmpname} 2>&1") |
|
with open(tmpname, "r") as f: |
|
output = f.read() |
|
os.remove(tmpname) |
|
return output, gr.Dropdown(label="Step 4: Select a file to modify/edit..",choices=list_files(files_list,thecomponent), value=None, type="value"), gr.Textbox(label=f"{str(os.getcwd())}/{str(thecomponent)}",value=lep(thecomponent),visible=True) |
|
|
|
with gr.Blocks() as iface: |
|
with gr.Row(): |
|
lstf_dp=gr.Dropdown(label="Step 4: Select a file to modify/edit..Carefully review each file.",choices=[], value=None, type="value") |
|
with gr.Column(): |
|
with gr.Group(): |
|
with gr.Row(): |
|
pmpt=gr.Textbox(label="Prompt", placeholder="Prompt goes here... eg: Extend the code to include an option a method and necessary functions for automatic...") |
|
with gr.Row(): |
|
sv=gr.Button("Try AI Assisted Editing", size="md") |
|
with gr.Group(): |
|
with gr.Row(): |
|
status = gr.Textbox(label="Status", interactive=False, placeholder="Edit mode:") |
|
with gr.Row(): |
|
save_btn = gr.Button("Save",size="sm") |
|
with gr.Row(): |
|
cd=gr.Code(label="Step 5: Modify/Edit code.. Don't forget to save modifications..",interactive=True,lines=30) |
|
with gr.Group(): |
|
with gr.Row(): |
|
spc_id=gr.Textbox(label="Step 6: Enter a new/existing space name", placeholder="eg: JoPmt/my_new_custom_gradio_component") |
|
with gr.Row(): |
|
spc_tkn=gr.Textbox(label="Your HF Token(write permission)") |
|
with gr.Row(): |
|
spc_btn=gr.Button("Push custom component to space") |
|
with gr.Row(): |
|
bldcc=gr.Button("Optional: Build Custom Component (Must have pypi account)..wip",interactive=False) |
|
sv.click(helper,[pmpt,cd],cd) |
|
with gr.Sidebar(position="left", open=True): |
|
with gr.Row(): |
|
output_os = gr.Textbox(label="System stdout:", lines=6, interactive=False, placeholder=">") |
|
with gr.Row(): |
|
cmd_input = gr.Textbox(label="Step 1: Name your custom component",placeholder="eg: Mycomponent") |
|
with gr.Row(): |
|
fos=gr.Dropdown(label="Step 2: Pick a template to modify", choices=["SimpleDropdown", "SimpleTextbox", "SimpleImage", "Audio", "BarPlot", "Button", "Chatbot", "ClearButton", "Checkbox", "CheckboxGroup", "Code", "ColorPicker", "DataFrame", "DownloadButton", "DuplicateButton", "Gallery", "HTML", "FileExplorer", "Image", "JSON", "Label", "LinePlot", "BrowserState", "LoginButton", "Markdown", "Textbox", "DateTime", "Dropdown", "Model3D", "File", "HighlightedText", "AnnotatedImage", "CheckboxGroup", "Number", "Plot", "Radio", "ScatterPlot", "Slider", "Timer", "UploadButton", "Video", "ImageEditor", "ImageSlider", "ParamViewer", "MultimodalTextbox", "NativePlot", "DeepLinkButton", "Accordion", "Column", "Row", "Group", "Tabs", "Tab", "TabItem", "Sidebar"],value=None,type="value") |
|
with gr.Row(): |
|
run_os_btn = gr.Button("Step 3: Setup custom component",size="md") |
|
with gr.Row(): |
|
lst_fls=gr.Textbox(label="",value="",visible=False) |
|
with gr.Row(): |
|
crea_name=gr.Textbox(label="Optional: Filename to create", placeholder="eg: filename.txt") |
|
with gr.Row(): |
|
create_fl=gr.Button("Optional: Create file",size="md") |
|
lstf_dp.input(open_file, inputs=[lstf_dp,cd,status], outputs=[cd,status]) |
|
save_btn.click(save_file, inputs=[lstf_dp, cmd_input, cd, lstf_dp], outputs=[status,lstf_dp]) |
|
create_fl.click(sove_file,[crea_name,cmd_input,cd,lstf_dp],[status,lstf_dp]).then(empty,[cd,crea_name,lst_fls,cmd_input],[cd,crea_name,lst_fls]) |
|
spc_btn.click(pushy,[spc_id,spc_tkn, cmd_input],[spc_id,spc_tkn]).then(fn=None,inputs=None,outputs=None,js="""()=>{ window.location.href = window.location.href; };""") |
|
run_os_btn.click(run_command_os_system, inputs=[cmd_input,fos,lstf_dp,lst_fls], outputs=[output_os,lstf_dp,lst_fls]) |
|
iface.launch(ssr_mode=False) |