-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
178 lines (159 loc) · 5.63 KB
/
Copy pathserver.py
File metadata and controls
178 lines (159 loc) · 5.63 KB
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
from fastapi import FastAPI, HTTPException, UploadFile, File, Form
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Optional, Dict, Any
import json
import os
from datetime import datetime
import uuid
import logging
import aiofiles
from leo import Leo, ClinicalInput
from config import Config
import traceback
# Set up logging
logging.basicConfig(level=logging.INFO)
app = FastAPI(
title="Leo Clinical Documentation Assistant",
description="AI-powered clinical documentation assistant for hospital in-patient rounds",
version="1.0.0"
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Initialize Leo with configuration
config = Config()
leo = Leo(config)
# Create upload directories if they don't exist
UPLOAD_DIR = "uploads"
AUDIO_DIR = os.path.join(UPLOAD_DIR, "audio")
IMAGE_DIR = os.path.join(UPLOAD_DIR, "images")
for directory in [UPLOAD_DIR, AUDIO_DIR, IMAGE_DIR]:
os.makedirs(directory, exist_ok=True)
class NoteRequest(BaseModel):
transcribed_audio: Optional[str] = None
extracted_text_from_images: Optional[str] = None
previous_note: Optional[str] = None
patient_info: Optional[Dict[str, Any]] = None
@app.post("/generate-note")
async def generate_note(request: NoteRequest):
"""
Generate a structured progress note from clinical input data
"""
try:
input_data = ClinicalInput(
transcribed_audio=request.transcribed_audio,
extracted_text_from_images=request.extracted_text_from_images,
previous_note=request.previous_note,
patient_info=request.patient_info
)
note = leo.process_input(input_data)
formatted_note = leo.format_note(note)
return {"note": formatted_note}
except Exception as e:
logging.exception("Error in /generate-note")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/upload-audio")
async def upload_audio(
file: UploadFile = File(...),
patient_info: str = Form(...)
):
"""
Upload and process audio file
"""
try:
# Save audio file with safe filename
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
safe_ext = os.path.splitext(file.filename)[1]
filename = f"{timestamp}_{uuid.uuid4().hex}{safe_ext}"
file_path = os.path.join(AUDIO_DIR, filename)
async with aiofiles.open(file_path, "wb") as buffer:
content = await file.read()
await buffer.write(content)
# Validate patient_info JSON
try:
patient_info_json = json.loads(patient_info)
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="Invalid JSON in patient_info")
# Transcribe audio using OpenAI Whisper
import openai
with open(file_path, "rb") as audio_file:
print("Transcribing audio file...")
transcript_response = openai.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
response_format="text"
)
transcript = transcript_response
print("Transcription completed. Transcript:", transcript)
# Generate note using Leo
input_data = ClinicalInput(
transcribed_audio=transcript,
patient_info=patient_info_json
)
note = leo.process_input(input_data)
formatted_note = leo.format_note(note)
return {
"message": "Audio file uploaded, transcribed, and note generated successfully.",
"filename": filename,
"patient_info": patient_info_json,
"transcript": transcript,
"note": formatted_note
}
except Exception as e:
logging.exception("Error in /upload-audio")
print('Exception:', e)
traceback.print_exc()
raise HTTPException(status_code=500, detail=str(e))
@app.post("/upload-image")
async def upload_image(
file: UploadFile = File(...),
patient_info: str = Form(...)
):
"""
Upload and process image file
"""
try:
# Save image file with safe filename
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
safe_ext = os.path.splitext(file.filename)[1]
filename = f"{timestamp}_{uuid.uuid4().hex}{safe_ext}"
file_path = os.path.join(IMAGE_DIR, filename)
async with aiofiles.open(file_path, "wb") as buffer:
content = await file.read()
await buffer.write(content)
# Validate patient_info JSON
try:
patient_info_json = json.loads(patient_info)
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="Invalid JSON in patient_info")
# TODO: Implement image text extraction
# For now, return a placeholder
return {
"message": "Image file uploaded successfully",
"filename": filename,
"patient_info": patient_info_json
}
except Exception as e:
logging.exception("Error in /upload-image")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/health")
async def health_check():
"""
Health check endpoint
"""
try:
return {
"status": "healthy",
"version": "1.0.0",
"llm_provider": getattr(config.llm, "provider", "unknown"),
"llm_model": getattr(config.llm, "model", "unknown")
}
except Exception as e:
logging.exception("Error in /health")
raise HTTPException(status_code=500, detail=str(e))