BramLeo commited on
Commit
2385eda
Β·
verified Β·
1 Parent(s): 5e3c1f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -49
app.py CHANGED
@@ -13,39 +13,31 @@ 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
17
  # ===================================
18
- def read_google_sheet():
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
- # πŸ“Œ Gunakan ID Spreadsheet (lebih aman)
28
- SPREADSHEET_ID = "1e_cNMhwF-QYpyYUpqQh-XCw-OdhWS6EuYsoBUsVtdNg" # πŸ”Ή Ganti dengan ID spreadsheet Anda
29
- SHEET_NAME = "datatarget" # πŸ”Ή Ganti dengan nama sheet
30
 
31
- # Buka spreadsheet dan worksheet
32
  spreadsheet = client.open_by_key(SPREADSHEET_ID)
33
- sheet = spreadsheet.worksheet(SHEET_NAME)
34
-
35
- # Ambil semua data dalam bentuk list (baris & kolom)
36
- data = sheet.get_all_values()
37
-
38
- # Format ulang data menjadi satu teks panjang (dapat disesuaikan)
39
- formatted_text = "\n".join([" | ".join(row) for row in data])
40
 
41
- return formatted_text
42
-
43
- except gspread.exceptions.SpreadsheetNotFound:
44
- return "❌ ERROR: Spreadsheet tidak ditemukan. Pastikan ID/nama benar!"
45
-
46
- except gspread.exceptions.WorksheetNotFound:
47
- return "❌ ERROR: Worksheet tidak ditemukan. Periksa kembali nama sheet!"
48
-
 
 
 
49
  except Exception as e:
50
  return f"❌ ERROR: {str(e)}"
51
 
@@ -54,8 +46,8 @@ def read_google_sheet():
54
  # ===================================
55
  def initialize_llama_model():
56
  model_path = hf_hub_download(
57
- repo_id="TheBLoke/zephyr-7b-beta-GGUF", # πŸ“Œ Repo model HuggingFace
58
- filename="zephyr-7b-beta.Q4_K_M.gguf", # πŸ“Œ Nama file model
59
  cache_dir="./models"
60
  )
61
  return model_path
@@ -73,22 +65,16 @@ def initialize_settings(model_path):
73
  # 4️⃣ Inisialisasi Index dari Data Spreadsheet
74
  # ===================================
75
  def initialize_index():
76
- # πŸ”Ή Ambil teks dari Google Spreadsheet
77
- text_data = read_google_sheet()
78
-
79
- # πŸ”Ή Konversi teks ke dalam format dokumen yang benar
80
- document = Document(text=text_data) # πŸ”Ή Ubah teks menjadi objek `Document`
81
- documents = [document] # πŸ”Ή Masukkan ke dalam list
82
-
83
- # πŸ”Ή Proses data menjadi node untuk vektor embedding
84
  parser = SentenceSplitter(chunk_size=150, chunk_overlap=10)
