Spaces:
Sleeping
Sleeping
File size: 8,750 Bytes
682caaf 6ed180c a745a50 a213db3 a745a50 a213db3 682caaf ed623a2 682caaf 6ed180c 682caaf 6ed180c a213db3 6ed180c a213db3 6ed180c a213db3 6ed180c a213db3 6ed180c a745a50 6ed180c a745a50 682caaf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
from fastapi import APIRouter
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'
)
logger = logging.getLogger(__name__)
def get_router():
router = APIRouter()
# Import sub-modules from the routes directory
from .routes.auth import auth
from .routes.patients import patients
from .routes.pdf import pdf
from .routes.appointments import router as appointments
from .routes.messaging import router as messaging
# from .routes.txagent import router as txagent, get_txagent, ChatRequest # Commented out - using separate TxAgent API
from fastapi import Depends, HTTPException, UploadFile, File
from core.security import get_current_user
import asyncio
import io
from datetime import datetime
from fastapi.responses import StreamingResponse
# Include sub-routers with correct prefixes
router.include_router(patients, tags=["patients"]) # Remove prefix since routes already have /patients
router.include_router(auth, prefix="/auth", tags=["auth"])
router.include_router(pdf, prefix="/patients", tags=["pdf"]) # Keep prefix for PDF routes
router.include_router(appointments, prefix="/appointments", tags=["appointments"])
router.include_router(messaging, tags=["messaging"])
# router.include_router(txagent, tags=["txagent"]) # Commented out - using separate TxAgent API
# TxAgent endpoints commented out - using separate TxAgent API
# @router.post("/chat-stream")
# async def chat_stream(
# request: ChatRequest,
# current_user: dict = Depends(get_current_user)
# ):
# """Streaming chat endpoint for TxAgent"""
# import logging
# logger = logging.getLogger(__name__)
#
# try:
# if not any(role in current_user.get('roles', []) for role in ['doctor', 'admin']):
# raise HTTPException(status_code=403, detail="Only doctors and admins can use TxAgent")
# # Get TxAgent instance
# txagent = get_txagent()
#
# if txagent is None:
# # Fallback response if TxAgent is not available
# response_text = f"TxAgent is currently unavailable. Please try again later. Your message was: {request.message}"
# else:
# # Get real response from TxAgent
# try:
# response_text = txagent.chat(
# message=request.message,
# temperature=request.temperature,
# max_new_tokens=request.max_new_tokens
# )
# except Exception as e:
# logger.error(f"TxAgent chat failed: {e}")
# response_text = f"Sorry, I encountered an error processing your request: {request.message}. Please try again."
# # Return streaming text without data: prefix
# async def generate():
# # Send the complete response as plain text
# yield response_text
# return StreamingResponse(
# generate(),
# media_type="text/plain",
# headers={
# "Cache-Control": "no-cache",
# "Connection": "keep-alive"
# }
# )
# except Exception as e:
# logger.error(f"Error in chat stream: {e}")
# raise HTTPException(status_code=500, detail="Failed to process chat stream")
# @router.post("/voice/synthesize")
# async def voice_synthesize_root(
# request: dict,
# current_user: dict = Depends(get_current_user)
# ):
# """Voice synthesis endpoint at root level for frontend compatibility"""
# try:
# if not any(role in current_user.get('roles', []) for role in ['doctor', 'admin']):
# raise HTTPException(status_code=403, detail="Only doctors and admins can use voice features")
#
# logger.info(f"Voice synthesis initiated by {current_user['email']}")
#
# # Extract parameters from request
# text = request.get('text', '')
# language = request.get('language', 'en-US')
# return_format = request.get('return_format', 'mp3')
#
# if not text:
# raise HTTPException(status_code=400, detail="Text is required")
#
# # Import voice function
# try:
# from voice import text_to_speech
# except ImportError:
# # Fallback if voice module is not available
# raise HTTPException(status_code=500, detail="Voice synthesis not available")
#
# # Convert language code for gTTS (e.g., 'en-US' -> 'en')
# language_code = language.split('-')[0] if '-' in language else language
#
# # Generate speech
# audio_data = text_to_speech(text, language=language_code)
#
# # Return audio data
# return StreamingResponse(
# io.BytesIO(audio_data),
# media_type=f"audio/{return_format}",
# headers={"Content-Disposition": f"attachment; filename=speech.{return_format}"}
# )
#
# except HTTPException:
# raise
# except Exception as e:
# logger.error(f"Error in voice synthesis: {e}")
# raise HTTPException(status_code=500, detail="Error generating voice output")
# @router.post("/analyze-report")
# async def analyze_report_root(
# file: UploadFile = File(...),
# current_user: dict = Depends(get_current_user)
# ):
# """Analyze uploaded report (PDF, DOCX, etc.) at root level"""
# try:
# if not any(role in current_user.get('roles', []) for role in ['doctor', 'admin']):
# raise HTTPException(status_code=403, detail="Only doctors and admins can analyze reports")
#
# logger.info(f"Report analysis initiated by {current_user['email']}")
#
# # Read file content
# file_content = await file.read()
# file_extension = file.filename.split('.')[-1].lower()
#
# # Extract text based on file type
# if file_extension == 'pdf':
# try:
# from voice import extract_text_from_pdf
# text_content = extract_text_from_pdf(file_content)
# except Exception as e:
# logger.error(f"PDF text extraction failed: {e}")
# text_content = "Failed to extract text from PDF"
# elif file_extension in ['docx', 'doc']:
# try:
# from docx import Document
# if Document:
# doc = Document(io.BytesIO(file_content))
# text_content = '\n'.join([paragraph.text for paragraph in doc.paragraphs])
# else:
# text_content = "Document processing not available"
# except Exception as e:
# logger.error(f"DOCX text extraction failed: {e}")
# text_content = "Failed to extract text from document"
# else:
# text_content = "Unsupported file format"
#
# # Analyze the content (for now, return a simple analysis)
# analysis_result = {
# "file_name": file.filename,
# "file_type": file_extension,
# "extracted_text": text_content[:500] + "..." if len(text_content) > 500 else text_content,
# "analysis": {
# "summary": f"Analyzed {file.filename} containing {len(text_content)} characters",
# "key_findings": ["Sample finding 1", "Sample finding 2"],
# "recommendations": ["Sample recommendation 1", "Sample recommendation 2"]
# },
# "timestamp": datetime.utcnow().isoformat()
# }
#
# return analysis_result
#
# except Exception as e:
# logger.error(f"Error analyzing report: {e}")
# raise HTTPException(status_code=500, detail="Error analyzing report")
return router
# Export the router
api_router = get_router() |