File size: 2,459 Bytes
eb50141
7c2e9ad
 
eb50141
4bf96b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa14f0d
4bf96b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb50141
 
4bf96b6
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
from llama_cpp import Llama
from huggingface_hub import hf_hub_download

# Download the model
model_name = "Mykes/med_tinyllama_gguf"
filename = "unsloth.Q4_K_M.gguf"
model_path = hf_hub_download(repo_id=model_name, filename=filename)

# Initialize the model
# model = Llama(model_path=model_path, n_ctx=2048, n_threads=4, n_batch=32, use_mmap=True, use_mlock=True, rope_freq_base=10000, rope_freq_scale=1.0)
model = Llama(model_path=model_path, n_ctx=256, n_threads=2, n_batch=8, use_mlock=True)
# def preload_model(model, preload_tokens=1024):
#     # Dummy call to load model into RAM by accessing parts of it
#     try:
#         dummy_input = " " * preload_tokens
#         _ = model(dummy_input, max_tokens=1)
#         print("Model preloaded into RAM.")
#     except Exception as e:
#         print(f"Error preloading model: {e}")

# # Preload the model into RAM
# preload_model(model)
def respond(
    message,
    history: list[tuple[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
):
    history = history[-3:]
    # Construct the prompt
    prompt = f"<s>{system_message}\n\n"
    for user_msg, assistant_msg in history:
        prompt += f"<|user|>{user_msg}<|end|></s> <|assistant|>{assistant_msg}<|end|></s>"
    prompt += f"<|user|>{message}<|end|></s> <|assistant|>"

    # Generate response
    response = ""
    for token in model(
        prompt,
        max_tokens=max_tokens,
        temperature=temperature,
        top_p=top_p,
        stream=True,
        stop=["<|end|>", "</s>"]
    ):
        response += token['choices'][0]['text']
        yield response.strip()

# Create the Gradio interface
demo = gr.ChatInterface(
    respond,
    undo_btn="Отменить",
    clear_btn="Очистить",
    additional_inputs=[
        # gr.Textbox(value="You are a friendly medical assistant.", label="System message"),
        gr.Textbox(value="", label="System message"),
        gr.Slider(minimum=128, maximum=4096, value=2048, step=1, label="Max new tokens"),
        gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
        gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.9,
            step=0.05,
            label="Top-p (nucleus sampling)",
        ),
    ],
    title="Med TinyLlama Chat",
    description="Chat with the Med TinyLlama model for medical information.",
)

if __name__ == "__main__":
    demo.launch()