Spaces:
Sleeping

PavanKumarD commited on
Commit
bce8fcb
·
verified ·
1 Parent(s): da9aff2

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +23 -28
app/main.py CHANGED
@@ -42,53 +42,48 @@ faiss_index = faiss.read_index(os.path.join(BASE_DIR, "vector_base", "faiss_inde
42
  document_chunks = np.load(os.path.join(BASE_DIR, "vector_base", "chunks.npy"), allow_pickle=True).tolist()
43
 
44
 
 
 
 
 
 
 
45
 
46
  chat_sessions = {}
47
 
48
- @app.get("/")
49
- def root():
50
- return {"status": "live"}
51
-
52
-
53
-
54
  # -------------- Chat Endpoint ----------------
 
 
55
  @app.post("/query")
56
  async def query_endpoint(
57
  request: Request,
58
  query: str = Form(...),
59
- user_data_text: str = Form(""), # ✅ Accept dynamic user data
 
60
  ):
61
- client_ip = request.client.host
62
- chat_history = chat_sessions.get(client_ip, [])
63
-
64
- # This now only uses dynamic user data
65
  combined_user_data = user_data_text.strip()
66
 
 
 
67
 
68
  answer = generate_answer(
69
  query=query,
70
  index=faiss_index,
71
  chunks=document_chunks,
72
  chat_history=chat_history,
73
- user_data_text=combined_user_data # ✅ Send to LLM
74
  )
75
 
 
 
 
76
  chat_history.append({"role": "user", "message": query})
77
  chat_history.append({"role": "bot", "message": answer})
78
- chat_sessions[client_ip] = chat_history
79
- # answer = auto_wrap_math_blocks(answer)
80
- # markdown_safe = protect_math(answer)
81
- # html_response = md(
82
- # answer,
83
- # extensions=[
84
- # 'markdown.extensions.extra',
85
- # 'markdown.extensions.admonition',
86
- # 'markdown.extensions.codehilite',
87
- # 'markdown.extensions.fenced_code',
88
- # 'markdown.extensions.tables',
89
- # 'markdown.extensions.toc'
90
- # ]
91
- # )
92
- # html_response = unprotect_math(html_response)
93
  return JSONResponse(content={"response": answer})
94
-
 
42
  document_chunks = np.load(os.path.join(BASE_DIR, "vector_base", "chunks.npy"), allow_pickle=True).tolist()
43
 
44
 
45
+ @app.post("/logout")
46
+ async def logout_user(session_id: str = Form(...)):
47
+ if session_id in chat_sessions:
48
+ del chat_sessions[session_id]
49
+ print(f"[LOGOUT] Session '{session_id}' flushed.")
50
+ return JSONResponse(content={"message": "Session cleared."})
51
 
52
  chat_sessions = {}
53
 
 
 
 
 
 
 
54
  # -------------- Chat Endpoint ----------------
55
+ import time
56
+
57
  @app.post("/query")
58
  async def query_endpoint(
59
  request: Request,
60
  query: str = Form(...),
61
+ user_data_text: str = Form(""),
62
+ session_id: str = Form(...)
63
  ):
64
+ start_time = time.time()
65
+ # ✅ Use session_id as the key instead of IP
66
+ chat_history = chat_sessions.get(session_id, [])
 
67
  combined_user_data = user_data_text.strip()
68
 
69
+ input_context = "\n".join([c["message"] for c in chat_history]) + combined_user_data
70
+ input_tokens = len(input_context.split())
71
 
72
  answer = generate_answer(
73
  query=query,
74
  index=faiss_index,
75
  chunks=document_chunks,
76
  chat_history=chat_history,
77
+ user_data_text=combined_user_data
78
  )
79
 
80
+ output_tokens = len(answer.split())
81
+ time_taken = round(time.time() - start_time, 2)
82
+
83
  chat_history.append({"role": "user", "message": query})
84
  chat_history.append({"role": "bot", "message": answer})
85
+ chat_sessions[session_id] = chat_history # ✅ Correct usage
86
+
87
+ print(f"[QUERY LOG] Session: {session_id} | Input Tokens: {input_tokens} | Output Tokens: {output_tokens} | Time: {time_taken}s")
88
+
 
 
 
 
 
 
 
 
 
 
 
89
  return JSONResponse(content={"response": answer})