Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,53 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
from llama_cpp import Llama
|
| 3 |
-
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
|
| 4 |
-
from llama_index.core.
|
| 5 |
-
from llama_index.
|
| 6 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 7 |
-
from
|
|
|
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def initialize_llama_model():
|
| 11 |
-
|
|
|
|
| 12 |
repo_id="TheBLoke/zephyr-7b-beta-GGUF", # Nama repo model
|
| 13 |
filename="zephyr-7b-beta.Q4_K_M.gguf", # Nama file model
|
| 14 |
cache_dir="./models" # Lokasi direktori untuk menyimpan model
|
| 15 |
)
|
| 16 |
return model_path
|
| 17 |
|
|
|
|
| 18 |
def initialize_settings(model_path):
|
| 19 |
Settings.llm = Llama(
|
| 20 |
model_path=model_path,
|
|
@@ -23,41 +56,75 @@ def initialize_settings(model_path):
|
|
| 23 |
top_p=0.9 # Mengurangi eksplorasi token
|
| 24 |
)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
"bahandokumen/
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
chat_engine.
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 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 |
+
env={"CMAKE_ARGS": "-DGGML_CUDA=on"},
|
| 25 |
+
check=True
|
| 26 |
+
)
|
| 27 |
+
# Periksa apakah CUDA Toolkit tersedia
|
| 28 |
+
if not shutil.which("nvcc"):
|
| 29 |
+
print("CUDA Toolkit tidak ditemukan. Pastikan sudah diinstal.")
|
| 30 |
+
return
|
| 31 |
+
|
| 32 |
+
print("Memasang ulang llama-cpp-python dengan dukungan CUDA...")
|
| 33 |
+
|
| 34 |
+
print("llama-cpp-python berhasil diinstal ulang dengan dukungan CUDA.")
|
| 35 |
+
except subprocess.CalledProcessError as e:
|
| 36 |
+
print(f"Error saat menginstal ulang llama-cpp-python: {e}")
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"Kesalahan umum: {e}")
|
| 39 |
+
|
| 40 |
+
# Fungsi untuk mengunduh model Llama
|
| 41 |
def initialize_llama_model():
|
| 42 |
+
# Unduh model jika belum ada di direktori kerja
|
| 43 |
+
model_path = hf_hub_download(
|
| 44 |
repo_id="TheBLoke/zephyr-7b-beta-GGUF", # Nama repo model
|
| 45 |
filename="zephyr-7b-beta.Q4_K_M.gguf", # Nama file model
|
| 46 |
cache_dir="./models" # Lokasi direktori untuk menyimpan model
|
| 47 |
)
|
| 48 |
return model_path
|
| 49 |
|
| 50 |
+
# Fungsi untuk mengatur konfigurasi Settings
|
| 51 |
def initialize_settings(model_path):
|
| 52 |
Settings.llm = Llama(
|
| 53 |
model_path=model_path,
|
|
|
|
| 56 |
top_p=0.9 # Mengurangi eksplorasi token
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# Fungsi untuk Menginisialisasi Index
|
| 60 |
+
def initialize_index():
|
| 61 |
+
# Tentukan dokumen input untuk pembacaan data
|
| 62 |
+
documents = SimpleDirectoryReader(input_files=["bahandokumen/K3.txt",
|
| 63 |
+
"bahandokumen/bonus.txt",
|
| 64 |
+
"bahandokumen/cuti.txt",
|
| 65 |
+
"bahandokumen/disiplinkerja.txt",
|
| 66 |
+
"bahandokumen/fasilitas&bantuan.txt",
|
| 67 |
+
"bahandokumen/upahlembur.txt",
|
| 68 |
+
"bahandokumen/waktukerja.txt"]).load_data()
|
| 69 |
+
|
| 70 |
+
parser = SentenceSplitter(chunk_size=150, chunk_overlap=10)
|
| 71 |
+
nodes = parser.get_nodes_from_documents(documents)
|
| 72 |
+
embedding = HuggingFaceEmbedding("BAAI/bge-base-en-v1.5")
|
| 73 |
+
Settings.embed_model = embedding
|
| 74 |
+
index = VectorStoreIndex(nodes)
|
| 75 |
+
return index
|
| 76 |
+
|
| 77 |
+
# Inisialisasi Mesin Chat
|
| 78 |
+
def initialize_chat_engine(index):
|
| 79 |
+
from llama_index.core.prompts import PromptTemplate
|
| 80 |
+
from llama_index.core.chat_engine.condense_plus_context import CondensePlusContextChatEngine
|
| 81 |
+
retriever = index.as_retriever(similarity_top_k=3)
|
| 82 |
+
chat_engine = CondensePlusContextChatEngine.from_defaults(
|
| 83 |
+
retriever=retriever,
|
| 84 |
+
verbose=True,
|
| 85 |
+
)
|
| 86 |
+
return chat_engine
|
| 87 |
+
|
| 88 |
+
# Fungsi untuk menghasilkan respons chatbot
|
| 89 |
+
def generate_response(message, history, chat_engine):
|
| 90 |
+
chat_messages = [
|
| 91 |
+
ChatMessage(
|
| 92 |
+
role="system",
|
| 93 |
+
content="Anda adalah chatbot yang selalu menjawab pertanyaan secara singkat, ramah, dan jelas dalam bahasa Indonesia."
|
| 94 |
+
),
|
| 95 |
+
]
|
| 96 |
+
response = chat_engine.stream_chat(message)
|
| 97 |
+
text = "".join(response.response_gen) # Gabungkan semua token menjadi string
|
| 98 |
+
history.append((message, text)) # Tambahkan ke riwayat
|
| 99 |
+
return history
|
| 100 |
+
|
| 101 |
+
def clear_history(chat_engine):
|
| 102 |
+
chat_engine.clear()
|
| 103 |
+
|
| 104 |
+
# Inisialisasi Komponen Gradio untuk UI
|
| 105 |
+
def launch_gradio(chat_engine):
|
| 106 |
+
with gr.Blocks() as demo:
|
| 107 |
+
# Mengatur tombol untuk menghapus riwayat chat
|
| 108 |
+
clear_btn = gr.Button("Clear")
|
| 109 |
+
clear_btn.click(lambda: clear_history(chat_engine))
|
| 110 |
+
|
| 111 |
+
# Membuat antarmuka chat
|
| 112 |
+
chat_interface = gr.ChatInterface(
|
| 113 |
+
lambda message, history: generate_response(message, history, chat_engine)
|
| 114 |
+
)
|
| 115 |
+
demo.launch()
|
| 116 |
+
|
| 117 |
+
# Fungsi Utama untuk Menjalankan Aplikasi
|
| 118 |
+
def main():
|
| 119 |
+
install_llama_with_cuda()
|
| 120 |
+
# Unduh model dan inisialisasi pengaturan
|
| 121 |
+
model_path = initialize_llama_model()
|
| 122 |
+
initialize_settings(model_path) # Mengirimkan model_path ke fungsi initialize_settings
|
| 123 |
+
# Inisialisasi index dan engine
|
| 124 |
+
index = initialize_index()
|
| 125 |
+
chat_engine = initialize_chat_engine(index)
|
| 126 |
+
# Luncurkan antarmuka
|
| 127 |
+
launch_gradio(chat_engine)
|
| 128 |
+
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
main()
|