File size: 23,799 Bytes
f275c80
f126604
d377221
f126604
7e095f4
ab172ce
fa9b390
069d7f4
 
60e4c3d
94662cd
 
f275c80
069d7f4
f126604
f275c80
 
7757822
ab172ce
e3305a8
069d7f4
 
 
ac9926b
3069ccd
f275c80
 
dfff005
ab172ce
f275c80
f0898a3
ab172ce
 
dfff005
f0898a3
f275c80
f126604
f275c80
f126604
 
ac9926b
 
 
 
f126604
 
f275c80
 
 
 
 
 
 
069d7f4
5620229
 
 
 
 
60e4c3d
5620229
069d7f4
 
 
 
 
 
 
 
f275c80
069d7f4
94662cd
 
 
 
 
 
 
 
ab172ce
 
 
 
94662cd
ab172ce
f275c80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab172ce
5620229
 
ab172ce
dfff005
 
 
f0898a3
6c1d81c
f0898a3
ab172ce
6c1d81c
dfff005
5620229
 
8dff938
 
94662cd
 
 
 
8dff938
 
 
 
 
 
 
 
 
f275c80
60e4c3d
8dff938
 
 
 
 
 
 
 
60e4c3d
94662cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac9926b
94662cd
 
 
 
 
 
 
 
 
 
 
 
 
ac9926b
94662cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab172ce
 
 
 
 
dfff005
ac9926b
 
fa9b390
 
3069ccd
 
 
ac9926b
 
 
 
 
 
 
 
 
 
 
3069ccd
f275c80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac9926b
f275c80
 
 
 
 
 
 
ac9926b
f275c80
 
 
 
 
 
ac9926b
f275c80
 
 
 
 
 
 
 
 
 
ac9926b
f275c80
 
 
 
 
ac9926b
f275c80
 
ac9926b
f275c80
 
ac9926b
 
 
 
 
 
 
ab172ce
60e4c3d
f0898a3
fa9b390
 
 
 
 
 
 
ac9926b
f0898a3
fa9b390
dfff005
ab172ce
 
f0898a3
94662cd
ab172ce
 
 
dfff005
 
 
 
94662cd
 
 
 
 
 
 
 
ab172ce
3069ccd
fa9b390
ab172ce
 
94662cd
fa9b390
ac9926b
ab172ce
94662cd
ab172ce
3069ccd
ab172ce
dfff005
 
94662cd
 
fa9b390
94662cd
fa9b390
94662cd
60e4c3d
dfff005
60e4c3d
069d7f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f126604
 
94662cd
ab172ce
dfff005
 
 
 
ab172ce
dfff005
ab172ce
 
 
dfff005
 
ab172ce
dfff005
 
ab172ce
 
f0898a3
f275c80
 
f0898a3
 
fa9b390
ab172ce
f0898a3
ab172ce
 
f275c80
ab172ce
f275c80
 
ab172ce
 
94662cd
3069ccd
ac9926b
ab172ce
bdcc052
e3305a8
f275c80
 
 
 
 
e3305a8
 
 
 
 
 
 
 
 
 
 
 
 
3069ccd
e3305a8
 
 
 
 
 
 
 
 
 
 
ea3d9f9
f275c80
 
 
 
 
ab172ce
ea3d9f9
ab172ce
ea3d9f9
ab172ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfff005
ea3d9f9
 
 
ab172ce
 
f126604
e456a0b
069d7f4
 
 
 
f275c80
 
069d7f4
f275c80
069d7f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f275c80
 
 
 
 
069d7f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f275c80
 
069d7f4
f275c80
069d7f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac9926b
 
9095ee0
 
0f83542
 
9095ee0
f275c80
 
ac9926b
f275c80
ac9926b
0f83542
 
 
 
 
 
64888a0
0f83542
 
 
 
 
 
 
64888a0
0f83542
 
 
 
 
 
 
1e0df14
 
64888a0
0f83542
 
 
 
 
 
 
 
 
 
 
 
