Update
Browse files
app.py
CHANGED
@@ -1,8 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
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"
|
@@ -21,7 +20,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
21 |
# Fungsi untuk menghasilkan respons
|
22 |
def generate_response(user_input, chat_history):
|
23 |
if not user_input.strip():
|
24 |
-
return [
|
25 |
|
26 |
if not chat_history:
|
27 |
chat_history = []
|
@@ -41,39 +40,29 @@ def generate_response(user_input, chat_history):
|
|
41 |
# Tokenisasi input
|
42 |
inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to("cpu")
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
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 [
|
|
|
|
|
|
|
|
|
77 |
|
78 |
# Fungsi untuk menghapus riwayat
|
79 |
def clear_history():
|
@@ -83,7 +72,7 @@ def clear_history():
|
|
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;}
|
@@ -99,11 +88,10 @@ with gr.Blocks(
|
|
99 |
|
100 |
# Komponen UI
|
101 |
chatbot = gr.Chatbot(
|
102 |
-
|
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(
|
@@ -123,8 +111,7 @@ with gr.Blocks(
|
|
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,
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
import os
|
|
|
5 |
|
6 |
# Nonaktifkan cache Hugging Face untuk hemat penyimpanan
|
7 |
os.environ["HF_HUB_DISABLE_CACHE"] = "1"
|
|
|
20 |
# Fungsi untuk menghasilkan respons
|
21 |
def generate_response(user_input, chat_history):
|
22 |
if not user_input.strip():
|
23 |
+
return [{"role": "assistant", "content": "Masukkan teks tidak boleh kosong!"}], chat_history
|
24 |
|
25 |
if not chat_history:
|
26 |
chat_history = []
|
|
|
40 |
# Tokenisasi input
|
41 |
inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to("cpu")
|
42 |
|
43 |
+
# Generate respons
|
44 |
+
outputs = model.generate(
|
45 |
+
**inputs,
|
46 |
+
max_new_tokens=200, # Batasi token untuk kecepatan
|
47 |
+
do_sample=True,
|
48 |
+
temperature=0.75,
|
49 |
+
top_p=0.85,
|
50 |
+
eos_token_id=tokenizer.eos_token_id,
|
51 |
+
use_cache=True # Cache untuk inferensi lebih cepat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
)
|
53 |
|
54 |
+
# Decode respons
|
55 |
+
bot_response = tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
|
56 |
+
|
57 |
# Perbarui riwayat percakapan
|
58 |
chat_history.append((user_input, bot_response))
|
59 |
|
60 |
+
# Format output untuk Gradio Chatbot (format messages)
|
61 |
+
return [
|
62 |
+
{"role": "user" if i % 2 == 0 else "assistant", "content": msg}
|
63 |
+
for i, (user_msg, bot_msg) in enumerate(chat_history)
|
64 |
+
for msg in [user_msg, bot_msg]
|
65 |
+
], chat_history
|
66 |
|
67 |
# Fungsi untuk menghapus riwayat
|
68 |
def clear_history():
|
|
|
72 |
with gr.Blocks(
|
73 |
theme=gr.themes.Monochrome(), # Tema modern dan bersih
|
74 |
css="""
|
75 |
+
#chatbot {border-radius: 10px; border: 1px solid #e0e0e0; padding: 10px;}
|
76 |
.gradio-container {max-width: 800px; margin: auto;}
|
77 |
#input-box {border-radius: 8px;}
|
78 |
#submit-btn, #clear-btn {border-radius: 8px; background: #007bff; color: white;}
|
|
|
88 |
|
89 |
# Komponen UI
|
90 |
chatbot = gr.Chatbot(
|
91 |
+
type="messages", # Gunakan format messages untuk kompatibilitas
|
92 |
height=450,
|
93 |
show_label=False,
|
94 |
+
elem_id="chatbot"
|
|
|
95 |
)
|
96 |
with gr.Row():
|
97 |
user_input = gr.Textbox(
|
|
|
111 |
submit_button.click(
|
112 |
fn=generate_response,
|
113 |
inputs=[user_input, chat_history],
|
114 |
+
outputs=[chatbot, chat_history]
|
|
|
115 |
)
|
116 |
clear_button.click(
|
117 |
fn=clear_history,
|