Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
demo
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
-
|
|
|
1 |
+
# Import Library yang Diperlukan
|
2 |
+
from llama_cpp import Llama
|
3 |
+
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, Settings
|
4 |
+
from llama_index.core.llms import ChatMessage
|
5 |
+
from llama_index.llms.llama_cpp import LlamaCPP
|
6 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
7 |
+
from llama_index.core.node_parser import SentenceSplitter
|
8 |
import gradio as gr
|
9 |
+
import os
|
10 |
|
11 |
+
# Fungsi Inisialisasi Model Llama
|
12 |
+
def initialize_llama_model():
|
13 |
+
return Llama(
|
14 |
+
model_path="zephyr-7b-beta.Q4_K_M.gguf",
|
15 |
+
n_gpu_layers=-1, # Aktifkan untuk akselerasi GPU jika tersedia
|
16 |
+
)
|
17 |
|
18 |
+
# Fungsi untuk Menginisialisasi Index
|
19 |
+
def initialize_index():
|
20 |
+
# Tentukan dokumen input untuk pembacaan data
|
21 |
+
documents = SimpleDirectoryReader(input_files=[
|
22 |
+
"/content/cuti.txt",
|
23 |
+
"/content/disiplin kerja.txt",
|
24 |
+
"/content/fasilitas dan bantuan bagi serikat pekerja.txt",
|
25 |
+
"/content/fasilitas pekerja.txt",
|
26 |
+
"/content/hari dan jam kerja.txt",
|
27 |
+
"/content/hubungan pengusaha dan serikat pekerja.txt",
|
28 |
+
"/content/istilah-istilah.txt",
|
29 |
+
"/content/jaminan bagi serikat pekerja.txt",
|
30 |
+
"/content/jaminan kesehatan.txt",
|
31 |
+
"/content/jaminan sosial tenaga kerja.txt",
|
32 |
+
"/content/kenaikan upah.txt",
|
33 |
+
"/content/kerja lembur.txt",
|
34 |
+
"/content/kerja shift.txt",
|
35 |
+
"/content/keselamatan dan kesehatan kerja.txt",
|
36 |
+
"/content/kewajiban pihak-pihak.txt",
|
37 |
+
"/content/kompensasi dan pesangon.txt",
|
38 |
+
"/content/larangan.txt",
|
39 |
+
"/content/luas kesepakatan.txt",
|
40 |
+
"/content/masa berlaku, perubahan, dan perpanjangan pkb.txt",
|
41 |
+
"/content/mogok kerja.txt",
|
42 |
+
"/content/pelanggaran dan sanksi.txt",
|
43 |
+
"/content/pemutusan hubungan kerja.txt",
|
44 |
+
"/content/pendidikan.txt",
|
45 |
+
"/content/pengakuan hak.txt",
|
46 |
+
"/content/pengangkatan pegawai tetap.txt",
|
47 |
+
"/content/pengunduran diri.txt",
|
48 |
+
"/content/penilaian dan kenaikan jabatan.txt",
|
49 |
+
"/content/pensiun.txt",
|
50 |
+
"/content/penyelesaian keluh kesah.txt",
|
51 |
+
"/content/perhitungan upah lembur.txt",
|
52 |
+
"/content/perjalanan dinas.txt",
|
53 |
+
"/content/persyaratan menjadi pekerja.txt",
|
54 |
+
"/content/pesangon dan penghargaan masa kerja.txt",
|
55 |
+
"/content/pihak yang mengadakan perjanjian.txt",
|
56 |
+
"/content/sanksi.txt",
|
57 |
+
"/content/tata cara penyampaian pengaduan dan keluh kesah.txt",
|
58 |
+
"/content/tenaga kerja asing.txt",
|
59 |
+
"/content/tunjangan.txt",
|
60 |
+
"/content/uang pisah.txt",
|
61 |
+
"/content/upah.txt"]).load_data()
|
62 |
|
63 |
+
parser = SentenceSplitter(chunk_size=300, chunk_overlap=20)
|
64 |
+
nodes = parser.get_nodes_from_documents(documents)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
embedding = HuggingFaceEmbedding("BAAI/bge-base-en-v1.5")
|
67 |
+
Settings.llm = initialize_llama_model()
|
68 |
+
Settings.embed_model = embedding
|
69 |
+
index = VectorStoreIndex(nodes)
|
|
|
70 |
|
71 |
+
return index.as_query_engine(similarity_top_k=3)
|
72 |
|
73 |
+
# Inisialisasi Mesin Chat
|
74 |
+
def initialize_chat_engine(index):
|
75 |
+
from llama_index.core.prompts import PromptTemplate
|
76 |
+
from llama_index.core.chat_engine.condense_plus_context import CondensePlusContextChatEngine
|
77 |
+
retriever = index.as_retriever(similarity_top_k=3)
|
78 |
+
chat_engine = CondensePlusContextChatEngine.from_defaults(
|
79 |
+
retriever=retriever,
|
80 |
+
verbose=True,
|
81 |
+
)
|
82 |
+
return chat_engine
|
83 |
|
84 |
+
# Fungsi Utama untuk Mendapatkan Respon Chatbot
|
85 |
+
def generate_response(message, history, chat_engine):
|
86 |
+
response = chat_engine.stream_chat(message)
|
87 |
+
text = ""
|
88 |
+
for token in response.response_gen:
|
89 |
+
text += token
|
90 |
+
yield text
|
|
|
91 |
|
92 |
+
# Fungsi untuk Menghapus Riwayat
|
93 |
+
def clear_history(chat_engine):
|
94 |
+
chat_engine.reset()
|
95 |
|
96 |
+
# Inisialisasi Komponen Gradio untuk UI
|
97 |
+
def launch_gradio(chat_engine):
|
98 |
+
with gr.Blocks() as demo:
|
99 |
+
# Mengatur tombol untuk menghapus riwayat chat
|
100 |
+
clear_btn = gr.Button("Clear")
|
101 |
+
clear_btn.click(lambda: clear_history(chat_engine))
|
102 |
|
103 |
+
# Membuat antarmuka chat
|
104 |
+
chat_interface = gr.ChatInterface(lambda message, history: generate_response(message, history, chat_engine))
|
105 |
+
|
106 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
+
# Fungsi Utama untuk Menjalankan Aplikasi
|
109 |
+
def main():
|
110 |
+
# Inisialisasi index dan engine
|
111 |
+
index = initialize_index()
|
112 |
+
chat_engine = initialize_chat_engine(index)
|
113 |
+
# Luncurkan antarmuka
|
114 |
+
launch_gradio(chat_engine)
|
115 |
|
116 |
if __name__ == "__main__":
|
117 |
+
main()
|