9095ee0
0f83542
1e0df14
 
 
 
 
64888a0
0f83542
 
 
 
 
64888a0
0f83542
ac9926b
 
 
0f83542
 
 
 
 
1e0df14
ac9926b
 
 
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# app.py (in TxAgent-API)
import os
import sys
import json
import logging
import re
import hashlib
import io
import base64
from datetime import datetime
from typing import List, Dict, Optional, Tuple
from enum import Enum
from fastapi import FastAPI, HTTPException, UploadFile, File, Query, Form, Depends
from fastapi.responses import StreamingResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import OAuth2PasswordBearer
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
import asyncio
from bson import ObjectId
import speech_recognition as sr
from gtts import gTTS
from pydub import AudioSegment
import PyPDF2
import mimetypes
from docx import Document
from jose import JWTError, jwt
from txagent.txagent import TxAgent
from db.mongo import get_mongo_client

# Logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger("TxAgentAPI")

# App
app = FastAPI(title="TxAgent API", version="2.6.0")

# CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"]
)

# JWT settings (must match CPS-API)
SECRET_KEY = os.getenv("SECRET_KEY", "your-secret-key")  # Same as CPS-API
ALGORITHM = "HS256"

# OAuth2 scheme (point to CPS-API's login endpoint)
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="https://rocketfarmstudios-cps-api.hf.space/auth/login")

# Pydantic Models
class ChatRequest(BaseModel):
    message: str
    temperature: float = 0.7
    max_new_tokens: int = 512
    history: Optional[List[Dict]] = None
    format: Optional[str] = "clean"

class VoiceInputRequest(BaseModel):
    audio_format: str = "wav"
    language: str = "en-US"

class VoiceOutputRequest(BaseModel):
    text: str
    language: str = "en"
    slow: bool = False
    return_format: str = "mp3"

# Enums
class RiskLevel(str, Enum):
    NONE = "none"
    LOW = "low"
    MODERATE = "moderate"
    HIGH = "high"
    SEVERE = "severe"

# Globals
agent = None
patients_collection = None
analysis_collection = None
alerts_collection = None

# JWT validation
async def get_current_user(token: str = Depends(oauth2_scheme)):
    credentials_exception = HTTPException(
        status_code=401,
        detail="Could not validate credentials",
        headers={"WWW-Authenticate": "Bearer"},
    )
    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        email: str = payload.get("sub")
        if email is None:
            raise credentials_exception
    except JWTError:
        raise credentials_exception
    user = await users_collection.find_one({"email": email})
    if user is None:
        raise credentials_exception
    return user

# Helper functions (unchanged from your original code)
def clean_text_response(text: str) -> str:
    text = re.sub(r'\n\s*\n', '\n\n', text)
    text = re.sub(r'[ ]+', ' ', text)
    return text.replace("**", "").replace("__", "").strip()

def extract_section(text: str, heading: str) -> str:
    try:
        pattern = rf"{re.escape(heading)}:\s*\n(.*?)(?=\n[A-Z][^\n]*:|\Z)"
        match = re.search(pattern, text, re.DOTALL | re.IGNORECASE)
        return match.group(1).strip() if match else ""
    except Exception as e:
        logger.error(f"Section extraction failed for heading '{heading}': {e}")
        return ""

def structure_medical_response(text: str) -> Dict:
    def extract_improved(text: str, heading: str) -> str:
        patterns = [
            rf"{re.escape(heading)}:\s*\n(.*?)(?=\n\s*\n|\Z)",
            rf"\*\*{re.escape(heading)}\*\*:\s*\n(.*?)(?=\n\s*\n|\Z)",
            rf"{re.escape(heading)}[\s\-]+(.*?)(?=\n\s*\n|\Z)",
            rf"\n{re.escape(heading)}\s*\n(.*?)(?=\n\s*\n|\Z)"
        ]
        for pattern in patterns:
            match = re.search(pattern, text, re.DOTALL | re.IGNORECASE)
            if match:
                content = match.group(1).strip()
                content = re.sub(r'^\s*[\-\*]\s*', '', content, flags=re.MULTILINE)
                return content
        return ""
    
    text = text.replace('**', '').replace('__', '')
    return {
        "summary": extract_improved(text, "Summary of Patient's Medical History") or 
                  extract_improved(text, "Summarize the patient's medical history"),
        "risks": extract_improved(text, "Identify Risks or Red Flags") or 
                extract_improved(text, "Risks or Red Flags"),
        "missed_issues": extract_improved(text, "Missed Diagnoses or Treatments") or 
                       extract_improved(text, "What the doctor might have missed"),
        "recommendations": extract_improved(text, "Suggest Next Clinical Steps") or 
                         extract_improved(text, "Suggested Clinical Actions")
    }

