Update app.py
Browse files
app.py
CHANGED
@@ -1,130 +1,141 @@
|
|
1 |
-
# Import Library yang Diperlukan
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import subprocess
|
6 |
from llama_cpp import Llama
|
7 |
-
from llama_index.core import
|
8 |
-
from llama_index.core.
|
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.
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
|
|
|
|
16 |
try:
|
17 |
-
|
18 |
-
|
19 |
-
|
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 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
except Exception as e:
|
38 |
-
|
39 |
|
40 |
-
#
|
|
|
|
|
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",
|
45 |
-
filename="zephyr-7b-beta.Q4_K_M.gguf",
|
46 |
-
cache_dir="./models"
|
47 |
)
|
48 |
return model_path
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
temperature=0.7, # Sesuaikan untuk respons yang lebih cepat
|
56 |
-
top_p=0.9 # Mengurangi eksplorasi token
|
57 |
-
)
|
58 |
|
59 |
-
#
|
|
|
|
|
60 |
def initialize_index():
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
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=
|
85 |
)
|
86 |
return chat_engine
|
87 |
|
88 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
def generate_response(message, history, chat_engine):
|
|
|
|
|
|
|
90 |
chat_messages = [
|
91 |
ChatMessage(
|
92 |
role="system",
|
93 |
-
content=
|
|
|
|
|
|
|
|
|
|
|
94 |
),
|
95 |
]
|
|
|
96 |
response = chat_engine.stream_chat(message)
|
97 |
-
|
98 |
-
history.append((message, text)) # Tambahkan ke riwayat
|
99 |
-
return history
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
#
|
105 |
-
|
106 |
-
|
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)
|
123 |
-
|
124 |
index = initialize_index()
|
125 |
chat_engine = initialize_chat_engine(index)
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
if __name__ == "__main__":
|
130 |
main()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import gspread
|
3 |
+
from oauth2client.service_account import ServiceAccountCredentials
|
|
|
4 |
from llama_cpp import Llama
|
5 |
+
from llama_index.core import VectorStoreIndex, Settings
|
6 |
+
from llama_index.core.node_parser import SentenceSplitter
|
|
|
7 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
8 |
+
from llama_index.llms.llama_cpp import LlamaCPP
|
9 |
from huggingface_hub import hf_hub_download
|
10 |
+
from llama_index.core.llms import ChatMessage
|
11 |
+
from llama_index.core.chat_engine.condense_plus_context import CondensePlusContextChatEngine
|
12 |
+
from llama_index.core.schema import Document
|
13 |
|
14 |
+
# ===================================
|
15 |
+
# 1️⃣ Fungsi Membaca Data Google Spreadsheet
|
16 |
+
# ===================================
|
17 |
+
def read_google_sheets():
|
18 |
try:
|
19 |
+
scope = ["https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive"]
|
20 |
+
creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
|
21 |
+
client = gspread.authorize(creds)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
SPREADSHEET_ID = "1e_cNMhwF-QYpyYUpqQh-XCw-OdhWS6EuYsoBUsVtdNg"
|
24 |
+
sheet_names = ["datatarget", "datacuti", "dataabsen", "datalembur"]
|
25 |
+
|
26 |
+
all_data = []
|
27 |
+
spreadsheet = client.open_by_key(SPREADSHEET_ID)
|
28 |
+
|
29 |
+
for sheet_name in sheet_names:
|
30 |
+
try:
|
31 |
+
sheet = spreadsheet.worksheet(sheet_name)
|
32 |
+
data = sheet.get_all_values()
|
33 |
+
all_data.append(f"=== Data dari {sheet_name.upper()} ===")
|
34 |
+
all_data.extend([" | ".join(row) for row in data])
|
35 |
+
all_data.append("\n")
|
36 |
+
except gspread.exceptions.WorksheetNotFound:
|
37 |
+
all_data.append(f"❌ ERROR: Worksheet {sheet_name} tidak ditemukan.")
|
38 |
+
|
39 |
+
return "\n".join(all_data).strip()
|
40 |
+
|
41 |
+
except gspread.exceptions.SpreadsheetNotFound:
|
42 |
+
return "❌ ERROR: Spreadsheet tidak ditemukan!"
|
43 |
+
|
44 |
except Exception as e:
|
45 |
+
return f"❌ ERROR: {str(e)}"
|
46 |
|
47 |
+
# ===================================
|
48 |
+
# 2️⃣ Inisialisasi Model Llama
|
49 |
+
# ===================================
|
50 |
def initialize_llama_model():
|
|
|
51 |
model_path = hf_hub_download(
|
52 |
+
repo_id="TheBLoke/zephyr-7b-beta-GGUF",
|
53 |
+
filename="zephyr-7b-beta.Q4_K_M.gguf",
|
54 |
+
cache_dir="./models"
|
55 |
)
|
56 |
return model_path
|
57 |
|
58 |
+
# ===================================
|
59 |
+
# 3️⃣ Inisialisasi Pengaturan Model
|
60 |
+
# ===================================
|
61 |
+
def initialize_settings(model_path):
|
62 |
+
Settings.llm = LlamaCPP(model_path=model_path, temperature=0.7)
|
|
|
|
|
|
|
63 |
|
64 |
+
# ===================================
|
65 |
+
# 4️⃣ Inisialisasi Index & Chat Engine
|
66 |
+
# ===================================
|
67 |
def initialize_index():
|
68 |
+
text_data = read_google_sheets()
|
69 |
+
document = Document(text=text_data)
|
70 |
+
parser = SentenceSplitter(chunk_size=100, chunk_overlap=30)
|
71 |
+
nodes = parser.get_nodes_from_documents([document])
|
72 |
+
|
73 |
+
embedding = HuggingFaceEmbedding("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
Settings.embed_model = embedding
|
75 |
+
|
76 |
index = VectorStoreIndex(nodes)
|
77 |
return index
|
78 |
|
|
|
79 |
def initialize_chat_engine(index):
|
|
|
|
|
80 |
retriever = index.as_retriever(similarity_top_k=3)
|
81 |
chat_engine = CondensePlusContextChatEngine.from_defaults(
|
82 |
retriever=retriever,
|
83 |
+
verbose=False # ❌ Hapus verbose agar tidak ada referensi dokumen
|
84 |
)
|
85 |
return chat_engine
|
86 |
|
87 |
+
# ===================================
|
88 |
+
# 5️⃣ Fungsi untuk Merapikan Jawaban Chatbot
|
89 |
+
# ===================================
|
90 |
+
def clean_response(response):
|
91 |
+
text = "".join(response.response_gen) # Gabungkan teks yang dihasilkan
|
92 |
+
text = text.replace("\n\n", "\n").strip() # Hilangkan newline berlebihan
|
93 |
+
text = text.replace("user:", "").replace("jawaban:", "").replace("assistant:", "").strip()
|
94 |
+
return text
|
95 |
+
|
96 |
+
# ===================================
|
97 |
+
# 6️⃣ Fungsi untuk Menghasilkan Respons Chatbot
|
98 |
+
# ===================================
|
99 |
def generate_response(message, history, chat_engine):
|
100 |
+
if history is None:
|
101 |
+
history = []
|
102 |
+
|
103 |
chat_messages = [
|
104 |
ChatMessage(
|
105 |
role="system",
|
106 |
+
content=(
|
107 |
+
"Anda adalah chatbot HRD yang membantu karyawan memahami administrasi perusahaan. "
|
108 |
+
"Gunakan Bahasa Indonesia dengan gaya profesional dan ramah. "
|
109 |
+
"Jika informasi tidak tersedia dalam dokumen, katakan dengan sopan bahwa Anda tidak tahu. "
|
110 |
+
"Jawaban harus singkat, jelas, dan sesuai konteks."
|
111 |
+
),
|
112 |
),
|
113 |
]
|
114 |
+
|
115 |
response = chat_engine.stream_chat(message)
|
116 |
+
cleaned_text = clean_response(response) # 🔹 Gunakan fungsi clean_response()
|
|
|
|
|
117 |
|
118 |
+
history.append((message, cleaned_text)) # 🔹 Pastikan hanya teks yang masuk ke history
|
119 |
+
return cleaned_text
|
120 |
+
|
121 |
+
# ===================================
|
122 |
+
# 7️⃣ Fungsi Utama untuk Menjalankan Aplikasi
|
123 |
+
# ===================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
def main():
|
|
|
|
|
125 |
model_path = initialize_llama_model()
|
126 |
+
initialize_settings(model_path)
|
127 |
+
|
128 |
index = initialize_index()
|
129 |
chat_engine = initialize_chat_engine(index)
|
130 |
+
|
131 |
+
def chatbot_response(message, history):
|
132 |
+
return generate_response(message, history, chat_engine)
|
133 |
+
|
134 |
+
gr.Interface(
|
135 |
+
fn=chatbot_response,
|
136 |
+
inputs=["text"],
|
137 |
+
outputs=["text"],
|
138 |
+
).launch()
|
139 |
+
|
140 |
if __name__ == "__main__":
|
141 |
main()
|