Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
import gspread
|
4 |
from oauth2client.service_account import ServiceAccountCredentials
|
@@ -13,7 +13,30 @@ from llama_index.core.chat_engine.condense_plus_context import CondensePlusConte
|
|
13 |
from llama_index.core.schema import Document
|
14 |
|
15 |
# ===================================
|
16 |
-
# 1️⃣ Fungsi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# ===================================
|
18 |
def read_google_sheets():
|
19 |
try:
|
@@ -82,7 +105,7 @@ def initialize_settings(model_path):
|
|
82 |
# 4️⃣ Inisialisasi Index dari Data Spreadsheet
|
83 |
# ===================================
|
84 |
def initialize_index():
|
85 |
-
text_data = read_google_sheets()
|
86 |
document = Document(text=text_data)
|
87 |
documents = [document]
|
88 |
|
@@ -113,7 +136,9 @@ def generate_response(message, history, chat_engine):
|
|
113 |
if history is None:
|
114 |
history = []
|
115 |
|
116 |
-
|
|
|
|
|
117 |
document = Document(text=text_data)
|
118 |
documents = [document]
|
119 |
|
@@ -128,17 +153,17 @@ def generate_response(message, history, chat_engine):
|
|
128 |
)
|
129 |
|
130 |
chat_messages = [
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
139 |
),
|
140 |
-
|
141 |
-
]
|
142 |
|
143 |
response = chat_engine.stream_chat(message)
|
144 |
text = "".join(response.response_gen)
|
|
|
1 |
+
import json
|
2 |
import gradio as gr
|
3 |
import gspread
|
4 |
from oauth2client.service_account import ServiceAccountCredentials
|
|
|
13 |
from llama_index.core.schema import Document
|
14 |
|
15 |
# ===================================
|
16 |
+
# 1️⃣ Fungsi Membaca Data PKB.json
|
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
|
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:
|
|
|
105 |
# 4️⃣ Inisialisasi Index dari Data Spreadsheet
|
106 |
# ===================================
|
107 |
def initialize_index():
|
108 |
+
text_data = read_google_sheets() + "\n" + read_pkb_json()
|
109 |
document = Document(text=text_data)
|
110 |
documents = [document]
|
111 |
|
|
|
136 |
if history is None:
|
137 |
history = []
|
138 |
|
139 |
+
# 🔹 Baca ulang data dari Google Sheets & PKB
|
140 |
+
text_data = read_google_sheets() + "\n" + read_pkb_json()
|
141 |
+
|
142 |
document = Document(text=text_data)
|
143 |
documents = [document]
|
144 |
|
|
|
153 |
)
|
154 |
|
155 |
chat_messages = [
|
156 |
+
ChatMessage(
|
157 |
+
role="system",
|
158 |
+
content=(
|
159 |
+
"Anda adalah chatbot yang dirancang khusus untuk berbicara dalam Bahasa Indonesia. "
|
160 |
+
"Anda tidak diperbolehkan menjawab dalam bahasa lain, termasuk Inggris. "
|
161 |
+
"Gunakan gaya bahasa profesional tetapi tetap ramah. "
|
162 |
+
"Jika informasi tidak tersedia dalam dokumen, katakan dengan sopan bahwa Anda tidak tahu. "
|
163 |
+
"Pastikan setiap jawaban diberikan secara ringkas, jelas, dan sesuai konteks."
|
164 |
+
),
|
165 |
),
|
166 |
+
]
|
|
|
167 |
|
168 |
response = chat_engine.stream_chat(message)
|
169 |
text = "".join(response.response_gen)
|