Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
|
3 |
+
import torch
|
4 |
+
import os
|
5 |
+
from threading import Thread
|
6 |
+
|
7 |
+
# Nonaktifkan cache Hugging Face untuk hemat penyimpanan
|
8 |
+
os.environ["HF_HUB_DISABLE_CACHE"] = "1"
|
9 |
+
|
10 |
+
# Muat model dan tokenizer
|
11 |
+
model_name = "Qwen/Qwen2-0.5B-Instruct"
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
14 |
+
model_name,
|
15 |
+
torch_dtype=torch.bfloat16, # bfloat16 untuk efisiensi CPU
|
16 |
+
device_map="cpu", # Paksa ke CPU untuk Space gratis
|
17 |
+
trust_remote_code=True,
|
18 |
+
low_cpu_mem_usage=True # Optimasi memori
|
19 |
+
)
|
20 |
+
|
21 |
+
# Fungsi untuk menghasilkan respons
|
22 |
+
def generate_response(user_input, chat_history):
|
23 |
+
if not user_input.strip():
|
24 |
+
return [("Error", "Masukkan teks tidak boleh kosong!")], chat_history
|
25 |
+
|
26 |
+
if not chat_history:
|
27 |
+
chat_history = []
|
28 |
+
|
29 |
+
# Format riwayat percakapan (batasi 5 interaksi terakhir untuk efisiensi)
|
30 |
+
messages = []
|
31 |
+
for user_msg, bot_msg in chat_history[-5:]:
|
32 |
+
messages.append({"role": "user", "content": user_msg})
|
33 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
34 |
+
|
35 |
+
# Tambahkan input pengguna saat ini
|
36 |
+
messages.append({"role": "user", "content": user_input})
|
37 |
+
|
38 |
+
# Buat prompt menggunakan format chat Qwen
|
39 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
40 |
+
|
41 |
+
# Tokenisasi input
|
42 |
+
inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to("cpu")
|
43 |
+
|
44 |
+
# Gunakan TextStreamer untuk streaming respons (meningkatkan UX)
|
45 |
+
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
46 |
+
|
47 |
+
# Generate respons di thread terpisah untuk responsivitas
|
48 |
+
def generate():
|
49 |
+
outputs = model.generate(
|
50 |
+
**inputs,
|
51 |
+
max_new_tokens=200, # Batasi token untuk kecepatan
|
52 |
+
do_sample=True,
|
53 |
+
temperature=0.75,
|
54 |
+
top_p=0.85,
|
55 |
+
eos_token_id=tokenizer.eos_token_id,
|
56 |
+
use_cache=True, # Cache untuk inferensi lebih cepat
|
57 |
+
streamer=streamer
|
58 |
+
)
|
59 |
+
return outputs
|
60 |
+
|
61 |
+
# Jalankan generasi di thread
|
62 |
+
thread = Thread(target=generate)
|
63 |
+
thread.start()
|
64 |
+
thread.join()
|
65 |
+
|
66 |
+
# Ambil respons dari output streamer (decode manual)
|
67 |
+
bot_response = tokenizer.decode(
|
68 |
+
model.generate(**inputs, max_new_tokens=200, do_sample=True, temperature=0.75, top_p=0.85)[0][inputs.input_ids.shape[-1]:],
|
69 |
+
skip_special_tokens=True
|
70 |
+
)
|
71 |
+
|
72 |
+
# Perbarui riwayat percakapan
|
73 |
+
chat_history.append((user_input, bot_response))
|
74 |
+
|
75 |
+
# Format output untuk Gradio Chatbot
|
76 |
+
return [(user_msg, bot_msg) for user_msg, bot_msg in chat_history], chat_history
|
77 |
+
|
78 |
+
# Fungsi untuk menghapus riwayat
|
79 |
+
def clear_history():
|
80 |
+
return [], []
|
81 |
+
|
82 |
+
# Antarmuka Gradio
|
83 |
+
with gr.Blocks(
|
84 |
+
theme=gr.themes.Monochrome(), # Tema modern dan bersih
|
85 |
+
css="""
|
86 |
+
#chatbot {border-radius: 10px; border: 1px solid #e0e0e0;}
|
87 |
+
.gradio-container {max-width: 800px; margin: auto;}
|
88 |
+
#input-box {border-radius: 8px;}
|
89 |
+
#submit-btn, #clear-btn {border-radius: 8px; background: #007bff; color: white;}
|
90 |
+
#submit-btn:hover, #clear-btn:hover {background: #0056b3;}
|
91 |
+
"""
|
92 |
+
) as demo:
|
93 |
+
gr.Markdown(
|
94 |
+
"""
|
95 |
+
# 💬 Chatbot Qwen (Alibaba)
|
96 |
+
Ajukan pertanyaan dan dapatkan respons cerdas dari model Qwen2-0.5B-Instruct!
|
97 |
+
"""
|
98 |
+
)
|
99 |
+
|
100 |
+
# Komponen UI
|
101 |
+
chatbot = gr.Chatbot(
|
102 |
+
label="Percakapan",
|
103 |
+
height=450,
|
104 |
+
show_label=False,
|
105 |
+
elem_id="chatbot",
|
106 |
+
bubble_full_width=False
|
107 |
+
)
|
108 |
+
with gr.Row():
|
109 |
+
user_input = gr.Textbox(
|
110 |
+
placeholder="Ketik pertanyaanmu di sini...",
|
111 |
+
show_label=False,
|
112 |
+
elem_id="input-box",
|
113 |
+
scale=4
|
114 |
+
)
|
115 |
+
submit_button = gr.Button("Kirim", elem_id="submit-btn", scale=1)
|
116 |
+
|
117 |
+
clear_button = gr.Button("Hapus Riwayat", elem_id="clear-btn")
|
118 |
+
|
119 |
+
# State untuk menyimpan riwayat percakapan
|
120 |
+
chat_history = gr.State([])
|
121 |
+
|
122 |
+
# Aksi tombol
|
123 |
+
submit_button.click(
|
124 |
+
fn=generate_response,
|
125 |
+
inputs=[user_input, chat_history],
|
126 |
+
outputs=[chatbot, chat_history],
|
127 |
+
_js="() => {document.querySelector('input').value = '';}" # Kosongkan input
|
128 |
+
)
|
129 |
+
clear_button.click(
|
130 |
+
fn=clear_history,
|
131 |
+
inputs=None,
|
132 |
+
outputs=[chatbot, chat_history]
|
133 |
+
)
|
134 |
+
|
135 |
+
# Luncurkan aplikasi
|
136 |
+
demo.launch()
|