Kekulanus's picture
Upload app.py with huggingface_hub
04531fe verified
Raw
History Blame Contribute Delete
3.17 kB
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()