File size: 1,376 Bytes
9ce478d
f14cf8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ce478d
 
f14cf8c
 
 
 
9ce478d
 
 
 
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
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

model_id = "STiFLeR7/Qwen2.5-3B-GPTQ"  # ✅ Your HF model repo

tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    device_map="auto",
    torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
    trust_remote_code=True
).eval()

def chat_fn(message, history):
    history = history or []
    prompt = ""
    for user, bot in history:
        prompt += f"User: {user}\nAssistant: {bot}\n"
    prompt += f"User: {message}\nAssistant:"

    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    with torch.no_grad():
        output = model.generate(
            **inputs,
            max_new_tokens=256,
            temperature=0.7,
            top_p=0.9,
            do_sample=True,
            pad_token_id=tokenizer.eos_token_id
        )

    decoded = tokenizer.decode(output[0], skip_special_tokens=True)
    reply = decoded.split("Assistant:")[-1].strip()
    history.append((message, reply))
    return history, history

demo = gr.ChatInterface(
    fn=chat_fn,
    title="🧠 Qwen2.5-3B GPTQ Chatbot",
    description="Running Qwen2.5-3B (GPTQ) from Hugging Face model repository",
    theme="soft",
)

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