Update app.py
Browse files
app.py
CHANGED
@@ -1,261 +1,101 @@
|
|
1 |
-
#
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
Original file is located at
|
7 |
-
https://colab.research.google.com/drive/1mbREVflL7r6y0hCUPE-MD3VzJJUs5OfR
|
8 |
-
"""
|
9 |
-
|
10 |
-
!huggingface-cli download TheBloke/zephyr-7B-beta-GGUF zephyr-7b-beta.Q4_K_M.gguf --local-dir . --local-dir-use-symlinks False
|
11 |
-
|
12 |
-
!CMAKE_ARGS="-DGGML_CUDA=on" pip install llama-cpp-python
|
13 |
-
|
14 |
from llama_cpp import Llama
|
15 |
-
|
16 |
-
llm = Llama(
|
17 |
-
model_path="/content/zephyr-7b-beta.Q4_K_M.gguf",
|
18 |
-
n_gpu_layers=-1, # Uncomment to use GPU acceleration
|
19 |
-
# seed=1337, # Uncomment to set a specific seed
|
20 |
-
# n_ctx=2048, # Uncomment to increase the context window
|
21 |
-
)
|
22 |
-
|
23 |
-
llm("What is Large Language Model?", max_tokens=128)
|
24 |
-
|
25 |
-
!pip install llama-index gradio
|
26 |
-
|
27 |
-
pip install llama-index-llms-llama-cpp
|
28 |
-
|
29 |
-
from typing import Sequence, Optional
|
30 |
-
|
31 |
-
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
|
32 |
-
from llama_index.llms.llama_cpp import LlamaCPP
|
33 |
-
from llama_index.llms.llama_cpp.llama_utils import (
|
34 |
-
messages_to_prompt,
|
35 |
-
completion_to_prompt,
|
36 |
-
)
|
37 |
from llama_index.core.llms import ChatMessage
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
prompt += f"{message.content}</s>\n"
|
47 |
-
|
48 |
-
return prompt + "<|assistant|>\n"
|
49 |
-
|
50 |
-
llm = LlamaCPP(
|
51 |
-
# You can pass in the URL to a GGML model to download it automatically
|
52 |
-
model_url=None,
|
53 |
-
# optionally, you can set the path to a pre-downloaded model instead of model_url
|
54 |
-
model_path="/content/zephyr-7b-beta.Q4_K_M.gguf",
|
55 |
-
temperature=0.1,
|
56 |
-
max_new_tokens=1000,
|
57 |
-
# llama2 has a context window of 4096 tokens, but we set it lower to allow for some wiggle room
|
58 |
-
context_window=3900,
|
59 |
-
# kwargs to pass to __call__()
|
60 |
-
generate_kwargs={},
|
61 |
-
# kwargs to pass to __init__()
|
62 |
-
# set to at least 1 to use GPU
|
63 |
-
model_kwargs={"n_gpu_layers": 1},
|
64 |
-
# transform inputs into Llama2 format
|
65 |
-
messages_to_prompt=messages_to_prompt,
|
66 |
-
completion_to_prompt=completion_to_prompt,
|
67 |
-
verbose=True,
|
68 |
-
)
|
69 |
-
|
70 |
-
messages = [
|
71 |
-
ChatMessage(
|
72 |
-
role="system",
|
73 |
-
content="Anda adalah chatbot yang selalu menjawab pertanyaan secara singkat, ramah, dan jelas dalam bahasa Indonesia."
|
74 |
-
),
|
75 |
-
ChatMessage(
|
76 |
-
role="user",
|
77 |
-
content="There's a llama on my lawn, how can I get rid of him?"
|
78 |
)
|
79 |
-
|
80 |
-
|
81 |
-
# zephyr_messages_to_prompt(messages)
|
82 |
-
|
83 |
-
response = llm.stream_chat(messages)
|
84 |
-
for r in response:
|
85 |
-
print(r.delta, end="", flush=True)
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
-
|
|
|
96 |
chat_messages = [
|
97 |
ChatMessage(
|
98 |
role="system",
|
99 |
content="Anda adalah chatbot yang selalu menjawab pertanyaan secara singkat, ramah, dan jelas dalam bahasa Indonesia."
|
100 |
),
|
101 |
]
|
102 |
-
for human, ai in history:
|
103 |
-
chat_messages.append(ChatMessage(
|
104 |
-
role="user",
|
105 |
-
content=human
|
106 |
-
))
|
107 |
-
chat_messages.append(ChatMessage(
|
108 |
-
role="assistant",
|
109 |
-
content=ai
|
110 |
-
))
|
111 |
-
|
112 |
-
chat_messages.append(ChatMessage(
|
113 |
-
role="user",
|
114 |
-
content=message
|
115 |
-
))
|
116 |
-
|
117 |
-
response = llm.stream_chat(chat_messages)
|
118 |
-
text = ""
|
119 |
-
for r in response:
|
120 |
-
text += r.delta
|
121 |
-
yield text
|
122 |
-
|
123 |
-
gr.ChatInterface(generate_response).launch()
|
124 |
-
|
125 |
-
from google.colab import files
|
126 |
-
uploaded = files.upload()
|
127 |
-
for filename in uploaded.keys():
|
128 |
-
print(uploaded[filename])
|
129 |
-
|
130 |
-
!pip install pypdf -q
|
131 |
-
|
132 |
-
from llama_index.core import SimpleDirectoryReader
|
133 |
-
documents = SimpleDirectoryReader(input_files=["/content/bahandokumen/K3.txt",
|
134 |
-
"/content/bahandokumen/bonus.txt",
|
135 |
-
"/content/bahandokumen/jadwallembur.txt",
|
136 |
-
"/content/bahandokumen/datalembur.txt",
|
137 |
-
"/content/bahandokumen/absensi.txt",
|
138 |
-
"/content/bahandokumen/sisacuti.txt",
|
139 |
-
"/content/bahandokumen/target.txt",
|
140 |
-
"/content/bahandokumen/cuti.txt",
|
141 |
-
"/content/bahandokumen/disiplinkerja.txt",
|
142 |
-
"/content/bahandokumen/fasilitas&bantuan.txt",
|
143 |
-
"/content/bahandokumen/fasilitaskerja.txt",
|
144 |
-
"/content/bahandokumen/hak.txt",
|
145 |
-
"/content/bahandokumen/hubunganpengusaha&serikat.txt",
|
146 |
-
"/content/bahandokumen/istilah.txt",
|
147 |
-
"/content/bahandokumen/jaminanserikat.txt",
|
148 |
-
"/content/bahandokumen/jamkes.txt",
|
149 |
-
"/content/bahandokumen/jamsos.txt",
|
150 |
-
"/content/bahandokumen/keluhkesah.txt",
|
151 |
-
"/content/bahandokumen/kenaikanupah.txt",
|
152 |
-
"/content/bahandokumen/kewajiban.txt",
|
153 |
-
"/content/bahandokumen/kompensasi.txt",
|
154 |
-
"/content/bahandokumen/larangan.txt",
|
155 |
-
"/content/bahandokumen/lembur.txt",
|
156 |
-
"/content/bahandokumen/luaskesepakatan.txt",
|
157 |
-
"/content/bahandokumen/mogok.txt",
|
158 |
-
"/content/bahandokumen/pelanggaran&sanksi.txt",
|
159 |
-
"/content/bahandokumen/pendidikan.txt",
|
160 |
-
"/content/bahandokumen/pengangkatan.txt",
|
161 |
-
"/content/bahandokumen/penilaian&promosi.txt",
|
162 |
-
"/content/bahandokumen/pensiun.txt",
|
163 |
-
"/content/bahandokumen/perjadin.txt",
|
164 |
-
"/content/bahandokumen/pesangon.txt",
|
165 |
-
"/content/bahandokumen/phk.txt",
|
166 |
-
"/content/bahandokumen/pihak.txt",
|
167 |
-
"/content/bahandokumen/pkb.txt",
|
168 |
-
"/content/bahandokumen/resign.txt",
|
169 |
-
"/content/bahandokumen/sanksi.txt",
|
170 |
-
"/content/bahandokumen/shift.txt",
|
171 |
-
"/content/bahandokumen/syaratkerja.txt",
|
172 |
-
"/content/bahandokumen/tatacara.txt",
|
173 |
-
"/content/bahandokumen/tka.txt",
|
174 |
-
"/content/bahandokumen/tunjangan.txt",
|
175 |
-
"/content/bahandokumen/uangpisah.txt",
|
176 |
-
"/content/bahandokumen/upah.txt",
|
177 |
-
"/content/bahandokumen/upahlembur.txt",
|
178 |
-
"/content/bahandokumen/waktukerja.txt"]).load_data()
|
179 |
-
|
180 |
-
from llama_index.core.node_parser import SentenceSplitter
|
181 |
-
parser = SentenceSplitter(chunk_size=300, chunk_overlap=20)
|
182 |
-
nodes = parser.get_nodes_from_documents(documents)
|
183 |
-
|
184 |
-
nodes[1].get_content()
|
185 |
-
|
186 |
-
!pip install llama-index-embeddings-huggingface
|
187 |
-
|
188 |
-
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
189 |
-
from llama_index.core import Settings
|
190 |
-
|
191 |
-
embedding = HuggingFaceEmbedding("BAAI/bge-base-en-v1.5")
|
192 |
-
Settings.llm=llm
|
193 |
-
Settings.embed_model=embedding
|
194 |
-
|
195 |
-
from llama_index.core import VectorStoreIndex
|
196 |
-
|
197 |
-
index = VectorStoreIndex(nodes)
|
198 |
-
|
199 |
-
retriever = index.as_retriever(similarity_top_k=3)
|
200 |
-
|
201 |
-
results = retriever.retrieve("Sebutkan Jadwal Lembur Tahun 2024?")
|
202 |
-
|
203 |
-
len(results)
|
204 |
-
|
205 |
-
results[0].get_content()
|
206 |
-
|
207 |
-
query_engine = index.as_query_engine(similarity_top_k=3)
|
208 |
-
response = query_engine.query("Sebutkan Jadwal Lembur Tahun 2024?")
|
209 |
-
response.response
|
210 |
-
|
211 |
-
from llama_index.core.prompts import PromptTemplate
|
212 |
-
from llama_index.core.llms import ChatMessage, MessageRole
|
213 |
-
from llama_index.core.chat_engine.condense_plus_context import (
|
214 |
-
CondensePlusContextChatEngine,
|
215 |
-
)
|
216 |
-
|
217 |
-
chat_engine = CondensePlusContextChatEngine.from_defaults(
|
218 |
-
retriever=retriever,
|
219 |
-
verbose=True,
|
220 |
-
)
|
221 |
-
|
222 |
-
response = chat_engine.chat("Sebutkan Jadwal Lembur Tahun 2024?")
|
223 |
-
|
224 |
-
response.response
|
225 |
-
|
226 |
-
response = chat_engine.chat("Apakah anda bisa menjelaskan target Bulan Januari?")
|
227 |
-
|
228 |
-
response.response
|
229 |
-
|
230 |
-
import gradio as gr
|
231 |
-
|
232 |
-
chat_engine.reset()
|
233 |
-
|
234 |
-
def generate_response(message, history):
|
235 |
-
chat_messages = [
|
236 |
-
ChatMessage(
|
237 |
-
role="system",
|
238 |
-
content="Anda adalah chatbot yang selalu pertanyaan dalam bahasa Indonesia dengan singkat dan ramah."
|
239 |
-
),
|
240 |
-
]
|
241 |
response = chat_engine.stream_chat(message)
|
242 |
-
text = ""
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import Library yang Diperlukan
|
2 |
+
import gradio as gr
|
3 |
+
import shutil
|
4 |
+
import os
|
5 |
+
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from llama_cpp import Llama
|
7 |
+
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, Settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from llama_index.core.llms import ChatMessage
|
9 |
+
from llama_index.llms.llama_cpp import LlamaCPP
|
10 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
11 |
+
from huggingface_hub import hf_hub_download
|
12 |
+
from llama_index.core.node_parser import SentenceSplitter
|
13 |
|
14 |
+
# Fungsi untuk mengunduh model Llama
|
15 |
+
def initialize_llama_model():
|
16 |
+
# Unduh model jika belum ada di direktori kerja
|
17 |
+
model_path = hf_hub_download(
|
18 |
+
repo_id="TheBLoke/zephyr-7b-beta-GGUF", # Nama repo model
|
19 |
+
filename="zephyr-7b-beta.Q4_K_M.gguf", # Nama file model
|
20 |
+
cache_dir="./models" # Lokasi direktori untuk menyimpan model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
)
|
22 |
+
return model_path
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
# Fungsi untuk mengatur konfigurasi Settings
|
25 |
+
def initialize_settings(model_path):
|
26 |
+
Settings.llm = LlamaCPP(
|
27 |
+
model_path=model_path,
|
28 |
+
temperature=0.7,
|
29 |
+
)
|
30 |
|
31 |
+
# Fungsi untuk Menginisialisasi Index
|
32 |
+
def initialize_index():
|
33 |
+
# Tentukan dokumen input untuk pembacaan data
|
34 |
+
documents = SimpleDirectoryReader(input_files=["bahandokumen/K3.txt",
|
35 |
+
"bahandokumen/bonus.txt",
|
36 |
+
"bahandokumen/cuti.txt",
|
37 |
+
"bahandokumen/disiplinkerja.txt",
|
38 |
+
"bahandokumen/fasilitas&bantuan.txt",
|
39 |
+
"bahandokumen/upahlembur.txt",
|
40 |
+
"bahandokumen/waktukerja.txt"]).load_data()
|
41 |
+
|
42 |
+
parser = SentenceSplitter(chunk_size=150, chunk_overlap=10)
|
43 |
+
nodes = parser.get_nodes_from_documents(documents)
|
44 |
+
embedding = HuggingFaceEmbedding("BAAI/bge-base-en-v1.5")
|
45 |
+
Settings.embed_model = embedding
|
46 |
+
index = VectorStoreIndex(nodes)
|
47 |
+
return index
|
48 |
+
|
49 |
+
# Inisialisasi Mesin Chat
|
50 |
+
def initialize_chat_engine(index):
|
51 |
+
from llama_index.core.prompts import PromptTemplate
|
52 |
+
from llama_index.core.chat_engine.condense_plus_context import CondensePlusContextChatEngine
|
53 |
+
retriever = index.as_retriever(similarity_top_k=3)
|
54 |
+
chat_engine = CondensePlusContextChatEngine.from_defaults(
|
55 |
+
retriever=retriever,
|
56 |
+
verbose=True,
|
57 |
+
)
|
58 |
+
return chat_engine
|
59 |
|
60 |
+
# Fungsi untuk menghasilkan respons chatbot
|
61 |
+
def generate_response(message, history, chat_engine):
|
62 |
chat_messages = [
|
63 |
ChatMessage(
|
64 |
role="system",
|
65 |
content="Anda adalah chatbot yang selalu menjawab pertanyaan secara singkat, ramah, dan jelas dalam bahasa Indonesia."
|
66 |
),
|
67 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
response = chat_engine.stream_chat(message)
|
69 |
+
text = "".join(response.response_gen) # Gabungkan semua token menjadi string
|
70 |
+
history.append((message, text)) # Tambahkan ke riwayat
|
71 |
+
return history
|
72 |
+
|
73 |
+
def clear_history(chat_engine):
|
74 |
+
chat_engine.clear()
|
75 |
+
|
76 |
+
# Inisialisasi Komponen Gradio untuk UI
|
77 |
+
def launch_gradio(chat_engine):
|
78 |
+
with gr.Blocks() as demo:
|
79 |
+
# Mengatur tombol untuk menghapus riwayat chat
|
80 |
+
clear_btn = gr.Button("Clear")
|
81 |
+
clear_btn.click(lambda: clear_history(chat_engine))
|
82 |
+
|
83 |
+
# Membuat antarmuka chat
|
84 |
+
chat_interface = gr.ChatInterface(
|
85 |
+
lambda message, history: generate_response(message, history, chat_engine)
|
86 |
+
)
|
87 |
+
demo.launch()
|
88 |
+
|
89 |
+
# Fungsi Utama untuk Menjalankan Aplikasi
|
90 |
+
def main():
|
91 |
+
# Unduh model dan inisialisasi pengaturan
|
92 |
+
model_path = initialize_llama_model()
|
93 |
+
initialize_settings(model_path) # Mengirimkan model_path ke fungsi initialize_settings
|
94 |
+
# Inisialisasi index dan engine
|
95 |
+
index = initialize_index()
|
96 |
+
chat_engine = initialize_chat_engine(index)
|
97 |
+
# Luncurkan antarmuka
|
98 |
+
launch_gradio(chat_engine)
|
99 |
+
|
100 |
+
if __name__ == "__main__":
|
101 |
+
main()
|