|
import gradio as gr |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
import torch |
|
import os |
|
|
|
|
|
os.environ["HF_HUB_DISABLE_CACHE"] = "1" |
|
|
|
|
|
model_name = "Qwen/Qwen2-0.5B-Instruct" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True) |
|
model = AutoModelForCausalLM.from_pretrained( |
|
model_name, |
|
torch_dtype=torch.bfloat16, |
|
device_map="cpu", |
|
trust_remote_code=True, |
|
low_cpu_mem_usage=True |
|
) |
|
|
|
|
|
def generate_response(user_input, chat_history): |
|
if not user_input.strip(): |
|
return [{"role": "assistant", "content": "Masukkan teks tidak boleh kosong!"}], chat_history |
|
|
|
if not chat_history: |
|
chat_history = [] |
|
|
|
|
|
messages = [] |
|
for user_msg, bot_msg in chat_history[-5:]: |
|
messages.append({"role": "user", "content": user_msg}) |
|
messages.append({"role": "assistant", "content": bot_msg}) |
|
|
|
|
|
messages.append({"role": "user", "content": user_input}) |
|
|
|
|
|
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) |
|
|
|
|
|
inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to("cpu") |
|
|
|
|
|
outputs = model.generate( |
|
**inputs, |
|
max_new_tokens=200, |
|
do_sample=True, |
|
temperature=0.75, |
|
top_p=0.85, |
|
eos_token_id=tokenizer.eos_token_id, |
|
use_cache=True |
|
) |
|
|
|
|
|
bot_response = tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True) |
|
|
|
|
|
chat_history.append((user_input, bot_response)) |
|
|
|
|
|
return [ |
|
{"role": "user" if i % 2 == 0 else "assistant", "content": msg} |
|
for i, (user_msg, bot_msg) in enumerate(chat_history) |
|
for msg in [user_msg, bot_msg] |
|
], chat_history |
|
|
|
|
|
def clear_history(): |
|
return [], [] |
|
|
|
|
|
with gr.Blocks( |
|
theme=gr.themes.Monochrome(), |
|
css=""" |
|
#chatbot {border-radius: 10px; border: 1px solid #e0e0e0; padding: 10px;} |
|
.gradio-container {max-width: 800px; margin: auto;} |
|
#input-box {border-radius: 8px;} |
|
#submit-btn, #clear-btn {border-radius: 8px; background: #007bff; color: white;} |
|
#submit-btn:hover, #clear-btn:hover {background: #0056b3;} |
|
""" |
|
) as demo: |
|
gr.Markdown( |
|
""" |
|
# π¬ Chatbot Qwen (Alibaba) |
|
Ajukan pertanyaan dan dapatkan respons cerdas dari model Qwen2-0.5B-Instruct! |
|
""" |
|
) |
|
|
|
|
|
chatbot = gr.Chatbot( |
|
type="messages", |
|
height=450, |
|
show_label=False, |
|
elem_id="chatbot" |
|
) |
|
with gr.Row(): |
|
user_input = gr.Textbox( |
|
placeholder="Ketik pertanyaanmu di sini...", |
|
show_label=False, |
|
elem_id="input-box", |
|
scale=4 |
|
) |
|
submit_button = gr.Button("Kirim", elem_id="submit-btn", scale=1) |
|
|
|
clear_button = gr.Button("Hapus Riwayat", elem_id="clear-btn") |
|
|
|
|
|
chat_history = gr.State([]) |
|
|
|
|
|
submit_button.click( |
|
fn=generate_response, |
|
inputs=[user_input, chat_history], |
|
outputs=[chatbot, chat_history] |
|
) |
|
clear_button.click( |
|
fn=clear_history, |
|
inputs=None, |
|
outputs=[chatbot, chat_history] |
|
) |
|
|
|
|
|
demo.launch() |
|
|