Team Ai
Apppublic

sithumonline/llama-cpp-python-cuda-gradio

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py76 linesDownload Raw Back to root
1import os2import gradio as gr3import copy4from llama_cpp import Llama5from huggingface_hub import hf_hub_download  6 7 8llm = Llama(9    model_path=hf_hub_download(10        repo_id=os.environ.get("REPO_ID", "TheBloke/Llama-2-7b-Chat-GGUF"),11        filename=os.environ.get("MODEL_FILE", "llama-2-7b-chat.Q5_0.gguf"),12    ),13    n_ctx=2048,14    n_gpu_layers=50, # change n_gpu_layers if you have more or less VRAM 15)16 17 18def generate_text(19    message,20    history: list[tuple[str, str]],21    system_message,22    max_tokens,23    temperature,24    top_p,25):26    temp = ""27    input_prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n "28    for interaction in history:29        input_prompt = input_prompt + str(interaction[0]) + " [/INST] " + str(interaction[1]) + " </s><s> [INST] "30 31    input_prompt = input_prompt + str(message) + " [/INST] "32 33    output = llm(34        input_prompt,35        temperature=temperature,36        top_p=top_p,37        top_k=40,38        repeat_penalty=1.1,39        max_tokens=max_tokens,40        stop=[41            "<|prompter|>",42            "<|endoftext|>",43            "<|endoftext|> \n",44            "ASSISTANT:",45            "USER:",46            "SYSTEM:",47        ],48        stream=True,49    )50    for out in output:51        stream = copy.deepcopy(out)52        temp += stream["choices"][0]["text"]53        yield temp54 55 56demo = gr.ChatInterface(57    generate_text,58    title="llama-cpp-python on GPU",59    description="Running LLM with https://github.com/abetlen/llama-cpp-python",60    examples=[61        ['How to setup a human base on Mars? Give short answer.'],62        ['Explain theory of relativity to me like I’m 8 years old.'],63        ['What is 9,000 * 9,000?'],64        ['Write a pun-filled happy birthday message to my friend Alex.'],65        ['Justify why a penguin might make a good king of the jungle.']66    ],67    cache_examples=False,68    retry_btn=None,69    undo_btn="Delete Previous",70    clear_btn="Clear",71)72demo.queue(concurrency_count=1, max_size=5)73 74if __name__ == "__main__":75    demo.launch()76