def detect_suicide_risk(text: str) -> Tuple[RiskLevel, float, List[str]]:
    suicide_keywords = [
        'suicide', 'suicidal', 'kill myself', 'end my life', 
        'want to die', 'self-harm', 'self harm', 'hopeless',
        'no reason to live', 'plan to die'
    ]
    explicit_mentions = [kw for kw in suicide_keywords if kw in text.lower()]
    if not explicit_mentions:
        return RiskLevel.NONE, 0.0, []
    
    assessment_prompt = (
        "Assess the suicide risk level based on this text. "
        "Consider frequency, specificity, and severity of statements. "
        "Respond with JSON format: {\"risk_level\": \"low/moderate/high/severe\", "
        "\"risk_score\": 0-1, \"factors\": [\"list of risk factors\"]}\n\n"
        f"Text to assess:\n{text}"
    )
    
    try:
        response = agent.chat(
            message=assessment_prompt,
            history=[],
            temperature=0.2,
            max_new_tokens=256
        )
        json_match = re.search(r'\{.*\}', response, re.DOTALL)
        if json_match:
            assessment = json.loads(json_match.group())
            return (
                RiskLevel(assessment.get("risk_level", "none").lower()),
                float(assessment.get("risk_score", 0)),
                assessment.get("factors", [])
            )
    except Exception as e:
        logger.error(f"Error in suicide risk assessment: {e}")
    
    risk_score = min(0.1 * len(explicit_mentions), 0.9)
    if risk_score > 0.7:
        return RiskLevel.HIGH, risk_score, explicit_mentions
    elif risk_score > 0.4:
        return RiskLevel.MODERATE, risk_score, explicit_mentions
    return RiskLevel.LOW, risk_score, explicit_mentions

async def create_alert(patient_id: str, risk_data: dict):
    alert_doc = {
        "patient_id": patient_id,
        "type": "suicide_risk",
        "level": risk_data["level"],
        "score": risk_data["score"],
        "factors": risk_data["factors"],
        "timestamp": datetime.utcnow(),
        "acknowledged": False
    }
    await alerts_collection.insert_one(alert_doc)
    logger.warning(f"⚠️ Created suicide risk alert for patient {patient_id}")

def serialize_patient(patient: dict) -> dict:
    patient_copy = patient.copy()
    if "_id" in patient_copy:
        patient_copy["_id"] = str(patient_copy["_id"])
    return patient_copy

def compute_patient_data_hash(data: dict) -> str:
    serialized = json.dumps(data, sort_keys=True)
    return hashlib.sha256(serialized.encode()).hexdigest()

def compute_file_content_hash(file_content: bytes) -> str:
    return hashlib.sha256(file_content).hexdigest()

def extract_text_from_pdf(pdf_data: bytes) -> str:
    try:
        pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_data))
        text = ""
        for page in pdf_reader.pages:
            text += page.extract_text() or ""
        return clean_text_response(text)
    except Exception as e:
        logger.error(f"Error extracting text from PDF: {e}")
        raise HTTPException(status_code=400, detail="Failed to extract text from PDF")

