import spaces import gradio as gr import transformers from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig import torch title = """ # Welcome to 🌟Tonic's🫡Command-R 🫡Command-R is a Large Language Model optimized for conversational interaction and long context tasks. It targets the “scalable” category of models that balance high performance with strong accuracy, enabling companies to move beyond proof of concept, and into production. 🫡Command-R boasts high precision on retrieval augmented generation (RAG) and tool use tasks, low latency and high throughput, a long 128k context, and strong capabilities across 10 key languages. You can build with this endpoint using✨StarCoder available here : [bigcode/starcoder2-15b](https://huggingface.co/bigcode/starcoder2-15b). You can also use 🫡Command-R 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🌟 [Torchon](https://github.com/Tonic-AI/Torchon)🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗 """ bnb_config = BitsAndBytesConfig(load_in_8bit=True) model_id = "CohereForAI/c4ai-command-r-v01" tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=bnb_config) @spaces.GPU def generate_response(user_input, max_new_tokens, temperature): # Format message with the command-r chat template messages = [{"role": "user", "content": user_input}] input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt") # Generate tokens gen_tokens = model.generate( input_ids['input_ids'], max_length=max_new_tokens + input_ids['input_ids'].shape[1], # Adjusting max_length to account for input length do_sample=True, temperature=temperature, ) # Decode tokens to string gen_text = tokenizer.decode(gen_tokens[0]) return gen_text def setup_examples(): examples = [ {"message": "What is the weather like today?", "max_new_tokens": 30, "temperature": 0.5}, {"message": "Tell me a joke.", "max_new_tokens": 50, "temperature": 0.7}, {"message": "Explain the concept of machine learning.", "max_new_tokens": 100, "temperature": 0.3} ] for example in examples: message_box.update(example["message"]) max_new_tokens_slider.update(example["max_new_tokens"]) temperature_slider.update(example["temperature"]) generate_button.click() with gr.Blocks() as demo: gr.Markdown(title) with gr.Row(): message_box = gr.Textbox(lines=2, label="Your Message") max_new_tokens_slider = gr.Slider(minimum=10, maximum=100, value=50, label="Max New Tokens") temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, value=0.3, label="Temperature") generate_button = gr.Button("Try🫡Command-R") output_box = gr.Textbox(label="🫡Command-R") generate_button.click( fn=generate_response, inputs=[message_box, max_new_tokens_slider, temperature_slider], outputs=output_box ) setup_examples_button = gr.Button("Load Example") setup_examples_button.click(fn=setup_examples, inputs=[], outputs=[]) demo.launch()