File size: 1,988 Bytes
eee4d14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1400d9
eee4d14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4e1d16
 
 
 
eee4d14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import gradio as gr
import copy
import time
import ctypes  # to run on C api directly
import llama_cpp
from llama_cpp import Llama
from huggingface_hub import hf_hub_download  # load from huggingfaces


llm = Llama(
    model_path=hf_hub_download(
        repo_id="TheBloke/WizardLM-7B-uncensored-GGML",
        filename="WizardLM-7B-uncensored.ggmlv3.q4_0.bin",
    ),
    n_ctx=2048,
    n_gpu_layers=30
)  # download model from hf/ n_ctx=2048 for high ccontext length

history = []

pre_prompt = " The user and the AI are having a conversation : <|endoftext|> \n "


def generate_text(input_text, history):

    temp = ""
    if history == []:
        input_text_with_history = (
            f"SYSTEM:{pre_prompt}"
            + "\n"
            + f"USER: {input_text} "
            + "\n"
            + " ASSISTANT:"
        )
    else:
        input_text_with_history = f"{history[-1][1]}" + "\n"
        input_text_with_history += f"USER: {input_text}" + "\n" + " ASSISTANT:"

    output = llm(
        input_text_with_history,
        temperature=0.15,
        top_p=0.1,
        top_k=40, 
        repeat_penalty=1.1,
        max_tokens=1024,
        stop=[
            "<|prompter|>",
            "<|endoftext|>",
            "<|endoftext|> \n",
            "ASSISTANT:",
            "USER:",
            "SYSTEM:",
        ],
        stream=True,
    )
    for out in output:
        stream = copy.deepcopy(out)
        temp += stream["choices"][0]["text"]
        yield temp

    history = ["init", input_text_with_history]


demo = gr.ChatInterface(
    generate_text,
    title="llama-cpp-python on GPU",
    description="Running LLM with https://github.com/abetlen/llama-cpp-python. btw the text streaming thing was the hardest thing to impliment",
    examples=["Hello", "Am I cool?", "Are tomatoes vegetables?"],
    cache_examples=True,
    retry_btn=None,
    undo_btn="Delete Previous",
    clear_btn="Clear",
)
demo.queue(concurrency_count=1, max_size=5)
demo.launch()