Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,14 @@
|
|
1 |
# Import Library yang Diperlukan
|
2 |
import gradio as gr
|
3 |
-
import
|
|
|
4 |
import os
|
5 |
-
import
|
|
|
|
|
|
|
6 |
from llama_cpp import Llama
|
7 |
-
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, Settings
|
8 |
from llama_index.core.llms import ChatMessage
|
9 |
from llama_index.llms.llama_cpp import LlamaCPP
|
10 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
@@ -13,11 +17,10 @@ from llama_index.core.node_parser import SentenceSplitter
|
|
13 |
|
14 |
# Fungsi untuk mengunduh model Llama
|
15 |
def initialize_llama_model():
|
16 |
-
# Unduh model jika belum ada di direktori kerja
|
17 |
model_path = hf_hub_download(
|
18 |
-
repo_id="TheBLoke/zephyr-7b-beta-GGUF",
|
19 |
-
filename="zephyr-7b-beta.Q4_K_M.gguf",
|
20 |
-
cache_dir="./models"
|
21 |
)
|
22 |
return model_path
|
23 |
|
@@ -28,74 +31,99 @@ def initialize_settings(model_path):
|
|
28 |
temperature=0.7,
|
29 |
)
|
30 |
|
31 |
-
# Fungsi untuk
|
32 |
-
def
|
33 |
-
#
|
34 |
-
|
35 |
-
"bahandokumen/bonus.txt",
|
36 |
-
"bahandokumen/cuti.txt",
|
37 |
-
"bahandokumen/disiplinkerja.txt",
|
38 |
-
"bahandokumen/fasilitas&bantuan.txt",
|
39 |
-
"bahandokumen/upahlembur.txt",
|
40 |
-
"bahandokumen/waktukerja.txt"]).load_data()
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
parser = SentenceSplitter(chunk_size=150, chunk_overlap=10)
|
43 |
nodes = parser.get_nodes_from_documents(documents)
|
|
|
|
|
44 |
embedding = HuggingFaceEmbedding("firqaaa/indo-sentence-bert-base")
|
45 |
Settings.embed_model = embedding
|
|
|
|
|
|
|
46 |
index = VectorStoreIndex(nodes)
|
47 |
-
|
48 |
|
49 |
# Inisialisasi Mesin Chat
|
50 |
-
def initialize_chat_engine(
|
51 |
-
|
52 |
-
from llama_index.core.chat_engine.condense_plus_context import CondensePlusContextChatEngine
|
53 |
-
retriever = index.as_retriever(similarity_top_k=3)
|
54 |
-
chat_engine = CondensePlusContextChatEngine.from_defaults(
|
55 |
-
retriever=retriever,
|
56 |
-
verbose=True,
|
57 |
-
)
|
58 |
-
return chat_engine
|
59 |
|
60 |
# Fungsi untuk menghasilkan respons chatbot
|
61 |
-
def generate_response(message, history
|
62 |
-
|
63 |
-
ChatMessage(
|
64 |
-
role="system",
|
65 |
-
content="Anda adalah chatbot yang selalu menjawab pertanyaan secara singkat, ramah, dan jelas dalam bahasa Indonesia."
|
66 |
-
),
|
67 |
-
]
|
68 |
response = chat_engine.stream_chat(message)
|
69 |
-
text = "".join(response.response_gen)
|
70 |
-
history.append((message, text))
|
71 |
return history
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
# Inisialisasi Komponen Gradio untuk UI
|
77 |
-
def launch_gradio(
|
78 |
with gr.Blocks() as demo:
|
79 |
-
# Mengatur tombol untuk menghapus riwayat chat
|
80 |
clear_btn = gr.Button("Clear")
|
81 |
-
|
82 |
-
|
83 |
-
# Membuat antarmuka chat
|
84 |
-
chat_interface = gr.ChatInterface(
|
85 |
-
lambda message, history: generate_response(message, history, chat_engine)
|
86 |
-
)
|
87 |
demo.launch()
|
88 |
|
89 |
# Fungsi Utama untuk Menjalankan Aplikasi
|
90 |
def main():
|
91 |
-
#
|
92 |
model_path = initialize_llama_model()
|
93 |
-
initialize_settings(model_path)
|
94 |
-
|
95 |
-
index
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
99 |
|
100 |
if __name__ == "__main__":
|
101 |
-
main()
|
|
|
1 |
# Import Library yang Diperlukan
|
2 |
import gradio as gr
|
3 |
+
import gdown
|
4 |
+
import pandas as pd
|
5 |
import os
|
6 |
+
import threading
|
7 |
+
import schedule
|
8 |
+
import time
|
9 |
+
from datetime import datetime
|
10 |
from llama_cpp import Llama
|
11 |
+
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, Settings, Document
|
12 |
from llama_index.core.llms import ChatMessage
|
13 |
from llama_index.llms.llama_cpp import LlamaCPP
|
14 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
|
|
17 |
|
18 |
# Fungsi untuk mengunduh model Llama
|
19 |
def initialize_llama_model():
|
|
|
20 |
model_path = hf_hub_download(
|
21 |
+
repo_id="TheBLoke/zephyr-7b-beta-GGUF",
|
22 |
+
filename="zephyr-7b-beta.Q4_K_M.gguf",
|
23 |
+
cache_dir="./models"
|
24 |
)
|
25 |
return model_path
|
26 |
|
|
|
31 |
temperature=0.7,
|
32 |
)
|
33 |
|
34 |
+
# Fungsi untuk mengunduh file CSV terbaru dari Google Drive
|
35 |
+
def download_csv_from_drive():
|
36 |
+
csv_url = "https://drive.google.com/uc?id=1UIx369_8GlzPiKArMVg8v-IwC6hYTYA0" # Ganti dengan ID file Google Drive kamu
|
37 |
+
output_csv = "data.csv"
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
if os.path.exists(output_csv):
|
40 |
+
os.remove(output_csv) # Hapus file lama
|
41 |
+
|
42 |
+
print("π Mengunduh file CSV terbaru...")
|
43 |
+
gdown.download(csv_url, output_csv, quiet=False)
|
44 |
+
return output_csv
|
45 |
+
|
46 |
+
# Fungsi untuk update index (dijalankan 1x sehari)
|
47 |
+
def update_index():
|
48 |
+
print(f"π [{datetime.now()}] Mengupdate index dengan data terbaru...")
|
49 |
+
csv_file = download_csv_from_drive()
|
50 |
+
|
51 |
+
# Baca CSV dengan Pandas
|
52 |
+
df = pd.read_csv(csv_file)
|
53 |
+
|
54 |
+
# Konversi isi CSV menjadi dokumen teks
|
55 |
+
documents = [Document(text=" | ".join(map(str, row.values))) for _, row in df.iterrows()]
|
56 |
+
|
57 |
+
# Tambahkan file dokumen lain
|
58 |
+
text_documents = SimpleDirectoryReader(input_files=[
|
59 |
+
"bahandokumen/K3.txt",
|
60 |
+
"bahandokumen/bonus.txt",
|
61 |
+
"bahandokumen/cuti.txt",
|
62 |
+
"bahandokumen/disiplinkerja.txt",
|
63 |
+
"bahandokumen/fasilitas&bantuan.txt",
|
64 |
+
"bahandokumen/upahlembur.txt",
|
65 |
+
"bahandokumen/waktukerja.txt"
|
66 |
+
]).load_data()
|
67 |
+
|
68 |
+
documents.extend(text_documents)
|
69 |
+
|
70 |
+
# Parsing dokumen menjadi nodes
|
71 |
parser = SentenceSplitter(chunk_size=150, chunk_overlap=10)
|
72 |
nodes = parser.get_nodes_from_documents(documents)
|
73 |
+
|
74 |
+
# Gunakan model embedding
|
75 |
embedding = HuggingFaceEmbedding("firqaaa/indo-sentence-bert-base")
|
76 |
Settings.embed_model = embedding
|
77 |
+
|
78 |
+
# Buat index vektor
|
79 |
+
global index
|
80 |
index = VectorStoreIndex(nodes)
|
81 |
+
print("β
Index berhasil diperbarui!")
|
82 |
|
83 |
# Inisialisasi Mesin Chat
|
84 |
+
def initialize_chat_engine():
|
85 |
+
return index.as_chat_engine(chat_mode="condense_plus_context", similarity_top_k=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
# Fungsi untuk menghasilkan respons chatbot
|
88 |
+
def generate_response(message, history):
|
89 |
+
chat_engine = initialize_chat_engine() # Gunakan index yang sudah diperbarui
|
|
|
|
|
|
|
|
|
|
|
90 |
response = chat_engine.stream_chat(message)
|
91 |
+
text = "".join(response.response_gen)
|
92 |
+
history.append((message, text))
|
93 |
return history
|
94 |
|
95 |
+
# Scheduler untuk update otomatis setiap jam 12 malam
|
96 |
+
def schedule_update():
|
97 |
+
schedule.every().day.at("00:00").do(update_index)
|
98 |
+
while True:
|
99 |
+
schedule.run_pending()
|
100 |
+
time.sleep(60) # Cek setiap 1 menit
|
101 |
+
|
102 |
+
# Fungsi untuk menjalankan scheduler di thread terpisah
|
103 |
+
def start_scheduler():
|
104 |
+
thread = threading.Thread(target=schedule_update, daemon=True)
|
105 |
+
thread.start()
|
106 |
+
|
107 |
# Inisialisasi Komponen Gradio untuk UI
|
108 |
+
def launch_gradio():
|
109 |
with gr.Blocks() as demo:
|
|
|
110 |
clear_btn = gr.Button("Clear")
|
111 |
+
chat_interface = gr.ChatInterface(lambda message, history: generate_response(message, history))
|
|
|
|
|
|
|
|
|
|
|
112 |
demo.launch()
|
113 |
|
114 |
# Fungsi Utama untuk Menjalankan Aplikasi
|
115 |
def main():
|
116 |
+
global index # Agar index bisa diperbarui secara global
|
117 |
model_path = initialize_llama_model()
|
118 |
+
initialize_settings(model_path)
|
119 |
+
|
120 |
+
print("π Inisialisasi index pertama kali...")
|
121 |
+
update_index() # Buat index pertama kali
|
122 |
+
|
123 |
+
print("β³ Menjalankan scheduler update index (setiap jam 12 malam)...")
|
124 |
+
start_scheduler() # Jalankan update otomatis di background
|
125 |
+
|
126 |
+
launch_gradio() # Jalankan chatbot
|
127 |
|
128 |
if __name__ == "__main__":
|
129 |
+
main()
|