async def analyze_patient_report(patient_id: Optional[str], report_content: str, file_type: str, file_content: bytes):
    identifier = patient_id if patient_id else compute_file_content_hash(file_content)
    report_data = {"identifier": identifier, "content": report_content, "file_type": file_type}
    report_hash = compute_patient_data_hash(report_data)
    logger.info(f"🧾 Analyzing report for identifier: {identifier}")

    existing_analysis = await analysis_collection.find_one({"identifier": identifier, "report_hash": report_hash})
    if existing_analysis:
        logger.info(f"✅ No changes in report data for {identifier}, skipping analysis")
        return existing_analysis

    prompt = (
        "You are a clinical decision support AI. Analyze the following patient report:\n"
        "1. Summarize the patient's medical history.\n"
        "2. Identify risks or red flags (including mental health and suicide risk).\n"
        "3. Highlight missed diagnoses or treatments.\n"
        "4. Suggest next clinical steps.\n"
        f"\nPatient Report ({file_type}):\n{'-'*40}\n{report_content[:10000]}"
    )

    raw_response = agent.chat(
        message=prompt,
        history=[],
        temperature=0.7,
        max_new_tokens=1024
    )
    structured_response = structure_medical_response(raw_response)

    risk_level, risk_score, risk_factors = detect_suicide_risk(raw_response)
    suicide_risk = {
        "level": risk_level.value,
        "score": risk_score,
        "factors": risk_factors
    }

    analysis_doc = {
        "identifier": identifier,
        "patient_id": patient_id,
        "timestamp": datetime.utcnow(),
        "summary": structured_response,
        "suicide_risk": suicide_risk,
        "raw": raw_response,
        "report_hash": report_hash,
        "file_type": file_type
    }

    await analysis_collection.update_one(
        {"identifier": identifier, "report_hash": report_hash},
        {"$set": analysis_doc},
        upsert=True
    )

    if patient_id and risk_level in [RiskLevel.MODERATE, RiskLevel.HIGH, RiskLevel.SEVERE]:
        await create_alert(patient_id, suicide_risk)

    logger.info(f"✅ Stored analysis for identifier {identifier}")
    return analysis_doc

async def analyze_all_patients():
    patients = await patients_collection.find({}).to_list(length=None)
    for patient in patients:
        await analyze_patient(patient)
        await asyncio.sleep(0.1)

async def analyze_patient(patient: dict):
    try:
        serialized = serialize_patient(patient)
        patient_id = serialized.get("fhir_id")
        patient_hash = compute_patient_data_hash(serialized)
        logger.info(f"🧾 Analyzing patient: {patient_id}")

        existing_analysis = await analysis_collection.find_one({"patient_id": patient_id})
        if existing_analysis and existing_analysis.get("data_hash") == patient_hash:
            logger.info(f"✅ No changes in patient data for {patient_id}, skipping analysis")
            return

        doc = json.dumps(serialized, indent=2)
        message = (
            "You are a clinical decision support AI.\n\n"
            "Given the patient document below:\n"
            "1. Summarize the patient's medical history.\n"
            "2. Identify risks or red flags (including mental health and suicide risk).\n"
            "3. Highlight missed diagnoses or treatments.\n"
            "4. Suggest next clinical steps.\n"
            f"\nPatient Document:\n{'-'*40}\n{doc[:10000]}"
        )

        raw = agent.chat(message=message, history=[], temperature=0.7, max_new_tokens=1024)
        structured = structure_medical_response(raw)
        
        risk_level, risk_score, risk_factors = detect_suicide_risk(raw)
        suicide_risk = {
            "level": risk_level.value,
            "score": risk_score,
            "factors": risk_factors
        }
        
        analysis_doc = {
            "identifier": patient_id,
            "patient_id": patient_id,
            "timestamp": datetime.utcnow(),
            "summary": structured,
            "suicide_risk": suicide_risk,
            "raw": raw,
            "data_hash": patient_hash
        }
        
        await analysis_collection.update_one(
            {"identifier": patient_id},
            {"$set": analysis_doc},
            upsert=True
        )
        
        if risk_level in [RiskLevel.MODERATE, RiskLevel.HIGH, RiskLevel.SEVERE]:
            await create_alert(patient_id, suicide_risk)
            
        logger.info(f"✅ Stored analysis for patient {patient_id}")

    except Exception as e:
        logger.error(f"Error analyzing patient: {e}")

def recognize_speech(audio_data: bytes, language: str = "en-US") -> str:
    recognizer = sr.Recognizer()
    try:
        with io.BytesIO(audio_data) as audio_file:
            with sr.AudioFile(audio_file) as source:
                audio = recognizer.record(source)
                text = recognizer.recognize_google(audio, language=language)
                return text
    except sr.UnknownValueError:
        logger.error("Google Speech Recognition could not understand audio")
        raise HTTPException(status_code=400, detail="Could not understand audio")
    except sr.RequestError as e:
        logger.error(f"Could not request results from Google Speech Recognition service; {e}")
        raise HTTPException(status_code=503, detail="Speech recognition service unavailable")
    except Exception as e:
        logger.error(f"Error in speech recognition: {e}")
        raise HTTPException(status_code=500, detail="Error processing speech")

