Update app.py
Browse files
app.py
CHANGED
@@ -13,30 +13,7 @@ from llama_index.core.chat_engine.condense_plus_context import CondensePlusConte
|
|
13 |
from llama_index.core.schema import Document
|
14 |
|
15 |
# ===================================
|
16 |
-
# 1️⃣ Fungsi Membaca Data
|
17 |
-
# ===================================
|
18 |
-
def read_pkb_json():
|
19 |
-
try:
|
20 |
-
with open("pkb.json", "r", encoding="utf-8") as file:
|
21 |
-
data = json.load(file)
|
22 |
-
|
23 |
-
pkb_text = "=== Perjanjian Kerja Bersama ===\n"
|
24 |
-
for bab, content in data["perjanjian_kerja_bersama"].items():
|
25 |
-
pkb_text += f"\n## {content['judul']} ##\n"
|
26 |
-
for pasal, pasal_data in content.items():
|
27 |
-
if pasal != "judul":
|
28 |
-
pkb_text += f"\n### {pasal_data['judul']} ###\n"
|
29 |
-
for item in pasal_data["isi"]:
|
30 |
-
if isinstance(item, dict):
|
31 |
-
pkb_text += f"- {item['istilah']}: {item['definisi']}\n"
|
32 |
-
else:
|
33 |
-
pkb_text += f"- {item}\n"
|
34 |
-
return pkb_text.strip()
|
35 |
-
except Exception as e:
|
36 |
-
return f"❌ ERROR membaca PKB.json: {str(e)}"
|
37 |
-
|
38 |
-
# ===================================
|
39 |
-
# 2️⃣ Fungsi Membaca Data Google Spreadsheet
|
40 |
# ===================================
|
41 |
def read_google_sheets():
|
42 |
try:
|
@@ -45,7 +22,7 @@ def read_google_sheets():
|
|
45 |
client = gspread.authorize(creds)
|
46 |
|
47 |
SPREADSHEET_ID = "1e_cNMhwF-QYpyYUpqQh-XCw-OdhWS6EuYsoBUsVtdNg"
|
48 |
-
sheet_names = ["datatarget", "datacuti", "dataabsen", "datalembur"]
|
49 |
|
50 |
all_data = []
|
51 |
spreadsheet = client.open_by_key(SPREADSHEET_ID)
|
@@ -69,7 +46,7 @@ def read_google_sheets():
|
|
69 |
return f"❌ ERROR: {str(e)}"
|
70 |
|
71 |
# ===================================
|
72 |
-
#
|
73 |
# ===================================
|
74 |
def initialize_llama_model():
|
75 |
model_path = hf_hub_download(
|
@@ -80,18 +57,18 @@ def initialize_llama_model():
|
|
80 |
return model_path
|
81 |
|
82 |
# ===================================
|
83 |
-
#
|
84 |
# ===================================
|
85 |
def initialize_settings(model_path):
|
86 |
Settings.llm = LlamaCPP(model_path=model_path, temperature=0.7)
|
87 |
|
88 |
# ===================================
|
89 |
-
#
|
90 |
# ===================================
|
91 |
def initialize_index():
|
92 |
-
text_data = read_google_sheets()
|
93 |
document = Document(text=text_data)
|
94 |
-
parser = SentenceSplitter(chunk_size=
|
95 |
nodes = parser.get_nodes_from_documents([document])
|
96 |
|
97 |
embedding = HuggingFaceEmbedding("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
@@ -109,7 +86,7 @@ def initialize_chat_engine(index):
|
|
109 |
return chat_engine
|
110 |
|
111 |
# ===================================
|
112 |
-
#
|
113 |
# ===================================
|
114 |
def clean_response(response):
|
115 |
text = "".join(response.response_gen) # Gabungkan teks yang dihasilkan
|
@@ -118,7 +95,7 @@ def clean_response(response):
|
|
118 |
return text
|
119 |
|
120 |
# ===================================
|
121 |
-
#
|
122 |
# ===================================
|
123 |
def generate_response(message, history, chat_engine):
|
124 |
if history is None:
|
@@ -143,7 +120,7 @@ def generate_response(message, history, chat_engine):
|
|
143 |
return cleaned_text
|
144 |
|
145 |
# ===================================
|
146 |
-
#
|
147 |
# ===================================
|
148 |
def main():
|
149 |
model_path = initialize_llama_model()
|
|
|
13 |
from llama_index.core.schema import Document
|
14 |
|
15 |
# ===================================
|
16 |
+
# 1️⃣ Fungsi Membaca Data Google Spreadsheet
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# ===================================
|
18 |
def read_google_sheets():
|
19 |
try:
|
|
|
22 |
client = gspread.authorize(creds)
|
23 |
|
24 |
SPREADSHEET_ID = "1e_cNMhwF-QYpyYUpqQh-XCw-OdhWS6EuYsoBUsVtdNg"
|
25 |
+
sheet_names = ["datatarget", "datacuti", "dataabsen", "datalembur", "pkb"]
|
26 |
|
27 |
all_data = []
|
28 |
spreadsheet = client.open_by_key(SPREADSHEET_ID)
|
|
|
46 |
return f"❌ ERROR: {str(e)}"
|
47 |
|
48 |
# ===================================
|
49 |
+
# 2️⃣ Inisialisasi Model Llama
|
50 |
# ===================================
|
51 |
def initialize_llama_model():
|
52 |
model_path = hf_hub_download(
|
|
|
57 |
return model_path
|
58 |
|
59 |
# ===================================
|
60 |
+
# 3️⃣ Inisialisasi Pengaturan Model
|
61 |
# ===================================
|
62 |
def initialize_settings(model_path):
|
63 |
Settings.llm = LlamaCPP(model_path=model_path, temperature=0.7)
|
64 |
|
65 |
# ===================================
|
66 |
+
# 4️⃣ Inisialisasi Index & Chat Engine
|
67 |
# ===================================
|
68 |
def initialize_index():
|
69 |
+
text_data = read_google_sheets()
|
70 |
document = Document(text=text_data)
|
71 |
+
parser = SentenceSplitter(chunk_size=100, chunk_overlap=30)
|
72 |
nodes = parser.get_nodes_from_documents([document])
|
73 |
|
74 |
embedding = HuggingFaceEmbedding("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
|
|
86 |
return chat_engine
|
87 |
|
88 |
# ===================================
|
89 |
+
# 5️⃣ Fungsi untuk Merapikan Jawaban Chatbot
|
90 |
# ===================================
|
91 |
def clean_response(response):
|
92 |
text = "".join(response.response_gen) # Gabungkan teks yang dihasilkan
|
|
|
95 |
return text
|
96 |
|
97 |
# ===================================
|
98 |
+
# 6️⃣ Fungsi untuk Menghasilkan Respons Chatbot
|
99 |
# ===================================
|
100 |
def generate_response(message, history, chat_engine):
|
101 |
if history is None:
|
|
|
120 |
return cleaned_text
|
121 |
|
122 |
# ===================================
|
123 |
+
# 7️⃣ Fungsi Utama untuk Menjalankan Aplikasi
|
124 |
# ===================================
|
125 |
def main():
|
126 |
model_path = initialize_llama_model()
|