Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from langchain_community.vectorstores import FAISS
|
| 3 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 4 |
from groq import Groq
|
| 5 |
|
| 6 |
# Load FAISS index
|
| 7 |
-
vector_store = FAISS.load_local("robohome_faiss", HuggingFaceEmbeddings())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Inisialisasi API Groq
|
| 10 |
-
client = Groq(api_key=
|
| 11 |
|
| 12 |
-
def retrieve_and_generate(query):
|
|
|
|
| 13 |
# Retrieve top 3 documents
|
| 14 |
docs = vector_store.similarity_search(query, k=3)
|
| 15 |
context = "\n\n".join([doc.page_content for doc in docs])
|
|
@@ -25,15 +32,18 @@ def retrieve_and_generate(query):
|
|
| 25 |
max_tokens=200
|
| 26 |
)
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# UI dengan Gradio
|
| 31 |
-
iface = gr.
|
| 32 |
fn=retrieve_and_generate,
|
| 33 |
-
|
| 34 |
-
|
| 35 |
title="RoboHome RAG Chatbot",
|
| 36 |
description="Chatbot ini menjawab pertanyaan berdasarkan dokumentasi RoboHome.",
|
| 37 |
)
|
| 38 |
|
| 39 |
-
iface.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from langchain_community.vectorstores import FAISS
|
| 4 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 5 |
from groq import Groq
|
| 6 |
|
| 7 |
# Load FAISS index
|
| 8 |
+
vector_store = FAISS.load_local("faiss_index/robohome_faiss", HuggingFaceEmbeddings()) # Pastikan path benar
|
| 9 |
+
|
| 10 |
+
# Load API Key dari variabel lingkungan
|
| 11 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 12 |
+
if not GROQ_API_KEY:
|
| 13 |
+
raise ValueError("⚠️ API Key Groq tidak ditemukan! Setel variabel lingkungan 'GROQ_API_KEY'.")
|
| 14 |
|
| 15 |
# Inisialisasi API Groq
|
| 16 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 17 |
|
| 18 |
+
def retrieve_and_generate(query, history=[]):
|
| 19 |
+
"""Retrieve knowledge base & generate response."""
|
| 20 |
# Retrieve top 3 documents
|
| 21 |
docs = vector_store.similarity_search(query, k=3)
|
| 22 |
context = "\n\n".join([doc.page_content for doc in docs])
|
|
|
|
| 32 |
max_tokens=200
|
| 33 |
)
|
| 34 |
|
| 35 |
+
# Return hasil dalam format chat
|
| 36 |
+
bot_response = response.choices[0].message.content
|
| 37 |
+
history.append((query, bot_response)) # Simpan ke history chat
|
| 38 |
+
return history, history
|
| 39 |
|
| 40 |
# UI dengan Gradio
|
| 41 |
+
iface = gr.ChatInterface(
|
| 42 |
fn=retrieve_and_generate,
|
| 43 |
+
chatbot=gr.Chatbot(label="Jawaban RoboHome"),
|
| 44 |
+
textbox=gr.Textbox(label="Ajukan pertanyaan tentang RoboHome"),
|
| 45 |
title="RoboHome RAG Chatbot",
|
| 46 |
description="Chatbot ini menjawab pertanyaan berdasarkan dokumentasi RoboHome.",
|
| 47 |
)
|
| 48 |
|
| 49 |
+
iface.launch(share=True) # Share=True untuk membuat link publik
|