Pulastya0 commited on
Commit
2b51b85
·
verified ·
1 Parent(s): 9ce9018

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -32,23 +32,20 @@ COLLECTION_NAME = "knowledge_base"
32
  # KB Setup Endpoint
33
  # -------------------------------
34
  @app.post("/setup")
35
- def setup_kb(kb_file: UploadFile = File(...)):
36
  """
37
- Loads a JSON file, generates embeddings, and populates a persistent ChromaDB knowledge base.
38
  Only runs when explicitly called.
39
  """
40
- if not os.path.exists(json_file_path):
41
- raise HTTPException(status_code=404, detail=f"File not found: {json_file_path}")
42
-
43
  try:
44
- # Load JSON data
45
- with open(json_file_path, "r", encoding="utf-8") as f:
46
- data = json.load(f)
47
 
48
  if not isinstance(data, list):
49
  raise HTTPException(status_code=400, detail="JSON must contain a list of knowledge items.")
50
 
51
- print(f"📘 Loaded {len(data)} items from {json_file_path}")
52
 
53
  # Initialize encoder and Chroma client (only here)
54
  encoder = SentenceTransformer("all-MiniLM-L6-v2")
@@ -79,6 +76,8 @@ def setup_kb(kb_file: UploadFile = File(...)):
79
 
80
  return {"message": "Knowledge base successfully initialized.", "count": collection.count()}
81
 
 
 
82
  except Exception as e:
83
  raise HTTPException(status_code=500, detail=f"Setup failed: {e}")
84
 
 
32
  # KB Setup Endpoint
33
  # -------------------------------
34
  @app.post("/setup")
35
+ async def setup_kb(kb_file: UploadFile = File(...)):
36
  """
37
+ Uploads a JSON file, generates embeddings, and populates a persistent ChromaDB knowledge base.
38
  Only runs when explicitly called.
39
  """
 
 
 
40
  try:
41
+ # Read JSON directly from the uploaded file
42
+ content_bytes = await kb_file.read()
43
+ data = json.loads(content_bytes)
44
 
45
  if not isinstance(data, list):
46
  raise HTTPException(status_code=400, detail="JSON must contain a list of knowledge items.")
47
 
48
+ print(f"📘 Loaded {len(data)} items from uploaded file '{kb_file.filename}'")
49
 
50
  # Initialize encoder and Chroma client (only here)
51
  encoder = SentenceTransformer("all-MiniLM-L6-v2")
 
76
 
77
  return {"message": "Knowledge base successfully initialized.", "count": collection.count()}
78
 
79
+ except json.JSONDecodeError:
80
+ raise HTTPException(status_code=400, detail="Invalid JSON file.")
81
  except Exception as e:
82
  raise HTTPException(status_code=500, detail=f"Setup failed: {e}")
83