Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,20 @@
|
|
1 |
-
|
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.
|
6 |
-
from llama_index.
|
7 |
-
from huggingface_hub import hf_hub_download
|
8 |
from llama_index.core.node_parser import SentenceSplitter
|
9 |
-
|
10 |
-
import
|
11 |
-
import
|
12 |
-
import subprocess
|
13 |
-
|
14 |
-
# Fungsi untuk memasang ulang llama-cpp-python dengan dukungan CUDA
|
15 |
-
def install_llama_with_cuda():
|
16 |
-
try:
|
17 |
-
# Baca file requirements.txt
|
18 |
-
with open("requirements.txt", "r") as f:
|
19 |
-
packages = f.read().splitlines()
|
20 |
-
|
21 |
-
# Install setiap paket dengan CMAKE_ARGS untuk dukungan CUDA
|
22 |
-
for package in packages:
|
23 |
-
subprocess.run(
|
24 |
-
[
|
25 |
-
package, "--force-reinstall", "--no-cache-dir"
|
26 |
-
],
|
27 |
-
env={"CMAKE_ARGS": "-DGGML_CUDA=on"},
|
28 |
-
check=True
|
29 |
-
)
|
30 |
-
# Periksa apakah CUDA Toolkit tersedia
|
31 |
-
if not shutil.which("nvcc"):
|
32 |
-
print("CUDA Toolkit tidak ditemukan. Pastikan sudah diinstal.")
|
33 |
-
return
|
34 |
-
|
35 |
-
print("Memasang ulang llama-cpp-python dengan dukungan CUDA...")
|
36 |
-
|
37 |
-
print("llama-cpp-python berhasil diinstal ulang dengan dukungan CUDA.")
|
38 |
-
except subprocess.CalledProcessError as e:
|
39 |
-
print(f"Error saat menginstal ulang llama-cpp-python: {e}")
|
40 |
-
except Exception as e:
|
41 |
-
print(f"Kesalahan umum: {e}")
|
42 |
-
|
43 |
-
# Fungsi untuk mengunduh model Llama
|
44 |
-
def initialize_llama_model():
|
45 |
-
# Unduh model jika belum ada di direktori kerja
|
46 |
-
model_path = hf_hub_download(
|
47 |
-
repo_id="TheBLoke/zephyr-7b-beta-GGUF", # Nama repo model
|
48 |
-
filename="zephyr-7b-beta.Q4_K_M.gguf", # Nama file model
|
49 |
-
cache_dir="./models" # Lokasi direktori untuk menyimpan model
|
50 |
-
)
|
51 |
-
return model_path
|
52 |
|
53 |
-
#
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
"temperature": 0.7, # Sesuaikan untuk respons yang lebih cepat
|
59 |
-
"top_p": 0.9, # Mengurangi eksplorasi token
|
60 |
-
}
|
61 |
-
)
|
62 |
|
63 |
-
#
|
64 |
-
|
65 |
-
# Tentukan dokumen input untuk pembacaan data
|
66 |
-
documents = SimpleDirectoryReader(input_files=["bahandokumen/K3.txt",
|
67 |
-
"bahandokumen/bonus.txt",
|
68 |
"bahandokumen/cuti.txt",
|
69 |
"bahandokumen/disiplinkerja.txt",
|
70 |
"bahandokumen/fasilitas&bantuan.txt",
|
@@ -105,70 +55,62 @@ def initialize_index():
|
|
105 |
"bahandokumen/upahlembur.txt",
|
106 |
"bahandokumen/waktukerja.txt"]).load_data()
|
107 |
|
108 |
-
|
109 |
-
|
|
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
114 |
|
115 |
-
|
116 |
|
117 |
-
#
|
118 |
-
|
119 |
-
|
120 |
-
from llama_index.core.chat_engine.condense_plus_context import CondensePlusContextChatEngine
|
121 |
-
retriever = index.as_retriever(similarity_top_k=3)
|
122 |
-
chat_engine = CondensePlusContextChatEngine.from_defaults(
|
123 |
-
retriever=retriever,
|
124 |
-
verbose=True,
|
125 |
-
)
|
126 |
-
return chat_engine
|
127 |
|
128 |
-
#
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
chat_messages = [
|
131 |
ChatMessage(
|
132 |
role="system",
|
133 |
-
content="Anda adalah chatbot yang selalu menjawab pertanyaan
|
134 |
-
)
|
135 |
]
|
136 |
-
response = chat_engine.stream_chat(message)
|
137 |
-
text = "".join(response.response_gen) # Gabungkan semua token menjadi string
|
138 |
-
history.append((message, text)) # Tambahkan ke riwayat
|
139 |
-
return history
|
140 |
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
# Inisialisasi Komponen Gradio untuk UI
|
145 |
-
def launch_gradio(chat_engine):
|
146 |
-
with gr.Blocks() as demo:
|
147 |
-
# Mengatur tombol untuk menghapus riwayat chat
|
148 |
-
clear_btn = gr.Button("Clear")
|
149 |
-
clear_btn.click(lambda: clear_history(chat_engine))
|
150 |
-
|
151 |
-
# Membuat antarmuka chat
|
152 |
-
chat_interface = gr.ChatInterface(
|
153 |
-
lambda message, history: generate_response(message, history, chat_engine)
|
154 |
-
)
|
155 |
|
156 |
-
|
157 |
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
initialize_settings(model_path) # Mengirimkan model_path ke fungsi initialize_settings
|
165 |
|
166 |
-
|
167 |
-
|
168 |
-
|
|
|
|
|
169 |
|
170 |
-
|
171 |
-
|
|
|
|
|
|
|
172 |
|
173 |
-
|
174 |
-
main()
|
|
|
1 |
+
import gradio as gr
|
|
|
|
|
2 |
from llama_index.core.llms import ChatMessage
|
3 |
+
from llama_index.core.chat_engine.condense_plus_context import CondensePlusContextChatEngine
|
4 |
+
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
|
|
|
5 |
from llama_index.core.node_parser import SentenceSplitter
|
6 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
7 |
+
from llama_index.core import Settings
|
8 |
+
from llama_cpp import Llama
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Load Llama model
|
11 |
+
llm = Llama(
|
12 |
+
model_path="zephyr-7b-beta.Q4_K_M.gguf", # Sesuaikan path model
|
13 |
+
n_gpu_layers=1, # Aktifkan GPU jika tersedia
|
14 |
+
)
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Konfigurasi untuk indexing dokumen
|
17 |
+
documents = SimpleDirectoryReader(input_files=[ "bahandokumen/bonus.txt",
|
|
|
|
|
|
|
18 |
"bahandokumen/cuti.txt",
|
19 |
"bahandokumen/disiplinkerja.txt",
|
20 |
"bahandokumen/fasilitas&bantuan.txt",
|
|
|
55 |
"bahandokumen/upahlembur.txt",
|
56 |
"bahandokumen/waktukerja.txt"]).load_data()
|
57 |
|
58 |
+
# Split dokumen menjadi node
|
59 |
+
parser = SentenceSplitter(chunk_size=300, chunk_overlap=20)
|
60 |
+
nodes = parser.get_nodes_from_documents(documents)
|
61 |
|
62 |
+
# Set embeddings dan index
|
63 |
+
embedding = HuggingFaceEmbedding("BAAI/bge-base-en-v1.5")
|
64 |
+
Settings.llm = llm
|
65 |
+
Settings.embed_model = embedding
|
66 |
|
67 |
+
index = VectorStoreIndex(nodes)
|
68 |
|
69 |
+
# Retrieve data berdasarkan query
|
70 |
+
retriever = index.as_retriever(similarity_top_k=3)
|
71 |
+
query_engine = index.as_query_engine(similarity_top_k=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
# Setup chat engine
|
74 |
+
chat_engine = CondensePlusContextChatEngine.from_defaults(
|
75 |
+
retriever=retriever,
|
76 |
+
verbose=True,
|
77 |
+
)
|
78 |
+
|
79 |
+
# Fungsi untuk merespons chat
|
80 |
+
def generate_response(message, history):
|
81 |
chat_messages = [
|
82 |
ChatMessage(
|
83 |
role="system",
|
84 |
+
content="Anda adalah chatbot yang selalu menjawab pertanyaan dalam bahasa Indonesia dengan singkat dan ramah."
|
85 |
+
)
|
86 |
]
|
|
|
|
|
|
|
|
|
87 |
|
88 |
+
for human, ai in history:
|
89 |
+
chat_messages.append(ChatMessage(role="user", content=human))
|
90 |
+
chat_messages.append(ChatMessage(role="assistant", content=ai))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
+
chat_messages.append(ChatMessage(role="user", content=message))
|
93 |
|
94 |
+
response = chat_engine.stream_chat(chat_messages)
|
95 |
+
text = ""
|
96 |
+
for token in response.response_gen:
|
97 |
+
text += token
|
98 |
+
yield text
|
99 |
|
100 |
+
# Fungsi untuk mereset riwayat chat
|
101 |
+
def clear_history():
|
102 |
+
chat_engine.reset()
|
|
|
103 |
|
104 |
+
# Gradio UI
|
105 |
+
with gr.Blocks() as demo:
|
106 |
+
with gr.Row():
|
107 |
+
clear_btn = gr.Button("Clear")
|
108 |
+
clear_btn.click(clear_history)
|
109 |
|
110 |
+
chat_interface = gr.ChatInterface(
|
111 |
+
fn=generate_response,
|
112 |
+
clear_btn=clear_btn,
|
113 |
+
chatbot_style="default"
|
114 |
+
)
|
115 |
|
116 |
+
demo.launch()
|
|