Spaces:
Sleeping
Sleeping
File size: 3,454 Bytes
ca321f9 22c74a0 ca321f9 22c74a0 ca321f9 22c74a0 ca321f9 22c74a0 ca321f9 b1b08c5 ca321f9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import spaces
title = """# π€΅Welcome to SAK's CodBot π» (A Coding ChatBot) βββ"""
MARKDOWN = """
CodingBot is an open-source coding language model that delivers excellent performance.
CodingBot leverages LlamaforCausalLM.
CodingBot supports 'java', 'javascript', 'c++', 'c#', 'c', 'html', 'java_server_pages', 'python', 'php', 'go', 'kotlin', 'swift', 'dart', 'shell', 'json', 'lua', 'matlab', 'yaml', 'css', 'rust', 'sql', 'ruby', 'tex', 'objective-c', 'powershell', 'ocaml', 'groovy', 'cmake', 'julia', 'perl', 'assembly', 'haskell', 'fortran', 'pascal', 'rmarkdown', 'scala', 'visual_basic', 'verilog', 'prolog', 'r', 'dockerfile','cobol', 'batchfile', 'toml', 'lisp', 'erlang', 'coffeescript', 'makefile', 'clojure', 'elixir'
**Demo by [Sunder Ali Khowaja](https://sander-ali.github.io) - [X](https://x.com/SunderAKhowaja) -[Github](https://github.com/sander-ali) -[Hugging Face](https://huggingface.co/SunderAli17)**
"""
# Define the device and model path
device = "cuda" if torch.cuda.is_available() else "cpu"
model_path = "SunderAli17/CodingBot"
# Load the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto").eval()
@spaces.GPU(duration=130)
def generate_code(system_prompt, user_prompt, max_length):
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(device)
generated_ids = model.generate(
model_inputs.input_ids,
max_new_tokens=max_length,
eos_token_id=tokenizer.eos_token_id
)
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
return response
theme = gr.themes.Soft(
font=[gr.themes.GoogleFont('Source Code Pro'), gr.themes.GoogleFont('Public Sans'), 'system-ui', 'sans-serif'],
)
js_func = """
function refresh() {
const url = new URL(window.location);
if (url.searchParams.get('__theme') !== 'dark') {
url.searchParams.set('__theme', 'dark');
window.location.href = url.href;
}
}
"""
with gr.Blocks(js = js_func, theme = theme) as SAK:
gr.Markdown(title)
gr.Markdown(MARKDOWN)
system_prompt_input = gr.Textbox(
label="π¨βπ» CodBot Instruction:",
value="Hello Sir! how are you today? I am here to generate clear and concise code examples for you.",
lines=2
)
user_prompt_input = gr.Code(
label="β Coding Prompt π»",
value="Shopping website in HTML and Java",
language="python",
lines=2
)
code_output = gr.Code(label="π¨βπ» CodBot", language='python', lines=50, interactive=True)
max_length_slider = gr.Slider(minimum=1, maximum=1800, value=650, label="Max Token Length")
generate_button = gr.Button("Generate Code")
generate_button.click(
generate_code,
inputs=[system_prompt_input, user_prompt_input, max_length_slider],
outputs=code_output
)
SAK.queue().launch(debug=True, share=True) |