Spaces:
Runtime error
Runtime error
Commit ·
353025f
1
Parent(s): c840d73
modified: app.py
Browse filesmodified: static/style.css
- app.py +36 -8
- static/style.css +10 -0
app.py
CHANGED
|
@@ -3,7 +3,7 @@ from flask import Flask, request, jsonify, render_template
|
|
| 3 |
from flask_cors import CORS
|
| 4 |
from pymongo import MongoClient, ReturnDocument
|
| 5 |
from pymongo.errors import PyMongoError
|
| 6 |
-
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
from dotenv import load_dotenv
|
|
@@ -32,14 +32,33 @@ print("Loading embedding model (this may take a while)...")
|
|
| 32 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 33 |
print("✅ Embedding model ('all-MiniLM-L6-v2') has been successfully loaded.")
|
| 34 |
|
|
|
|
| 35 |
try:
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
knowledge_collection = chroma_client.get_collection(
|
| 38 |
-
name="website_knowledge"
|
| 39 |
)
|
| 40 |
-
print("✅ Successfully connected to ChromaDB (
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
-
print(f"❌ FAILED to connect to ChromaDB.
|
|
|
|
| 43 |
|
| 44 |
# --- 3. Frontend Endpoints ---
|
| 45 |
@app.route("/")
|
|
@@ -137,14 +156,23 @@ def handle_chat():
|
|
| 137 |
# --- 2. (RAG) - Perform RAG ---
|
| 138 |
print(f"Searching for context for: \"{user_message}\"")
|
| 139 |
query_embedding = embedding_model.encode(user_message).tolist()
|
|
|
|
|
|
|
|
|
|
| 140 |
results = knowledge_collection.query(
|
| 141 |
query_embeddings=[query_embedding],
|
| 142 |
n_results=5
|
| 143 |
)
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
# --- 3. (RAG) Build Prompt (
|
| 148 |
system_prompt = """You are the 'Enviro Education Tools Product Selector & System Designer'. Your answers are for technical professionals who use American English.
|
| 149 |
|
| 150 |
Your task is to answer the user's question *strictly* and *only* based on the context provided. The context provided IS the information from https://enviroeducationtools.com/.
|
|
|
|
| 3 |
from flask_cors import CORS
|
| 4 |
from pymongo import MongoClient, ReturnDocument
|
| 5 |
from pymongo.errors import PyMongoError
|
| 6 |
+
import chromadb # <-- MENGGANTIKAN HttpClient
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
from dotenv import load_dotenv
|
|
|
|
| 32 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 33 |
print("✅ Embedding model ('all-MiniLM-L6-v2') has been successfully loaded.")
|
| 34 |
|
| 35 |
+
# --- KODE BARU UNTUK KONEKSI HOSTING (CloudClient) ---
|
| 36 |
try:
|
| 37 |
+
# 1. Ambil kredensial Cloud Anda dari .env
|
| 38 |
+
CHROMA_API_KEY = os.getenv("CHROMA_API_KEY")
|
| 39 |
+
CHROMA_TENANT = os.getenv("CHROMA_TENANT")
|
| 40 |
+
CHROMA_DATABASE = os.getenv("CHROMA_DATABASE")
|
| 41 |
+
|
| 42 |
+
if not CHROMA_API_KEY or not CHROMA_TENANT or not CHROMA_DATABASE:
|
| 43 |
+
raise ValueError("CHROMA_API_KEY, CHROMA_TENANT, or CHROMA_DATABASE missing from .env file")
|
| 44 |
+
|
| 45 |
+
# 2. Gunakan chromadb.CloudClient
|
| 46 |
+
chroma_client = chromadb.CloudClient(
|
| 47 |
+
tenant=CHROMA_TENANT,
|
| 48 |
+
database=CHROMA_DATABASE,
|
| 49 |
+
api_key=CHROMA_API_KEY
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# 3. Ambil collection (data Anda sudah ada di sana dari 'chroma copy')
|
| 53 |
knowledge_collection = chroma_client.get_collection(
|
| 54 |
+
name="website_knowledge"
|
| 55 |
)
|
| 56 |
+
print(f"✅ Successfully connected to ChromaDB Cloud (Tenant: {CHROMA_TENANT}).")
|
| 57 |
+
print(f"✅ Found {knowledge_collection.count()} documents in 'website_knowledge' collection.")
|
| 58 |
+
|
| 59 |
except Exception as e:
|
| 60 |
+
print(f"❌ FAILED to connect to ChromaDB Cloud. Check your .env variables. Error: {e}")
|
| 61 |
+
# --- AKHIR DARI KODE BARU ---
|
| 62 |
|
| 63 |
# --- 3. Frontend Endpoints ---
|
| 64 |
@app.route("/")
|
|
|
|
| 156 |
# --- 2. (RAG) - Perform RAG ---
|
| 157 |
print(f"Searching for context for: \"{user_message}\"")
|
| 158 |
query_embedding = embedding_model.encode(user_message).tolist()
|
| 159 |
+
|
| 160 |
+
# Catatan: n_results=5 dapat menyebabkan kontaminasi konteks.
|
| 161 |
+
# Pertimbangkan untuk mengganti ke 1 jika jawaban tidak akurat.
|
| 162 |
results = knowledge_collection.query(
|
| 163 |
query_embeddings=[query_embedding],
|
| 164 |
n_results=5
|
| 165 |
)
|
| 166 |
+
|
| 167 |
+
# Tambahkan pemeriksaan jika 'results' kosong
|
| 168 |
+
if not results['documents'] or not results['documents'][0]:
|
| 169 |
+
context = "No relevant context found."
|
| 170 |
+
print("Context not found.")
|
| 171 |
+
else:
|
| 172 |
+
context = "\n\n".join(results['documents'][0])
|
| 173 |
+
print("Context found.")
|
| 174 |
|
| 175 |
+
# --- 3. (RAG) Build Prompt (Per New Instructions) ---
|
| 176 |
system_prompt = """You are the 'Enviro Education Tools Product Selector & System Designer'. Your answers are for technical professionals who use American English.
|
| 177 |
|
| 178 |
Your task is to answer the user's question *strictly* and *only* based on the context provided. The context provided IS the information from https://enviroeducationtools.com/.
|
static/style.css
CHANGED
|
@@ -33,6 +33,16 @@ body:not(.landing-page) {
|
|
| 33 |
height: 100vh;
|
| 34 |
}
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
.landing-container {
|
| 37 |
max-width: 600px;
|
| 38 |
padding: 20px;
|
|
|
|
| 33 |
height: 100vh;
|
| 34 |
}
|
| 35 |
|
| 36 |
+
/* --- Style untuk Landing Page (index.html) --- */
|
| 37 |
+
body.landing-page {
|
| 38 |
+
background-color: #f0f2f5; /* Warna abu-abu yang bersih */
|
| 39 |
+
display: flex;
|
| 40 |
+
align-items: center;
|
| 41 |
+
justify-content: center;
|
| 42 |
+
text-align: center;
|
| 43 |
+
overflow: auto; /* Mengganti overflow:hidden dari body utama */
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
.landing-container {
|
| 47 |
max-width: 600px;
|
| 48 |
padding: 20px;
|