import spaces import gradio as gr import torch import transformers from transformers import AutoModelForCausalLM, AutoTokenizer import os title = """# Welcome to 🌟Tonic's✨StarCoder ✨StarCoder StarCoder2-15B model is a 15B parameter model trained on 600+ programming languages from The Stack v2, with opt-out requests excluded. The model uses Grouped Query Attention, a context window of 16,384 tokens with a sliding window attention of 4,096 tokens, and was trained using the Fill-in-the-Middle objective on 4+ trillion tokens. The model was trained with NVIDIA NeMo™ Framework using the NVIDIA Eos Supercomputer built with NVIDIA DGX H100 systems. You can build with this endpoint using✨StarCoder available here : [bigcode/starcoder2-15b](https://huggingface.co./bigcode/starcoder2-15b). You can also use ✨StarCoder by cloning this space. Simply click here: Duplicate Space Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's 🛠️community 👻 [![Join us on Discord](https://img.shields.io/discord/1109943800132010065?label=Discord&logo=discord&style=flat-square)](https://discord.gg/GWpVpekp) On 🤗Huggingface:[MultiTransformer](https://huggingface.co./MultiTransformer) Math 🔍 [introspector](https://huggingface.co./introspector) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to🌟 [SciTonic](https://github.com/Tonic-AI/scitonic)🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗 """ default_system_prompt = """SYSTEM: You are an AI that code. Answer with code.""" model_path = "bigcode/starcoder2-15b" hf_token = os.getenv("HF_TOKEN") if not hf_token: raise ValueError("Hugging Face token not found. Please set the HF_TOKEN environment variable.") model = AutoModelForCausalLM.from_pretrained( model_path, torch_dtype=torch.bfloat16, device_map="auto", # trust_remote_code=True, token=hf_token, ) tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) # import gradio as gr # from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig # checkpoint = "bigcode/starcoder2-15b" # quantization_config = BitsAndBytesConfig(load_in_8bit=True) # tokenizer = AutoTokenizer.from_pretrained(checkpoint) # model = AutoModelForCausalLM.from_pretrained(checkpoint, quantization_config=quantization_config).to("cuda") def generate_text(prompt, temperature, max_length): inputs = tokenizer.encode(prompt, return_tensors="pt").to("cuda") outputs = model.generate(inputs, max_length=max_length, top_p=0.9, temperature=temperature) return tokenizer.decode(outputs[0]) def gradio_app(): with gr.Blocks() as demo: gr.Markdown(title) output = gr.Code(label="Generated Code", lines=40) with gr.Row(): generate_btn = gr.Button("Generate") with gr.Row(): temperature = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, value=0.5, label="Temperature") max_length = gr.Slider(minimum=100, maximum=1024, step=10, value=100, label="Generate Length") with gr.Row(): prompt = gr.Textbox(label="Enter your code prompt", placeholder="def print_hello_world():") generate_btn.click( fn=generate_text, inputs=[prompt, temperature, max_length], outputs=output ) demo.launch()