Spaces:
Sleeping
Sleeping
Upload main.py
Browse files
main.py
CHANGED
|
@@ -18,7 +18,10 @@ app = FastAPI(title="Medical Policy RAG Chatbot API")
|
|
| 18 |
# --------------------------
|
| 19 |
|
| 20 |
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")
|
| 21 |
-
redis_client = redis.from_url(REDIS_URL)
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
INDEX_PATH = os.getenv("FAISS_INDEX_PATH", "data/faiss.index")
|
| 24 |
METADATA_PATH = os.getenv("METADATA_PATH", "data/metadata.pkl")
|
|
@@ -84,16 +87,27 @@ def generate_answer(prompt: str) -> str:
|
|
| 84 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 85 |
|
| 86 |
def store_pending(session_id: str, data: dict):
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
def get_pending(session_id: str) -> Optional[dict]:
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
if raw:
|
| 92 |
return json.loads(raw)
|
| 93 |
return None
|
| 94 |
|
| 95 |
def clear_pending(session_id: str):
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
# --------------------------
|
| 99 |
# Core endpoint
|
|
|
|
| 18 |
# --------------------------
|
| 19 |
|
| 20 |
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")
|
| 21 |
+
redis_client = redis.from_url(REDIS_URL, socket_connect_timeout=1)
|
| 22 |
+
|
| 23 |
+
# Local fallback for session data if Redis is unavailable
|
| 24 |
+
local_cache = {}
|
| 25 |
|
| 26 |
INDEX_PATH = os.getenv("FAISS_INDEX_PATH", "data/faiss.index")
|
| 27 |
METADATA_PATH = os.getenv("METADATA_PATH", "data/metadata.pkl")
|
|
|
|
| 87 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 88 |
|
| 89 |
def store_pending(session_id: str, data: dict):
|
| 90 |
+
try:
|
| 91 |
+
redis_client.setex(f"session:{session_id}:pending", 600, json.dumps(data))
|
| 92 |
+
except Exception:
|
| 93 |
+
local_cache[f"session:{session_id}:pending"] = json.dumps(data)
|
| 94 |
|
| 95 |
def get_pending(session_id: str) -> Optional[dict]:
|
| 96 |
+
try:
|
| 97 |
+
raw = redis_client.get(f"session:{session_id}:pending")
|
| 98 |
+
except Exception:
|
| 99 |
+
raw = local_cache.get(f"session:{session_id}:pending")
|
| 100 |
+
|
| 101 |
if raw:
|
| 102 |
return json.loads(raw)
|
| 103 |
return None
|
| 104 |
|
| 105 |
def clear_pending(session_id: str):
|
| 106 |
+
try:
|
| 107 |
+
redis_client.delete(f"session:{session_id}:pending")
|
| 108 |
+
except Exception:
|
| 109 |
+
pass
|
| 110 |
+
local_cache.pop(f"session:{session_id}:pending", None)
|
| 111 |
|
| 112 |
# --------------------------
|
| 113 |
# Core endpoint
|