Spaces:
Runtime error
Runtime error
Update endpoints.py
Browse files- endpoints.py +26 -5
endpoints.py
CHANGED
|
@@ -3,17 +3,38 @@ from fastapi.responses import StreamingResponse, JSONResponse
|
|
| 3 |
from fastapi.encoders import jsonable_encoder
|
| 4 |
from typing import Optional
|
| 5 |
from models import ChatRequest, VoiceOutputRequest, RiskLevel
|
| 6 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from utils import clean_text_response
|
| 8 |
from analysis import analyze_patient_report
|
| 9 |
from voice import recognize_speech, text_to_speech, extract_text_from_pdf
|
| 10 |
from docx import Document
|
| 11 |
import re
|
| 12 |
import io
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def create_router(agent, logger, patients_collection, analysis_collection, users_collection, notifications_collection):
|
| 19 |
router = APIRouter()
|
|
|
|
| 3 |
from fastapi.encoders import jsonable_encoder
|
| 4 |
from typing import Optional
|
| 5 |
from models import ChatRequest, VoiceOutputRequest, RiskLevel
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from bson import ObjectId
|
| 8 |
+
import asyncio
|
| 9 |
+
from config import notifications_collection, send_push_notification, decode_access_token, AUTH_SPACE_URL, logger
|
| 10 |
+
import httpx
|
| 11 |
from utils import clean_text_response
|
| 12 |
from analysis import analyze_patient_report
|
| 13 |
from voice import recognize_speech, text_to_speech, extract_text_from_pdf
|
| 14 |
from docx import Document
|
| 15 |
import re
|
| 16 |
import io
|
| 17 |
+
|
| 18 |
+
async def get_current_user(token: str = Depends(lambda: None)):
|
| 19 |
+
if not token:
|
| 20 |
+
raise HTTPException(status_code=401, detail="Authorization token required")
|
| 21 |
+
if token.startswith("Bearer "):
|
| 22 |
+
token = token.split(" ")[1]
|
| 23 |
+
payload = decode_access_token(token)
|
| 24 |
+
if not payload:
|
| 25 |
+
raise HTTPException(status_code=401, detail="Invalid token")
|
| 26 |
+
email = payload.get("sub")
|
| 27 |
+
if not email:
|
| 28 |
+
raise HTTPException(status_code=401, detail="Invalid token payload")
|
| 29 |
+
|
| 30 |
+
# Fetch user from auth Space
|
| 31 |
+
async with httpx.AsyncClient() as client:
|
| 32 |
+
headers = {"Authorization": f"Bearer {token}"}
|
| 33 |
+
response = await client.get(f"{AUTH_SPACE_URL}/me", headers=headers)
|
| 34 |
+
if response.status_code != 200:
|
| 35 |
+
raise HTTPException(status_code=response.status_code, detail="Could not validate credentials")
|
| 36 |
+
user = response.json()
|
| 37 |
+
return user
|
| 38 |
|
| 39 |
def create_router(agent, logger, patients_collection, analysis_collection, users_collection, notifications_collection):
|
| 40 |
router = APIRouter()
|