Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,12 @@ import time
|
|
4 |
from oauth2client.service_account import ServiceAccountCredentials
|
5 |
from llama_cpp import Llama
|
6 |
from llama_index.core import VectorStoreIndex, Settings
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
from llama_index.core.schema import Document
|
8 |
|
9 |
# ===================================
|
@@ -19,6 +25,21 @@ 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 |
except gspread.exceptions.WorksheetNotFound:
|
23 |
all_data.append(f"❌ ERROR: Worksheet {sheet_name} tidak ditemukan.")
|
24 |
|
@@ -27,6 +48,12 @@ def read_google_sheets():
|
|
27 |
|
28 |
except gspread.exceptions.SpreadsheetNotFound:
|
29 |
return "❌ ERROR: Spreadsheet tidak ditemukan!"
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
# ===================================
|
31 |
def initialize_llama_model():
|
32 |
model_path = hf_hub_download(
|
@@ -34,23 +61,24 @@ def initialize_llama_model():
|
|
34 |
filename="zephyr-7b-beta.Q4_K_M.gguf",
|
35 |
cache_dir="./models"
|
36 |
)
|
|
|
|
|
37 |
# ===================================
|
38 |
# 3️⃣ Inisialisasi Pengaturan Model
|
39 |
# ===================================
|
40 |
-
|
41 |
def initialize_settings(model_path):
|
42 |
Settings.llm = LlamaCPP(
|
43 |
model_path=model_path,
|
44 |
temperature=0.7,
|
45 |
-
|
46 |
-
max_new_tokens=512,
|
47 |
-
# n_gpu_layers=20, # ❌ Hapus jika error
|
48 |
-
model_kwargs={"n_ctx": 4096}
|
49 |
)
|
50 |
|
51 |
-
|
52 |
# ===================================
|
53 |
# 4️⃣ Inisialisasi Index & Chat Engine
|
|
|
|
|
|
|
|
|
54 |
parser = SentenceSplitter(chunk_size=100, chunk_overlap=30)
|
55 |
nodes = parser.get_nodes_from_documents([document])
|
56 |
|
@@ -68,6 +96,7 @@ def initialize_chat_engine(index):
|
|
68 |
)
|
69 |
return chat_engine
|
70 |
|
|
|
71 |
# 5️⃣ Fungsi untuk Merapikan Jawaban Chatbot
|
72 |
# ===================================
|
73 |
def clean_response(response):
|
@@ -76,6 +105,18 @@ def clean_response(response):
|
|
76 |
text = text.replace("user:", "").replace("jawaban:", "").replace("assistant:", "").strip()
|
77 |
return text
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
"Jangan menjawab menggunakan Bahasa Inggris. "
|
80 |
"Gunakan Bahasa Indonesia dengan gaya profesional dan ramah. "
|
81 |
"Jika informasi tidak tersedia dalam dokumen, katakan dengan sopan bahwa Anda tidak tahu. "
|
@@ -92,6 +133,8 @@ def clean_response(response):
|
|
92 |
history.append((message, cleaned_text))
|
93 |
return cleaned_text
|
94 |
|
|
|
|
|
95 |
# ===================================
|
96 |
def main():
|
97 |
model_path = initialize_llama_model()
|
|
|
4 |
from oauth2client.service_account import ServiceAccountCredentials
|
5 |
from llama_cpp import Llama
|
6 |
from llama_index.core import VectorStoreIndex, Settings
|
7 |
+
from llama_index.core.node_parser import SentenceSplitter
|
8 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
9 |
+
from llama_index.llms.llama_cpp import LlamaCPP
|
10 |
+
from huggingface_hub import hf_hub_download
|
11 |
+
from llama_index.core.llms import ChatMessage
|
12 |
+
from llama_index.core.chat_engine.condense_plus_context import CondensePlusContextChatEngine
|
13 |
from llama_index.core.schema import Document
|
14 |
|
15 |
# ===================================
|
|
|
25 |
try:
|
26 |
scope = ["https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive"]
|
27 |
creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
|
28 |
+
client = gspread.authorize(creds)
|
29 |
+
|
30 |
+
SPREADSHEET_ID = "1e_cNMhwF-QYpyYUpqQh-XCw-OdhWS6EuYsoBUsVtdNg"
|
31 |
+
sheet_names = ["datatarget", "datacuti", "dataabsen", "datalembur", "pkb"]
|
32 |
+
|
33 |
+
all_data = []
|
34 |
+
spreadsheet = client.open_by_key(SPREADSHEET_ID)
|
35 |
+
|
36 |
+
for sheet_name in sheet_names:
|
37 |
+
try:
|
38 |
+
sheet = spreadsheet.worksheet(sheet_name)
|
39 |
+
data = sheet.get_all_values()
|
40 |
+
all_data.append(f"=== Data dari {sheet_name.upper()} ===")
|
41 |
+
all_data.extend([" | ".join(row) for row in data])
|
42 |
+
all_data.append("\n")
|
43 |
except gspread.exceptions.WorksheetNotFound:
|
44 |
all_data.append(f"❌ ERROR: Worksheet {sheet_name} tidak ditemukan.")
|
45 |
|
|
|
48 |
|
49 |
except gspread.exceptions.SpreadsheetNotFound:
|
50 |
return "❌ ERROR: Spreadsheet tidak ditemukan!"
|
51 |
+
|
52 |
+
except Exception as e:
|
53 |
+
return f"❌ ERROR: {str(e)}"
|
54 |
+
|
55 |
+
# ===================================
|
56 |
+
# 2️⃣ Inisialisasi Model Llama
|
57 |
# ===================================
|
58 |
def initialize_llama_model():
|
59 |
model_path = hf_hub_download(
|
|
|
61 |
filename="zephyr-7b-beta.Q4_K_M.gguf",
|
62 |
cache_dir="./models"
|
63 |
)
|
64 |
+
return model_path
|
65 |
+
|
66 |
# ===================================
|
67 |
# 3️⃣ Inisialisasi Pengaturan Model
|
68 |
# ===================================
|
|
|
69 |
def initialize_settings(model_path):
|
70 |
Settings.llm = LlamaCPP(
|
71 |
model_path=model_path,
|
72 |
temperature=0.7,
|
73 |
+
n_gpu_layers=20 # 🔥 Mempercepat inferensi dengan GPU jika tersedia
|
|
|
|
|
|
|
74 |
)
|
75 |
|
|
|
76 |
# ===================================
|
77 |
# 4️⃣ Inisialisasi Index & Chat Engine
|
78 |
+
# ===================================
|
79 |
+
def initialize_index():
|
80 |
+
text_data = read_google_sheets()
|
81 |
+
document = Document(text=text_data)
|
82 |
parser = SentenceSplitter(chunk_size=100, chunk_overlap=30)
|
83 |
nodes = parser.get_nodes_from_documents([document])
|
84 |
|
|
|
96 |
)
|
97 |
return chat_engine
|
98 |
|
99 |
+
# ===================================
|
100 |
# 5️⃣ Fungsi untuk Merapikan Jawaban Chatbot
|
101 |
# ===================================
|
102 |
def clean_response(response):
|
|
|
105 |
text = text.replace("user:", "").replace("jawaban:", "").replace("assistant:", "").strip()
|
106 |
return text
|
107 |
|
108 |
+
# ===================================
|
109 |
+
# 6️⃣ Fungsi untuk Menghasilkan Respons Chatbot
|
110 |
+
# ===================================
|
111 |
+
def generate_response(message, history, chat_engine):
|
112 |
+
if history is None:
|
113 |
+
history = []
|
114 |
+
|
115 |
+
chat_messages = [
|
116 |
+
ChatMessage(
|
117 |
+
role="system",
|
118 |
+
content=(
|
119 |
+
"Anda adalah chatbot HRD yang membantu karyawan memahami administrasi perusahaan. "
|
120 |
"Jangan menjawab menggunakan Bahasa Inggris. "
|
121 |
"Gunakan Bahasa Indonesia dengan gaya profesional dan ramah. "
|
122 |
"Jika informasi tidak tersedia dalam dokumen, katakan dengan sopan bahwa Anda tidak tahu. "
|
|
|
133 |
history.append((message, cleaned_text))
|
134 |
return cleaned_text
|
135 |
|
136 |
+
# ===================================
|
137 |
+
# 7️⃣ Fungsi Utama untuk Menjalankan Aplikasi
|
138 |
# ===================================
|
139 |
def main():
|
140 |
model_path = initialize_llama_model()
|