import gradio as gr from llama_cpp import Llama import requests # Define available models MODELS = { "Llama-3.2-3B": { "repo_id": "lmstudio-community/Llama-3.2-3B-Instruct-GGUF", "filename": "*Q4_K_M.gguf", "chat_format": "chatml" }, "Llama-3.2-5B": { "repo_id": "lmstudio-community/Llama-3.2-1B-Instruct-GGUF", "filename": "*Q4_K_M.gguf", "chat_format": "chatml" }, "Phi-3.5-mini": { "repo_id": "bartowski/Phi-3.5-mini-instruct-GGUF", "filename": "*Q4_K_M.gguf", "chat_format": "chatml" }, "Granite-3B": { "repo_id": "lmstudio-community/granite-3.0-3b-a800m-instruct-GGUF", "filename": "*Q4_K_M.gguf", "chat_format": "chatml" }, "Qwen2.5-3B": { "repo_id": "lmstudio-community/Qwen2.5-3B-Instruct-GGUF", "filename": "*Q4_K_M.gguf", "chat_format": "chatml" }, "SmolLM2-1.7B": { "repo_id": "HuggingFaceTB/SmolLM2-1.7B-Instruct-GGUF", "filename": "*Q4_K_M.gguf", "chat_format": "chatml" }, "Qwen2.5-1.5B": { "repo_id": "lmstudio-community/Qwen2.5-1.5B-Instruct-GGUF", "filename": "*Q4_K_M.gguf", "chat_format": "chatml" }, "Granite-1B": { "repo_id": "lmstudio-community/granite-3.0-1b-a400m-instruct-GGUF", "filename": "*Q4_K_M.gguf", "chat_format": "chatml" }, "AMD-OLMo-1B": { "repo_id": "lmstudio-community/AMD-OLMo-1B-SFT-GGUF", "filename": "*Q4_K_M.gguf", "chat_format": "chatml" } } # Initialize with default model current_model = None def load_model(model_name): global current_model model_info = MODELS[model_name] current_model = Llama.from_pretrained( repo_id=model_info["repo_id"], filename=model_info["filename"], verbose=True, n_ctx=32768, n_threads=2, chat_format=model_info["chat_format"] ) return current_model # Initialize with first model current_model = load_model(list(MODELS.keys())[0]) def respond( message, history, model_name, system_message, max_tokens, temperature, top_p, ): global current_model # Load new model if changed if current_model is None or model_name not in str(current_model.model_path): current_model = load_model(model_name) # Start with system message messages = [] if system_message and system_message.strip(): messages.append({"role": "system", "content": system_message}) # Add chat history if history: messages.extend(history) # Add current message messages.append({"role": "user", "content": message}) # Generate response response = current_model.create_chat_completion( messages=messages, stream=True, max_tokens=max_tokens, temperature=temperature, top_p=top_p ) message_repl = "" for chunk in response: if len(chunk['choices'][0]["delta"]) != 0 and "content" in chunk['choices'][0]["delta"]: message_repl = message_repl + chunk['choices'][0]["delta"]["content"] yield message_repl def get_chat_title(model_name): return f"{model_name} < - Load different model in Additional Inputs" with gr.Blocks() as demo: with gr.Row(): title = gr.HTML(value=f"