Jaal047 commited on
Commit
4d53c99
Β·
verified Β·
1 Parent(s): 1a25261

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -22
app.py CHANGED
@@ -4,35 +4,36 @@ from langchain_community.vectorstores import FAISS
4
  from langchain_huggingface import HuggingFaceEmbeddings
5
  from groq import Groq
6
 
7
- # Path FAISS index
8
  faiss_path = "faiss_index"
9
 
10
- # Pastikan file FAISS index ada sebelum loading
11
  if not os.path.exists(f"{faiss_path}/index.faiss"):
12
- raise FileNotFoundError(f"⚠️ File FAISS index tidak ditemukan di {faiss_path}. Pastikan Anda telah membuat dan mengunggahnya!")
13
 
14
- # Load FAISS index
15
  vector_store = FAISS.load_local(
16
  faiss_path,
17
  HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2"),
18
  allow_dangerous_deserialization=True
19
  )
20
 
21
- # Load API Key dari variabel lingkungan
22
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
23
  if not GROQ_API_KEY:
24
  raise ValueError("⚠️ API Key Groq tidak ditemukan! Setel variabel lingkungan 'GROQ_API_KEY'.")
25
 
26
- # Inisialisasi API Groq
27
  client = Groq(api_key=GROQ_API_KEY)
28
 
29
  def retrieve_and_generate(query, history=[]):
30
- """Retrieve knowledge base & generate response."""
31
- # Retrieve top 3 documents
 
32
  docs = vector_store.similarity_search(query, k=3)
33
  context = "\n\n".join([doc.page_content for doc in docs])
34
 
35
- # Generate response with LLM
36
  response = client.chat.completions.create(
37
  model="mixtral-8x7b-32768",
38
  messages=[
@@ -43,18 +44,22 @@ def retrieve_and_generate(query, history=[]):
43
  max_tokens=200
44
  )
45
 
46
- # Return hasil dalam format chat
47
  bot_response = response.choices[0].message.content
48
- history.append((query, bot_response)) # Simpan ke history chat
49
- return history, history
50
-
51
- # UI dengan Gradio
52
- iface = gr.ChatInterface(
53
- fn=retrieve_and_generate,
54
- chatbot=gr.Chatbot(label="Jawaban RoboHome"),
55
- textbox=gr.Textbox(label="Ajukan pertanyaan tentang RoboHome"),
56
- title="RoboHome RAG Chatbot",
57
- description="Chatbot ini menjawab pertanyaan berdasarkan dokumentasi RoboHome.",
58
- )
 
 
 
 
 
59
 
60
- iface.launch(share=True) # Share=True untuk membuat link publik
 
4
  from langchain_huggingface import HuggingFaceEmbeddings
5
  from groq import Groq
6
 
7
+ # βœ… Path FAISS index
8
  faiss_path = "faiss_index"
9
 
10
+ # βœ… Pastikan FAISS index ada sebelum loading
11
  if not os.path.exists(f"{faiss_path}/index.faiss"):
12
+ raise FileNotFoundError(f"⚠️ FAISS index tidak ditemukan di {faiss_path}. Pastikan Anda telah mengunggahnya!")
13
 
14
+ # βœ… Load FAISS index
15
  vector_store = FAISS.load_local(
16
  faiss_path,
17
  HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2"),
18
  allow_dangerous_deserialization=True
19
  )
20
 
21
+ # βœ… Load API Key dari Secrets di Hugging Face Spaces
22
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
23
  if not GROQ_API_KEY:
24
  raise ValueError("⚠️ API Key Groq tidak ditemukan! Setel variabel lingkungan 'GROQ_API_KEY'.")
25
 
26
+ # βœ… Inisialisasi API Groq
27
  client = Groq(api_key=GROQ_API_KEY)
28
 
29
  def retrieve_and_generate(query, history=[]):
30
+ """πŸ” Retrieve dokumen & 🧠 Generate jawaban dari LLM."""
31
+
32
+ # πŸ” Ambil 3 dokumen yang paling relevan
33
  docs = vector_store.similarity_search(query, k=3)
34
  context = "\n\n".join([doc.page_content for doc in docs])
35
 
36
+ # 🧠 Generate respons dengan model Groq
37
  response = client.chat.completions.create(
38
  model="mixtral-8x7b-32768",
39
  messages=[
 
44
  max_tokens=200
45
  )
46
 
 
47
  bot_response = response.choices[0].message.content
48
+ history.append((query, bot_response)) # βœ… Simpan chat history
49
+ return history
50
+
51
+ # βœ… UI dengan Gradio
52
+ with gr.Blocks() as demo:
53
+ gr.Markdown("## πŸ€– RoboHome RAG Chatbot")
54
+ gr.Markdown("Chatbot ini menjawab pertanyaan berdasarkan dokumentasi RoboHome.")
55
+
56
+ chatbot = gr.Chatbot(label="πŸ’¬ Jawaban RoboHome")
57
+ input_text = gr.Textbox(label="✏️ Ajukan pertanyaan tentang RoboHome", placeholder="Ketik pertanyaan di sini...")
58
+ send_button = gr.Button("πŸš€ Kirim")
59
+
60
+ def process_input(user_input, history):
61
+ return retrieve_and_generate(user_input, history)
62
+
63
+ send_button.click(process_input, inputs=[input_text, chatbot], outputs=chatbot)
64
 
65
+ demo.launch(share=True)