BramLeo commited on
Commit
b571d20
·
verified ·
1 Parent(s): 0fbe35a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -34
app.py CHANGED
@@ -13,13 +13,37 @@ from llama_index.core.chat_engine.condense_plus_context import CondensePlusConte
13
  from llama_index.core.schema import Document
14
 
15
  # ===================================
16
- # 1️⃣ Fungsi untuk Membaca Google Spreadsheet dari Beberapa Worksheet
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # ===================================
18
  def read_google_sheets():
19
  try:
20
  scope = ["https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive"]
21
  creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
22
  client = gspread.authorize(creds)
 
23
  SPREADSHEET_ID = "1e_cNMhwF-QYpyYUpqQh-XCw-OdhWS6EuYsoBUsVtdNg"
24
  sheet_names = ["datatarget", "datacuti", "dataabsen", "datalembur"]
25
 
@@ -34,23 +58,13 @@ def read_google_sheets():
34
  all_data.append("\n")
35
  except gspread.exceptions.WorksheetNotFound:
36
  all_data.append(f"❌ ERROR: Worksheet {sheet_name} tidak ditemukan.")
37
- return "\n".join(all_data)
38
- except Exception as e:
39
- return f"❌ ERROR: {str(e)}"
40
 
41
- # ===================================
42
- # 2️⃣ Fungsi untuk Membaca File PKB JSON
43
- # ===================================
44
- def read_pkb_json():
45
- try:
46
- with open("pkb.json", "r", encoding="utf-8") as file:
47
- pkb_data = json.load(file)
48
- return json.dumps(pkb_data, indent=2, ensure_ascii=False)
49
  except Exception as e:
50
  return f"❌ ERROR: {str(e)}"
51
 
52
  # ===================================
53
- # 3️⃣ Fungsi untuk Mengunduh Model Llama
54
  # ===================================
55
  def initialize_llama_model():
56
  model_path = hf_hub_download(
@@ -61,33 +75,20 @@ def initialize_llama_model():
61
  return model_path
62
 
63
  # ===================================
64
- # 4️⃣ Inisialisasi Model dan Pengaturan
65
- # ===================================
66
- def initialize_settings(model_path):
67
- Settings.llm = LlamaCPP(
68
- model_path=model_path,
69
- temperature=0.7,
70
- )
71
-
72
- # ===================================
73
- # 5️⃣ Inisialisasi Index dari Data Spreadsheet & PKB JSON
74
  # ===================================
75
  def initialize_index():
76
- text_data = read_google_sheets()
77
- pkb_data = read_pkb_json()
78
- combined_text = text_data + "\n\n" + pkb_data
79
-
80
- document = Document(text=combined_text)
81
- documents = [document]
82
  parser = SentenceSplitter(chunk_size=150, chunk_overlap=10)
83
- nodes = parser.get_nodes_from_documents(documents)
84
  embedding = HuggingFaceEmbedding("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
85
  Settings.embed_model = embedding
86
  index = VectorStoreIndex(nodes)
87
  return index
88
 
89
  # ===================================
90
- # 6️⃣ Inisialisasi Mesin Chatbot
91
  # ===================================
92
  def initialize_chat_engine(index):
93
  retriever = index.as_retriever(similarity_top_k=3)
@@ -98,22 +99,43 @@ def initialize_chat_engine(index):
98
  return chat_engine
99
 
100
  # ===================================
101
- # 7️⃣ Fungsi untuk Menghasilkan Respons Chatbot
102
  # ===================================
103
  def generate_response(message, history, chat_engine):
104
  if history is None:
105
  history = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  response = chat_engine.stream_chat(message)
107
  text = "".join(response.response_gen)
 
108
  history.append((message, text))
109
  return history
110
 
111
  # ===================================
112
- # 8️⃣ Fungsi Utama untuk Menjalankan Aplikasi
113
  # ===================================
114
  def main():
115
  model_path = initialize_llama_model()
116
- initialize_settings(model_path)
 
 
 
 
117
  index = initialize_index()
118
  chat_engine = initialize_chat_engine(index)
119
 
 
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:
43
  scope = ["https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive"]
44
  creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
45
  client = gspread.authorize(creds)
46
+
47
  SPREADSHEET_ID = "1e_cNMhwF-QYpyYUpqQh-XCw-OdhWS6EuYsoBUsVtdNg"
48
  sheet_names = ["datatarget", "datacuti", "dataabsen", "datalembur"]
49
 
 
58
  all_data.append("\n")
59
  except gspread.exceptions.WorksheetNotFound:
60
  all_data.append(f"❌ ERROR: Worksheet {sheet_name} tidak ditemukan.")
 
 
 
61
 
62
+ return "\n".join(all_data)
 
 
 
 
 
 
 
63
  except Exception as e:
64
  return f"❌ ERROR: {str(e)}"
65
 
66
  # ===================================
67
+ # 3️⃣ Inisialisasi Model Llama
68
  # ===================================
69
  def initialize_llama_model():
70
  model_path = hf_hub_download(
 
75
  return model_path
76
 
77
  # ===================================
78
+ # 4️⃣ Inisialisasi Index Data PKB & Spreadsheet
 
 
 
 
 
 
 
 
 
79
  # ===================================
80
  def initialize_index():
81
+ text_data = read_google_sheets() + "\n" + read_pkb_json()
82
+ document = Document(text=text_data)
 
 
 
 
83
  parser = SentenceSplitter(chunk_size=150, chunk_overlap=10)
84
+ nodes = parser.get_nodes_from_documents([document])
85
  embedding = HuggingFaceEmbedding("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
86
  Settings.embed_model = embedding
87
  index = VectorStoreIndex(nodes)
88
  return index
89
 
90
  # ===================================
91
+ # 5️⃣ Inisialisasi Chatbot
92
  # ===================================
93
  def initialize_chat_engine(index):
94
  retriever = index.as_retriever(similarity_top_k=3)
 
99
  return chat_engine
100
 
101
  # ===================================
102
+ # 6️⃣ Fungsi untuk Respons Chatbot
103
  # ===================================
104
  def generate_response(message, history, chat_engine):
105
  if history is None:
106
  history = []
107
+
108
+ chat_messages = [
109
+ ChatMessage(
110
+ role="system",
111
+ content=(
112
+ "Anda adalah chatbot yang dirancang khusus untuk berbicara dalam Bahasa Indonesia. "
113
+ "Anda adalah chatbot HRD yang membantu karyawan dalam memahami administrasi dan data perusahaan. "
114
+ "Anda tidak diperbolehkan menjawab dalam bahasa lain, termasuk Inggris. "
115
+ "Gunakan gaya bahasa profesional tetapi tetap ramah. "
116
+ "Jika informasi tidak tersedia dalam dokumen, katakan dengan sopan bahwa Anda tidak tahu. "
117
+ "Pastikan setiap jawaban diberikan secara ringkas, jelas, dan sesuai konteks."
118
+ "Jawaban harus singkat, jelas, dan dalam Bahasa Indonesia."
119
+ ),
120
+ ),
121
+ ]
122
+
123
  response = chat_engine.stream_chat(message)
124
  text = "".join(response.response_gen)
125
+
126
  history.append((message, text))
127
  return history
128
 
129
  # ===================================
130
+ # 7️⃣ Fungsi Utama untuk Menjalankan Aplikasi
131
  # ===================================
132
  def main():
133
  model_path = initialize_llama_model()
134
+ Settings.llm = LlamaCPP(
135
+ model_path=model_path,
136
+ temperature=0.7,
137
+ )
138
+
139
  index = initialize_index()
140
  chat_engine = initialize_chat_engine(index)
141