Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,20 +30,23 @@ kb_collection = None
|
|
| 30 |
# KB Setup Endpoint
|
| 31 |
# -------------------------------
|
| 32 |
@app.post("/setup")
|
| 33 |
-
async def setup_endpoint(
|
| 34 |
global kb_collection
|
| 35 |
|
| 36 |
-
if not
|
| 37 |
-
raise HTTPException(status_code=
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
chroma_client = chromadb.Client(
|
| 45 |
Settings(
|
| 46 |
-
persist_directory="/data/chroma_db",
|
| 47 |
anonymized_telemetry=False
|
| 48 |
)
|
| 49 |
)
|
|
@@ -52,12 +55,14 @@ async def setup_endpoint(req: SetupRequest):
|
|
| 52 |
try:
|
| 53 |
kb_collection = chroma_client.get_collection("kb_collection")
|
| 54 |
except:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# Add KB entries
|
| 63 |
for entry in kb_data["knowledge_base"]:
|
|
@@ -69,14 +74,13 @@ async def setup_endpoint(req: SetupRequest):
|
|
| 69 |
"question_variations": entry.get("question_variations", []),
|
| 70 |
"keywords": entry.get("keywords", [])
|
| 71 |
}],
|
| 72 |
-
ids=[entry["id"]]
|
| 73 |
-
embedding_function=embedding_func
|
| 74 |
)
|
| 75 |
|
| 76 |
-
# Persist
|
| 77 |
kb_collection.persist()
|
| 78 |
|
| 79 |
-
return {"status": "KB embedded and stored successfully"}
|
| 80 |
|
| 81 |
# -------------------------------
|
| 82 |
# Step-by-Step Endpoints
|
|
|
|
| 30 |
# KB Setup Endpoint
|
| 31 |
# -------------------------------
|
| 32 |
@app.post("/setup")
|
| 33 |
+
async def setup_endpoint(kb_file: UploadFile = File(...)):
|
| 34 |
global kb_collection
|
| 35 |
|
| 36 |
+
if not kb_file.filename.endswith(".json"):
|
| 37 |
+
raise HTTPException(status_code=400, detail="Only JSON files are allowed")
|
| 38 |
|
| 39 |
+
# Read uploaded KB.json
|
| 40 |
+
contents = await kb_file.read()
|
| 41 |
+
try:
|
| 42 |
+
kb_data = json.loads(contents)
|
| 43 |
+
except Exception as e:
|
| 44 |
+
raise HTTPException(status_code=400, detail=f"Invalid JSON file: {e}")
|
| 45 |
|
| 46 |
+
# Initialize Chroma client
|
| 47 |
chroma_client = chromadb.Client(
|
| 48 |
Settings(
|
| 49 |
+
persist_directory="/data/chroma_db",
|
| 50 |
anonymized_telemetry=False
|
| 51 |
)
|
| 52 |
)
|
|
|
|
| 55 |
try:
|
| 56 |
kb_collection = chroma_client.get_collection("kb_collection")
|
| 57 |
except:
|
| 58 |
+
# Create collection with embedding function (new API)
|
| 59 |
+
embedding_func = embedding_functions.SentenceTransformerEmbeddingFunction(
|
| 60 |
+
model_name="sentence-transformers/multi-qa-mpnet-base-dot-v1"
|
| 61 |
+
)
|
| 62 |
+
kb_collection = chroma_client.create_collection(
|
| 63 |
+
name="kb_collection",
|
| 64 |
+
embedding_function=embedding_func
|
| 65 |
+
)
|
| 66 |
|
| 67 |
# Add KB entries
|
| 68 |
for entry in kb_data["knowledge_base"]:
|
|
|
|
| 74 |
"question_variations": entry.get("question_variations", []),
|
| 75 |
"keywords": entry.get("keywords", [])
|
| 76 |
}],
|
| 77 |
+
ids=[entry["id"]]
|
|
|
|
| 78 |
)
|
| 79 |
|
| 80 |
+
# Persist collection
|
| 81 |
kb_collection.persist()
|
| 82 |
|
| 83 |
+
return {"status": "KB uploaded, embedded, and stored successfully"}
|
| 84 |
|
| 85 |
# -------------------------------
|
| 86 |
# Step-by-Step Endpoints
|