def text_to_speech(text: str, language: str = "en", slow: bool = False) -> bytes:
    try:
        tts = gTTS(text=text, lang=language, slow=slow)
        mp3_fp = io.BytesIO()
        tts.write_to_fp(mp3_fp)
        mp3_fp.seek(0)
        return mp3_fp.read()
    except Exception as e:
        logger.error(f"Error in text-to-speech conversion: {e}")
        raise HTTPException(status_code=500, detail="Error generating speech")

@app.on_event("startup")
async def startup_event():
    global agent, patients_collection, analysis_collection, alerts_collection

    agent = TxAgent(
        model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
        rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
        enable_finish=True,
        enable_rag=False,
        force_finish=True,
        enable_checker=True,
        step_rag_num=4,
        seed=42
    )
    agent.chat_prompt = (
        "You are a clinical assistant AI. Analyze the patient's data and provide clear clinical recommendations."
    )
    agent.init_model()
    logger.info("✅ TxAgent initialized")

    db = get_mongo_client()["cps_db"]
    global users_collection  # Add this to access users_collection for authentication
    users_collection = db["users"]
    patients_collection = db["patients"]
    analysis_collection = db["patient_analysis_results"]
    alerts_collection = db["clinical_alerts"]
    logger.info("📡 Connected to MongoDB")

    asyncio.create_task(analyze_all_patients())

# Protected Endpoints (add Depends(get_current_user) to all endpoints)
@app.get("/status")
async def status(current_user: dict = Depends(get_current_user)):
    logger.info(f"Status endpoint accessed by {current_user['email']}")
    return {
        "status": "running",
        "timestamp": datetime.utcnow().isoformat(),
        "version": "2.6.0",
        "features": ["chat", "voice-input", "voice-output", "patient-analysis", "report-upload"]
    }

@app.get("/patients/analysis-results")
async def get_patient_analysis_results(
    name: Optional[str] = Query(None),
    current_user: dict = Depends(get_current_user)
):
    logger.info(f"Fetching analysis results by {current_user['email']}")
    try:
        query = {}
        if name:
            name_regex = re.compile(name, re.IGNORECASE)
            matching_patients = await patients_collection.find({"full_name": name_regex}).to_list(length=None)
            patient_ids = [p["fhir_id"] for p in matching_patients if "fhir_id" in p]
            if not patient_ids:
                return []
            query = {"patient_id": {"$in": patient_ids}}

        analyses = await analysis_collection.find(query).sort("timestamp", -1).to_list(length=100)
        enriched_results = []
        for analysis in analyses:
            patient = await patients_collection.find_one({"fhir_id": analysis.get("patient_id")})
            if patient:
                analysis["full_name"] = patient.get("full_name", "Unknown")
            analysis["_id"] = str(analysis["_id"])
            enriched_results.append(analysis)

        return enriched_results

    except Exception as e:
        logger.error(f"Error fetching analysis results: {e}")
        raise HTTPException(status_code=500, detail="Failed to retrieve analysis results")

@app.post("/chat-stream")
async def chat_stream_endpoint(
    request: ChatRequest,
    current_user: dict = Depends(get_current_user)
):
    logger.info(f"Chat stream initiated by {current_user['email']}")
    async def token_stream():
        try:
            conversation = [{"role": "system", "content": agent.chat_prompt}]
            if request.history:
                conversation.extend(request.history)
            conversation.append({"role": "user", "content": request.message})

            input_ids = agent.tokenizer.apply_chat_template(
                conversation, add_generation_prompt=True, return_tensors="pt"
            ).to(agent.device)

            output = agent.model.generate(
                input_ids,
                do_sample=True,
                temperature=request.temperature,
                max_new_tokens=request.max_new_tokens,
                pad_token_id=agent.tokenizer.eos_token_id,
                return_dict_in_generate=True
            )

            text = agent.tokenizer.decode(output["sequences"][0][input_ids.shape[1]:], skip_special_tokens=True)
            for chunk in text.split():
                yield chunk + " "
                await asyncio.sleep(0.05)
        except Exception as e:
            logger.error(f"Streaming error: {e}")
            yield f"⚠️ Error: {e}"

    return StreamingResponse(token_stream(), media_type="text/plain")

