Update app.py
Browse files
app.py
CHANGED
@@ -96,6 +96,9 @@ def initialize_chat_engine(index):
|
|
96 |
|
97 |
# Fungsi untuk menghasilkan respons chatbot
|
98 |
def generate_response(message, history, chat_engine):
|
|
|
|
|
|
|
99 |
chat_messages = [
|
100 |
ChatMessage(
|
101 |
role="system",
|
@@ -108,35 +111,33 @@ def generate_response(message, history, chat_engine):
|
|
108 |
]
|
109 |
response = chat_engine.stream_chat(message)
|
110 |
text = "".join(response.response_gen) # Gabungkan semua token menjadi string
|
111 |
-
|
|
|
112 |
return history
|
113 |
|
114 |
def clear_history(chat_engine):
|
115 |
chat_engine.clear()
|
116 |
|
117 |
-
# Inisialisasi Komponen Gradio untuk UI
|
118 |
-
def launch_gradio(chat_engine):
|
119 |
-
with gr.Blocks() as demo:
|
120 |
-
# Mengatur tombol untuk menghapus riwayat chat
|
121 |
-
clear_btn = gr.Button("Clear")
|
122 |
-
clear_btn.click(lambda: clear_history(chat_engine))
|
123 |
-
|
124 |
-
# Membuat antarmuka chat
|
125 |
-
chat_interface = gr.ChatInterface(
|
126 |
-
lambda message, history: generate_response(message, history, chat_engine)
|
127 |
-
)
|
128 |
-
demo.launch()
|
129 |
-
|
130 |
# Fungsi Utama untuk Menjalankan Aplikasi
|
131 |
def main():
|
132 |
# Unduh model dan inisialisasi pengaturan
|
133 |
model_path = initialize_llama_model()
|
134 |
-
initialize_settings(model_path)
|
135 |
-
|
|
|
136 |
index = initialize_index()
|
137 |
-
chat_engine = initialize_chat_engine(index)
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
if __name__ == "__main__":
|
142 |
main()
|
|
|
96 |
|
97 |
# Fungsi untuk menghasilkan respons chatbot
|
98 |
def generate_response(message, history, chat_engine):
|
99 |
+
if history is None: # ✅ Cek jika history None
|
100 |
+
history = []
|
101 |
+
|
102 |
chat_messages = [
|
103 |
ChatMessage(
|
104 |
role="system",
|
|
|
111 |
]
|
112 |
response = chat_engine.stream_chat(message)
|
113 |
text = "".join(response.response_gen) # Gabungkan semua token menjadi string
|
114 |
+
|
115 |
+
history.append((message, text)) # ✅ Sekarang history sudah pasti berupa list
|
116 |
return history
|
117 |
|
118 |
def clear_history(chat_engine):
|
119 |
chat_engine.clear()
|
120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
# Fungsi Utama untuk Menjalankan Aplikasi
|
122 |
def main():
|
123 |
# Unduh model dan inisialisasi pengaturan
|
124 |
model_path = initialize_llama_model()
|
125 |
+
initialize_settings(model_path)
|
126 |
+
|
127 |
+
# Inisialisasi index dan chat engine
|
128 |
index = initialize_index()
|
129 |
+
chat_engine = initialize_chat_engine(index)
|
130 |
+
|
131 |
+
# Fungsi untuk chat
|
132 |
+
def chatbot_response(message, history):
|
133 |
+
return generate_response(message, history, chat_engine)
|
134 |
+
|
135 |
+
# Luncurkan Gradio UI
|
136 |
+
gr.Interface(
|
137 |
+
fn=chatbot_response,
|
138 |
+
inputs=["text"],
|
139 |
+
outputs=["text"],
|
140 |
+
).launch()
|
141 |
+
|
142 |
if __name__ == "__main__":
|
143 |
main()
|