vikramvasudevan commited on
Commit
083e322
·
verified ·
1 Parent(s): 9bf7fc2

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. server.py +26 -7
server.py CHANGED
@@ -3,27 +3,46 @@ import traceback
3
  import uuid
4
  from fastapi import APIRouter, Request
5
  from fastapi.responses import JSONResponse
6
- from gradio_client import Client
7
  from pydantic import BaseModel
8
  from app import chat
9
 
10
  router = APIRouter()
11
 
 
 
 
12
 
13
  class Message(BaseModel):
14
  text: str
15
-
16
 
17
  @router.post("/chat")
18
- async def handle_chat(msg: Message):
19
  try:
20
- thread_id = uuid.uuid4()
 
 
 
 
 
 
 
 
 
 
21
  reply_text = chat(
22
- debug_mode=False, message=msg.text, history=None, thread_id=thread_id
 
 
 
23
  )
24
 
25
- return {"reply": reply_text}
 
26
 
27
  except Exception as e:
28
  traceback.print_exc()
29
- return {"reply": f"Error: {e}"}
 
 
 
 
3
  import uuid
4
  from fastapi import APIRouter, Request
5
  from fastapi.responses import JSONResponse
 
6
  from pydantic import BaseModel
7
  from app import chat
8
 
9
  router = APIRouter()
10
 
11
+ # In-memory mapping from session_id -> thread_id
12
+ # For production, you may want Redis or a DB for persistence
13
+ thread_map = {}
14
 
15
  class Message(BaseModel):
16
  text: str
17
+ session_id: str | None = None # Optional session ID from client
18
 
19
  @router.post("/chat")
20
+ async def handle_chat(msg: Message, request: Request):
21
  try:
22
+ # Use existing session_id if provided, else generate new
23
+ session_id = msg.session_id
24
+ if not session_id:
25
+ session_id = str(uuid.uuid4())
26
+
27
+ # Get or create a persistent thread_id for this session
28
+ if session_id not in thread_map:
29
+ thread_map[session_id] = str(uuid.uuid4())
30
+ thread_id = thread_map[session_id]
31
+
32
+ # Call your graph/chat function
33
  reply_text = chat(
34
+ debug_mode=False,
35
+ message=msg.text,
36
+ history=None,
37
+ thread_id=thread_id
38
  )
39
 
40
+ # Return both reply and session_id to the client
41
+ return {"reply": reply_text, "session_id": session_id}
42
 
43
  except Exception as e:
44
  traceback.print_exc()
45
+ return JSONResponse(
46
+ status_code=500,
47
+ content={"reply": f"Error: {e}"}
48
+ )