"""Template Demo for IBM Granite Hugging Face spaces.""" from collections.abc import Iterator from datetime import datetime from pathlib import Path from threading import Thread import gradio as gr import spaces import torch from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer from themes.research_monochrome import theme # Vision imports import random from transformers import LlavaNextProcessor, LlavaNextForConditionalGeneration today_date = datetime.today().strftime("%B %-d, %Y") # noqa: DTZ002 SYS_PROMPT = f"""Knowledge Cutoff Date: April 2024. Today's Date: {today_date}. You are Granite, developed by IBM. You are a helpful AI assistant""" TITLE = "IBM Granite 3.1 8b Instruct & Vision Preview" DESCRIPTION = """
Granite 3.1 8b instruct is an open-source LLM supporting a 128k context window. Start with one of the sample prompts or upload an image and ask a question. Keep in mind that AI can occasionally make mistakes. View Documentation
""" MAX_INPUT_TOKEN_LENGTH = 128_000 MAX_NEW_TOKENS = 1024 TEMPERATURE = 0.7 TOP_P = 0.85 TOP_K = 50 REPETITION_PENALTY = 1.05 if not torch.cuda.is_available(): print("This demo may not work on CPU.") # Text Model and Tokenizer text_model = AutoModelForCausalLM.from_pretrained( "ibm-granite/granite-3.1-8b-instruct", torch_dtype=torch.float16, device_map="auto" ) text_tokenizer = AutoTokenizer.from_pretrained("ibm-granite/granite-3.1-8b-instruct") text_tokenizer.use_default_system_prompt = False # Vision Model and Processor vision_model_path = "ibm-granite/granite-vision-3.1-2b-preview" vision_processor = LlavaNextProcessor.from_pretrained(vision_model_path, use_fast=True) vision_model = LlavaNextForConditionalGeneration.from_pretrained(vision_model_path, torch_dtype="auto", device_map="auto") @spaces.GPU def generate( message: str, chat_history: list[dict], temperature: float = TEMPERATURE, repetition_penalty: float = REPETITION_PENALTY, top_p: float = TOP_P, top_k: float = TOP_K, max_new_tokens: int = MAX_NEW_TOKENS, ) -> Iterator[str]: """Generate function for text chat demo.""" # Build messages conversation = [] conversation.append({"role": "system", "content": SYS_PROMPT}) conversation += chat_history conversation.append({"role": "user", "content": message}) # Convert messages to prompt format input_ids = text_tokenizer.apply_chat_template( conversation, return_tensors="pt", add_generation_prompt=True, truncation=True, max_length=MAX_INPUT_TOKEN_LENGTH - max_new_tokens, ) input_ids = input_ids.to(text_model.device) streamer = TextIteratorStreamer(text_tokenizer, skip_prompt=True, skip_special_tokens=True) generate_kwargs = dict( {"input_ids": input_ids}, streamer=streamer, max_new_tokens=max_new_tokens, do_sample=True, top_p=top_p, top_k=top_k, temperature=temperature, num_beams=1, repetition_penalty=repetition_penalty, ) t = Thread(target=text_model.generate, kwargs=generate_kwargs) t.start() outputs = [] for text in streamer: outputs.append(text) yield "".join(outputs) def get_text_from_content(content): texts = [] for item in content: if item["type"] == "text": texts.append(item["text"]) elif item["type"] == "image": texts.append("[Image]") return " ".join(texts) @spaces.GPU def chat_inference(image, text, temperature, top_p, top_k, max_tokens, conversation): if conversation is None: conversation = [] user_content = [] if image is not None: user_content.append({"type": "image", "image": image}) if text and text.strip(): user_content.append({"type": "text", "text": text.strip()}) if not user_content: return conversation_display(conversation), conversation conversation.append({ "role": "user", "content": user_content }) inputs = vision_processor.apply_chat_template( conversation, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt" ).to("cuda") torch.manual_seed(random.randint(0, 10000)) generation_kwargs = { "max_new_tokens": max_tokens, "temperature": temperature, "top_p": top_p, "top_k": top_k, "do_sample": True, } output = vision_model.generate(**inputs, **generation_kwargs) assistant_response = vision_processor.decode(output[0], skip_special_tokens=True) conversation.append({ "role": "assistant", "content": [{"type": "text", "text": assistant_response.strip()}] }) return conversation_display(conversation), conversation def conversation_display(conversation): chat_history = [] for msg in conversation: if msg["role"] == "user": user_text = get_text_from_content(msg["content"]) chat_history.append({"role": "user", "content": user_text}) elif msg["role"] == "assistant": assistant_text = msg["content"][0]["text"].split("<|assistant|>")[-1].strip() chat_history.append({"role": "assistant", "content": assistant_text}) return chat_history def clear_chat(): return [], [], "", None css_file_path = Path(Path(__file__).parent / "app.css") head_file_path = Path(Path(__file__).parent / "app_head.html") # Advanced settings (displayed in Accordion) - Common settings for both models temperature_slider = gr.Slider( minimum=0, maximum=1.0, value=TEMPERATURE, step=0.1, label="Temperature", elem_classes=["gr_accordion_element"] ) top_p_slider = gr.Slider( minimum=0, maximum=1.0, value=TOP_P, step=0.05, label="Top P", elem_classes=["gr_accordion_element"] ) top_k_slider = gr.Slider( minimum=0, maximum=100, value=TOP_K, step=1, label="Top K", elem_classes=["gr_accordion_element"] ) # Advanced settings specific to Text model repetition_penalty_slider = gr.Slider( minimum=0, maximum=2.0, value=REPETITION_PENALTY, step=0.05, label="Repetition Penalty (Text Model)", elem_classes=["gr_accordion_element"], ) max_new_tokens_slider = gr.Slider( minimum=1, maximum=2000, value=MAX_NEW_TOKENS, step=1, label="Max New Tokens (Text Model)", elem_classes=["gr_accordion_element"], ) # Advanced settings specific to Vision model max_tokens_slider_vision = gr.Slider( minimum=10, maximum=300, value=128, step=1, label="Max Tokens (Vision Model)", elem_classes=["gr_accordion_element"], ) chat_interface_accordion = gr.Accordion(label="Advanced Settings", open=False) with gr.Blocks(fill_height=True, css_paths=css_file_path, head_paths=head_file_path, theme=theme, title=TITLE) as demo: gr.HTML(f"