File size: 3,172 Bytes
2800d18
 
04531fe
2800d18
 
04531fe
2800d18
 
 
 
04531fe
 
 
 
 
 
 
2800d18
 
 
04531fe
2800d18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04531fe
2800d18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
import spaces
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
import torch

MODEL_ID = "IQuestLab/IQuest-Coder-V1-40B-Loop-Instruct"

print("Loading tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)

print("Loading model with 4-bit quantization...")
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_use_double_quant=True,
)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    trust_remote_code=True,
    quantization_config=bnb_config,
    device_map="cuda",
)
model.eval()
print("Model loaded.")


@spaces.GPU(duration=120)
def generate(message, history, system_prompt, max_tokens, temperature, top_p):
    messages = [{"role": "system", "content": system_prompt}]
    for user_msg, assistant_msg in history:
        messages.append({"role": "user", "content": user_msg})
        if assistant_msg:
            messages.append({"role": "assistant", "content": assistant_msg})
    messages.append({"role": "user", "content": message})

    text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    inputs = tokenizer(text, return_tensors="pt").to(model.device)

    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
            do_sample=True,
        )

    response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
    return response


with gr.Blocks(title="IQuest LoopCoder 40B", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# IQuest LoopCoder V1 40B\n40B params, loop architecture, 4-bit quantized for GPU inference.")

    with gr.Row():
        with gr.Column(scale=3):
            chatbot = gr.Chatbot(height=500, label="Chat")
            msg = gr.Textbox(placeholder="Ask something...", label="Message", lines=2)
            with gr.Row():
                submit = gr.Button("Send", variant="primary")
                clear = gr.Button("Clear")

        with gr.Column(scale=1):
            system_prompt = gr.Textbox(
                value="You are LoopCoder, a helpful assistant developed by IQuest.",
                label="System prompt",
                lines=3,
            )
            max_tokens = gr.Slider(64, 8192, value=2048, step=64, label="Max tokens")
            temperature = gr.Slider(0.0, 2.0, value=0.3, step=0.05, label="Temperature")
            top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top P")

    def respond(message, chat_history, sys_prompt, max_tok, temp, top):
        response = generate(message, chat_history, sys_prompt, max_tok, temp, top)
        chat_history.append((message, response))
        return "", chat_history

    msg.submit(respond, [msg, chatbot, system_prompt, max_tokens, temperature, top_p], [msg, chatbot])
    submit.click(respond, [msg, chatbot, system_prompt, max_tokens, temperature, top_p], [msg, chatbot])
    clear.click(lambda: ([], ""), outputs=[chatbot, msg])

demo.launch()