Spaces:
Paused
Paused
| # Import Library yang Diperlukan | |
| import gradio as gr | |
| import gspread | |
| from oauth2client.service_account import ServiceAccountCredentials | |
| from llama_cpp import Llama | |
| from llama_index.core import VectorStoreIndex, Settings | |
| from llama_index.core.node_parser import SentenceSplitter | |
| from llama_index.embeddings.huggingface import HuggingFaceEmbedding | |
| from llama_index.llms.llama_cpp import LlamaCPP | |
| from huggingface_hub import hf_hub_download | |
| from llama_index.core.llms import ChatMessage | |
| from llama_index.core.chat_engine.condense_plus_context import CondensePlusContextChatEngine | |
| # =================================== | |
| # 1๏ธโฃ Fungsi untuk Membaca Google Spreadsheet | |
| # =================================== | |
| def read_google_sheet(): | |
| # Tentukan scope akses ke Google Sheets & Drive | |
| scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"] | |
| # Load kredensial dari file credentials.json | |
| creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope) | |
| client = gspread.authorize(creds) | |
| # ๐ GANTI BAGIAN INI SESUAI SPREADSHEET ANDA | |
| spreadsheet = client.open("datatarget") # ๐น Ganti dengan nama spreadsheet Anda | |
| sheet = spreadsheet.datatarget # ๐น Jika ingin sheet lain, ganti dengan spreadsheet.worksheet("NamaSheet") | |
| # Ambil semua data dalam bentuk list (baris & kolom) | |
| data = sheet.get_all_values() | |
| # Format ulang data menjadi satu teks panjang (dapat disesuaikan) | |
| formatted_text = "\n".join([" | ".join(row) for row in data]) | |
| return formatted_text | |
| # =================================== | |
| # 2๏ธโฃ Fungsi untuk Mengunduh Model Llama | |
| # =================================== | |
| def initialize_llama_model(): | |
| model_path = hf_hub_download( | |
| repo_id="TheBLoke/zephyr-7b-beta-GGUF", # ๐ Repo model HuggingFace | |
| filename="zephyr-7b-beta.Q4_K_M.gguf", # ๐ Nama file model | |
| cache_dir="./models" | |
| ) | |
| return model_path | |
| # =================================== | |
| # 3๏ธโฃ Inisialisasi Model dan Pengaturan | |
| # =================================== | |
| def initialize_settings(model_path): | |
| Settings.llm = LlamaCPP( | |
| model_path=model_path, | |
| temperature=0.7, | |
| ) | |
| # =================================== | |
| # 4๏ธโฃ Inisialisasi Index dari Data Spreadsheet | |
| # =================================== | |
| def initialize_index(): | |
| # ๐น Ambil teks dari Google Spreadsheet | |
| text_data = read_google_sheet() | |
| # ๐น Konversi teks ke dalam format dokumen | |
| documents = [text_data] | |
| # ๐น Proses data menjadi node untuk vektor embedding | |
| parser = SentenceSplitter(chunk_size=150, chunk_overlap=10) | |
| nodes = parser.get_nodes_from_documents(documents) | |
| # ๐น Gunakan model embedding | |
| embedding = HuggingFaceEmbedding("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2") | |
| Settings.embed_model = embedding | |
| # ๐น Buat index vektor | |
| index = VectorStoreIndex(nodes) | |
| return index | |
| # =================================== | |
| # 5๏ธโฃ Inisialisasi Mesin Chatbot | |
| # =================================== | |
| def initialize_chat_engine(index): | |
| retriever = index.as_retriever(similarity_top_k=3) | |
| chat_engine = CondensePlusContextChatEngine.from_defaults( | |
| retriever=retriever, | |
| verbose=True, | |
| ) | |
| return chat_engine | |
| # =================================== | |
| # 6๏ธโฃ Fungsi untuk Menghasilkan Respons Chatbot | |
| # =================================== | |
| def generate_response(message, history, chat_engine): | |
| if history is None: | |
| history = [] | |
| chat_messages = [ | |
| ChatMessage( | |
| role="system", | |
| content="Anda adalah chatbot yang menjawab dalam bahasa Indonesia berdasarkan dokumen di Google Spreadsheet." | |
| ), | |
| ] | |
| response = chat_engine.stream_chat(message) | |
| text = "".join(response.response_gen) # ๐น Gabungkan semua token menjadi string | |
| history.append((message, text)) | |
| return history | |
| # =================================== | |
| # 7๏ธโฃ Fungsi Utama untuk Menjalankan Aplikasi | |
| # =================================== | |
| def main(): | |
| # ๐น Unduh model dan inisialisasi pengaturan | |
| model_path = initialize_llama_model() | |
| initialize_settings(model_path) | |
| # ๐น Inisialisasi index dan chat engine | |
| index = initialize_index() | |
| chat_engine = initialize_chat_engine(index) | |
| # ๐น Fungsi untuk chat | |
| def chatbot_response(message, history): | |
| return generate_response(message, history, chat_engine) | |
| # ๐น Luncurkan Gradio UI | |
| gr.Interface( | |
| fn=chatbot_response, | |
| inputs=["text"], | |
| outputs=["text"], | |
| ).launch() | |
| if __name__ == "__main__": | |
| main() |