CoolFace
Apppublic

SpacesExamples/llama-cpp-python-cuda-gradio

sourceHugging Faceupdated 3y agoView on Hugging Face
23likes
app.py72 linesDownload Raw Back to root
1import os2import gradio as gr3import copy4import time5import llama_cpp6from llama_cpp import Llama7from huggingface_hub import hf_hub_download  8 9 10llm = Llama(11    model_path=hf_hub_download(12        repo_id=os.environ.get("REPO_ID", "TheBloke/Llama-2-7b-Chat-GGUF"),13        filename=os.environ.get("MODEL_FILE", "llama-2-7b-chat.Q5_0.gguf"),14    ),15    n_ctx=2048,16    n_gpu_layers=50, # change n_gpu_layers if you have more or less VRAM 17) 18 19history = []20 21system_message = """22You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe.  Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.23 24If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.25"""26 27 28def generate_text(message, history):29    temp = ""30    input_prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n "31    for interaction in history:32        input_prompt = input_prompt + str(interaction[0]) + " [/INST] " + str(interaction[1]) + " </s><s> [INST] "33 34    input_prompt = input_prompt + str(message) + " [/INST] "35 36    output = llm(37        input_prompt,38        temperature=0.15,39        top_p=0.1,40        top_k=40, 41        repeat_penalty=1.1,42        max_tokens=1024,43        stop=[44            "<|prompter|>",45            "<|endoftext|>",46            "<|endoftext|> \n",47            "ASSISTANT:",48            "USER:",49            "SYSTEM:",50        ],51        stream=True,52    )53    for out in output:54        stream = copy.deepcopy(out)55        temp += stream["choices"][0]["text"]56        yield temp57 58    history = ["init", input_prompt]59 60 61demo = gr.ChatInterface(62    generate_text,63    title="llama-cpp-python on GPU",64    description="Running LLM with https://github.com/abetlen/llama-cpp-python",65    examples=["tell me everything about llamas"],66    cache_examples=True,67    retry_btn=None,68    undo_btn="Delete Previous",69    clear_btn="Clear",70)71demo.queue(concurrency_count=1, max_size=5)72demo.launch()