85
- nodes = parser.get_nodes_from_documents(documents) # βœ… Sekarang `documents` adalah list of `Document`
86
-
87
- # πŸ”Ή Gunakan model embedding
88
  embedding = HuggingFaceEmbedding("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
89
  Settings.embed_model = embedding
90
-
91
- # πŸ”Ή Buat index vektor
92
  index = VectorStoreIndex(nodes)
93
  return index
94
 
@@ -110,23 +96,20 @@ def generate_response(message, history, chat_engine):
110
  if history is None:
111
  history = []
112
 
113
- # πŸ”Ή Ambil data terbaru dari Google Spreadsheet setiap kali ada pertanyaan
114
- text_data = read_google_sheet()
115
  document = Document(text=text_data)
116
  documents = [document]
117
-
118
- # πŸ”Ή Perbarui index dengan data terbaru
119
  parser = SentenceSplitter(chunk_size=150, chunk_overlap=10)
120
  nodes = parser.get_nodes_from_documents(documents)
121
  index = VectorStoreIndex(nodes)
122
  retriever = index.as_retriever(similarity_top_k=3)
123
-
124
- # πŸ”Ή Buat ulang chat engine dengan index yang diperbarui
125
  chat_engine = CondensePlusContextChatEngine.from_defaults(
126
  retriever=retriever,
127
  verbose=True,
128
  )
129
-
130
  chat_messages = [
131
  ChatMessage(
132
  role="system",
@@ -138,11 +121,10 @@ def generate_response(message, history, chat_engine):
138
  "Fokuslah memberikan jawaban yang akurat dan relevan sesuai dengan dokumen yang tersedia."
139
  ),
140
  ]
141
-
142
- # πŸ”Ή Gunakan chat engine baru untuk menjawab pertanyaan
143
  response = chat_engine.stream_chat(message)
144
  text = "".join(response.response_gen)
145
-
146
  history.append((message, text))
147
  return history
148
 
 
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
 
24
+ SPREADSHEET_ID = "1e_cNMhwF-QYpyYUpqQh-XCw-OdhWS6EuYsoBUsVtdNg"
25
+ sheet_names = ["datatarget", "dataabsen", "datacuti", "datalembur"] # πŸ”Ή Daftar sheet yang akan dibaca
 
26
 
27
+ combined_text = ""
28
  spreadsheet = client.open_by_key(SPREADSHEET_ID)
 
 
 
 
 
 
 
29
 
30
+ for sheet_name in sheet_names:
31
+ try:
32
+ sheet = spreadsheet.worksheet(sheet_name)
33
+ data = sheet.get_all_values()
34
+ formatted_text = f"\n=== Data dari {sheet_name} ===\n"
35
+ formatted_text += "\n".join([" | ".join(row) for row in data])
36
+ combined_text += formatted_text + "\n"
37
+ except gspread.exceptions.WorksheetNotFound:
38
+ combined_text += f"\n❌ ERROR: Worksheet '{sheet_name}' tidak ditemukan!\n"
39
+
40
+ return combined_text.strip()
41
  except Exception as e:
42
  return f"❌ ERROR: {str(e)}"
43
 
 
46
  # ===================================
47
  def initialize_llama_model():
48
  model_path = hf_hub_download(
49
+ repo_id="TheBLoke/zephyr-7b-beta-GGUF",
50
+ filename="zephyr-7b-beta.Q4_K_M.gguf",
51
  cache_dir="./models"
52
  )
53
  return model_path
 
65
  # 4️⃣ Inisialisasi Index dari Data Spreadsheet
66
  # ===================================
67
  def initialize_index():
68
+ text_data = read_google_sheets()
69
+ document = Document(text=text_data)
70
+ documents = [document]
71
+
 
 
 
 
72
  parser = SentenceSplitter(chunk_size=150, chunk_overlap=10)
73
+ nodes = parser.get_nodes_from_documents(documents)
74
+
 
75
  embedding = HuggingFaceEmbedding("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
76
  Settings.embed_model = embedding
77
+
 
78
  index = VectorStoreIndex(nodes)
79
  return index
80
 
 
96
  if history is None:
97
  history = []
98
 
99
+ text_data = read_google_sheets()
 
100
  document = Document(text=text_data)
101
  documents = [document]
102
+
 
103
  parser = SentenceSplitter(chunk_size=150, chunk_overlap=10)
104
  nodes = parser.get_nodes_from_documents(documents)
105
  index = VectorStoreIndex(nodes)
106
  retriever = index.as_retriever(similarity_top_k=3)
107
+
 
108
  chat_engine = CondensePlusContextChatEngine.from_defaults(
109
  retriever=retriever,
110
  verbose=True,
111
  )
112
+
113
  chat_messages = [
114
  ChatMessage(
115
  role="system",
 
121
  "Fokuslah memberikan jawaban yang akurat dan relevan sesuai dengan dokumen yang tersedia."
122
  ),
123
  ]
124
+
 
125
  response = chat_engine.stream_chat(message)
126
  text = "".join(response.response_gen)
127
+
128
  history.append((message, text))
129
  return history
130