Pulastya0 commited on
Commit
005b7c5
·
verified ·
1 Parent(s): 24f4ff5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -18
app.py CHANGED
@@ -30,20 +30,23 @@ kb_collection = None
30
  # KB Setup Endpoint
31
  # -------------------------------
32
  @app.post("/setup")
33
- async def setup_endpoint(req: SetupRequest):
34
  global kb_collection
35
 
36
- if not os.path.exists(req.kb_file):
37
- raise HTTPException(status_code=404, detail="KB.json file not found")
38
 
39
- # Load KB.json
40
- with open(req.kb_file, "r") as f:
41
- kb_data = json.load(f)
 
 
 
42
 
43
- # Create Chroma client (new API)
44
  chroma_client = chromadb.Client(
45
  Settings(
46
- persist_directory="/data/chroma_db", # make sure this dir exists and writable
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
- kb_collection = chroma_client.create_collection("kb_collection")
56
-
57
- # Embedding function
58
- embedding_func = embedding_functions.SentenceTransformerEmbeddingFunction(
59
- model_name="sentence-transformers/multi-qa-mpnet-base-dot-v1"
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 to disk
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