@app.post("/voice/transcribe")
async def transcribe_voice(
    audio: UploadFile = File(...),
    language: str = Query("en-US", description="Language code for speech recognition"),
    current_user: dict = Depends(get_current_user)
):
    logger.info(f"Voice transcription initiated by {current_user['email']}")
    try:
        audio_data = await audio.read()
        if not audio.filename.lower().endswith(('.wav', '.mp3', '.ogg', '.flac')):
            raise HTTPException(status_code=400, detail="Unsupported audio format")
        
        text = recognize_speech(audio_data, language)
        return {"text": text}
    
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error in voice transcription: {e}")
        raise HTTPException(status_code=500, detail="Error processing voice input")

@app.post("/voice/synthesize")
async def synthesize_voice(
    request: VoiceOutputRequest,
    current_user: dict = Depends(get_current_user)
):
    logger.info(f"Voice synthesis initiated by {current_user['email']}")
    try:
        audio_data = text_to_speech(request.text, request.language, request.slow)
        
        if request.return_format == "base64":
            return {"audio": base64.b64encode(audio_data).decode('utf-8')}
        else:
            return StreamingResponse(
                io.BytesIO(audio_data),
                media_type="audio/mpeg",
                headers={"Content-Disposition": "attachment; filename=speech.mp3"}
            )
    
    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")

@app.post("/voice/chat")
async def voice_chat_endpoint(
    audio: UploadFile = File(...),
    language: str = Query("en-US", description="Language code for speech recognition"),
    temperature: float = Query(0.7, ge=0.1, le=1.0),
    max_new_tokens: int = Query(512, ge=50, le=1024),
    current_user: dict = Depends(get_current_user)
):
    logger.info(f"Voice chat initiated by {current_user['email']}")
    try:
        audio_data = await audio.read()
        user_message = recognize_speech(audio_data, language)
        
        chat_response = agent.chat(
            message=user_message,
            history=[],
            temperature=temperature,
            max_new_tokens=max_new_tokens
        )
        
        audio_data = text_to_speech(chat_response, language.split('-')[0])
        
        return StreamingResponse(
            io.BytesIO(audio_data),
            media_type="audio/mpeg",
            headers={"Content-Disposition": "attachment; filename=response.mp3"}
        )
    
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error in voice chat: {e}")
        raise HTTPException(status_code=500, detail="Error processing voice chat")

@app.post("/analyze-report")
async def analyze_clinical_report(
    file: UploadFile = File(...),
    patient_id: Optional[str] = Form(None),
    temperature: float = Form(0.5),
    max_new_tokens: int = Form(1024),
    current_user: dict = Depends(get_current_user)
):
    logger.info(f"Report analysis initiated by {current_user['email']}")
    try:
        content_type = file.content_type
        allowed_types = [
            'application/pdf',
            'text/plain',
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
        ]

        if content_type not in allowed_types:
            raise HTTPException(
                status_code=400,
                detail=f"Unsupported file type: {content_type}. Supported types: PDF, TXT, DOCX"
            )

        file_content = await file.read()

        if content_type == 'application/pdf':
            text = extract_text_from_pdf(file_content)
        elif content_type == 'text/plain':
            text = file_content.decode('utf-8')
        elif content_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
            doc = Document(io.BytesIO(file_content))
            text = "\n".join([para.text for para in doc.paragraphs])
        else:
            raise HTTPException(status_code=400, detail="Unsupported file type")

        text = clean_text_response(text)
        if len(text.strip()) < 50:
            raise HTTPException(
                status_code=400,
                detail="Extracted text is too short (minimum 50 characters required)"
            )

        analysis = await analyze_patient_report(
            patient_id=patient_id,
            report_content=text,
            file_type=content_type,
            file_content=file_content
        )

        if "_id" in analysis and isinstance(analysis["_id"], ObjectId):
            analysis["_id"] = str(analysis["_id"])
        if "timestamp" in analysis and isinstance(analysis["timestamp"], datetime):
            analysis["timestamp"] = analysis["timestamp"].isoformat()

        return JSONResponse(content=jsonable_encoder({
            "status": "success",
            "analysis": analysis,
            "patient_id": patient_id,
            "file_type": content_type,
            "file_size": len(file_content)
        }))

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error in report analysis: {str(e)}")
        raise HTTPException(
            status_code=500,
            detail=f"Failed to analyze report: {str(e)}"
        )

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)