BramLeo commited on
Commit
d6ad8ad
·
verified ·
1 Parent(s): b18a293

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -68
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # Import Library yang Diperlukan
2
  import gradio as gr
3
  import gspread
4
  from oauth2client.service_account import ServiceAccountCredentials
@@ -17,49 +17,40 @@ from llama_index.core.schema import Document
17
  # ===================================
18
  def read_google_sheets():
19
  try:
20
- # Tentukan scope akses ke Google Sheets & Drive
21
  scope = ["https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive"]
22
-
23
- # Load kredensial dari file credentials.json
24
  creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
25
  client = gspread.authorize(creds)
26
-
27
- # ID Spreadsheet (tetap sama untuk semua sheet)
28
  SPREADSHEET_ID = "1e_cNMhwF-QYpyYUpqQh-XCw-OdhWS6EuYsoBUsVtdNg"
29
-
30
- # 📌 Daftar nama worksheet yang akan dibaca
31
  sheet_names = ["datatarget", "datacuti", "dataabsen", "datalembur"]
32
 
33
- all_data = [] # 🔹 List untuk menyimpan semua data
34
-
35
- # 📌 Loop untuk membaca setiap worksheet
36
  spreadsheet = client.open_by_key(SPREADSHEET_ID)
37
  for sheet_name in sheet_names:
38
  try:
39
  sheet = spreadsheet.worksheet(sheet_name)
40
  data = sheet.get_all_values()
41
-
42
- # Tambahkan header nama sheet sebelum data untuk membedakan
43
  all_data.append(f"=== Data dari {sheet_name.upper()} ===")
44
  all_data.extend([" | ".join(row) for row in data])
45
- all_data.append("\n") # Pisahkan tiap sheet dengan newline
46
-
47
  except gspread.exceptions.WorksheetNotFound:
48
  all_data.append(f"❌ ERROR: Worksheet {sheet_name} tidak ditemukan.")
 
 
 
49
 
50
- # Gabungkan semua data menjadi satu string panjang
51
- formatted_text = "\n".join(all_data)
52
-
53
- return formatted_text
54
-
55
- except gspread.exceptions.SpreadsheetNotFound:
56
- return "❌ ERROR: Spreadsheet tidak ditemukan. Pastikan ID/nama benar!"
57
-
58
  except Exception as e:
59
  return f"❌ ERROR: {str(e)}"
60
 
61
  # ===================================
62
- # 2️⃣ Fungsi untuk Mengunduh Model Llama
63
  # ===================================
64
  def initialize_llama_model():
65
  model_path = hf_hub_download(
@@ -70,7 +61,7 @@ def initialize_llama_model():
70
  return model_path
71
 
72
  # ===================================
73
- # 3️⃣ Inisialisasi Model dan Pengaturan
74
  # ===================================
75
  def initialize_settings(model_path):
76
  Settings.llm = LlamaCPP(
@@ -79,24 +70,24 @@ def initialize_settings(model_path):
79
  )
80
 
81
  # ===================================
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
-
89
  parser = SentenceSplitter(chunk_size=150, chunk_overlap=10)
90
  nodes = parser.get_nodes_from_documents(documents)
91
-
92
  embedding = HuggingFaceEmbedding("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
93
  Settings.embed_model = embedding
94
-
95
  index = VectorStoreIndex(nodes)
96
  return index
97
 
98
  # ===================================
99
- # 5️⃣ Inisialisasi Mesin Chatbot
100
  # ===================================
101
  def initialize_chat_engine(index):
102
  retriever = index.as_retriever(similarity_top_k=3)
@@ -107,58 +98,28 @@ def initialize_chat_engine(index):
107
  return chat_engine
108
 
109
  # ===================================
110
- # 6️⃣ Fungsi untuk Menghasilkan Respons Chatbot
111
  # ===================================
112
  def generate_response(message, history, chat_engine):
113
  if history is None:
114
  history = []
115
-
116
- text_data = read_google_sheets()
117
- document = Document(text=text_data)
118
- documents = [document]
119
-
120
- parser = SentenceSplitter(chunk_size=150, chunk_overlap=10)
121
- nodes = parser.get_nodes_from_documents(documents)
122
- index = VectorStoreIndex(nodes)
123
- retriever = index.as_retriever(similarity_top_k=3)
124
-
125
- chat_engine = CondensePlusContextChatEngine.from_defaults(
126
- retriever=retriever,
127
- verbose=True,
128
- )
129
-
130
- chat_messages = [
131
- ChatMessage(
132
- role="system",
133
- content=(
134
- "Anda adalah chatbot yang dirancang khusus untuk berbicara dalam Bahasa Indonesia. "
135
- "Anda tidak diperbolehkan menjawab dalam bahasa lain, termasuk Inggris. "
136
- "Gunakan gaya bahasa profesional tetapi tetap ramah. "
137
- "Jika informasi tidak tersedia dalam dokumen, katakan dengan sopan bahwa Anda tidak tahu. "
138
- "Pastikan setiap jawaban diberikan secara ringkas, jelas, dan sesuai konteks."
139
- ),
140
- ),
141
- ]
142
-
143
  response = chat_engine.stream_chat(message)
144
  text = "".join(response.response_gen)
145
-
146
  history.append((message, text))
147
  return history
148
 
149
  # ===================================
150
- # 7️⃣ Fungsi Utama untuk Menjalankan Aplikasi
151
  # ===================================
152
  def main():
153
  model_path = initialize_llama_model()
154
- initialize_settings(model_path)
155
-
156
  index = initialize_index()
157
- chat_engine = initialize_chat_engine(index)
158
-
159
  def chatbot_response(message, history):
160
- return generate_response(message, history, chat_engine)
161
-
162
  gr.Interface(
163
  fn=chatbot_response,
164
  inputs=["text"],
 
1
+ import json
2
  import gradio as gr
3
  import gspread
4
  from oauth2client.service_account import ServiceAccountCredentials
 
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
 
26
+ all_data = []
 
 
27
  spreadsheet = client.open_by_key(SPREADSHEET_ID)
28
  for sheet_name in sheet_names:
29
  try:
30
  sheet = spreadsheet.worksheet(sheet_name)
31
  data = sheet.get_all_values()
 
 
32
  all_data.append(f"=== Data dari {sheet_name.upper()} ===")
33
  all_data.extend([" | ".join(row) for row in data])
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
  return model_path
62
 
63
  # ===================================
64
+ # 4️⃣ Inisialisasi Model dan Pengaturan
65
  # ===================================
66
  def initialize_settings(model_path):
67
  Settings.llm = LlamaCPP(
 
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
  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
+
120
  def chatbot_response(message, history):
121
+ return generate_response(message, history, chat_engine)
122
+
123
  gr.Interface(
124
  fn=chatbot_response,
125
  inputs=["text"],