|
from fastapi import FastAPI, File, UploadFile , APIRouter , Request |
|
from fastapi.responses import JSONResponse |
|
from typing import List |
|
from controllers.transcription_controller import process_uploaded_files |
|
from openai import OpenAI |
|
import os |
|
|
|
|
|
router = APIRouter() |
|
|
|
client = OpenAI( |
|
api_key=os.getenv('OPENAI_API_KEY') |
|
) |
|
|
|
CLINICAL_SUMMARY_PROMPT = """ |
|
You are a clinical summarization engine for psychiatric case notes. Given a transcription of a doctor–patient conversation, extract all clinically relevant details valuable for both doctor and patient. Specifically, extract the following: |
|
|
|
- Patient Info: e.g., identifier, demographics. |
|
- Session Details: date, time, chief complaint, presenting symptoms. |
|
- History: psychiatric history and any other relevant medical history. |
|
- Mental Status Exam: appearance, behavior, speech, mood, affect, thought process, thought content, perception, cognition, insight, and judgment. |
|
- Assessment: clinician’s assessment/diagnosis. |
|
- Risk Assessment: any risks (e.g., suicidal ideation). |
|
- Treatment Plan: recommendations, follow-up plans. |
|
- Key Points: a list of critical details. |
|
|
|
For any missing data, use "N/A". Output only the JSON in the exact structure below without additional commentary. |
|
|
|
JSON Structure Example: |
|
{ |
|
"patient_info": { |
|
"id": "N/A", |
|
"demographics": "N/A" |
|
}, |
|
"session_details": { |
|
"date": "YYYY-MM-DD", |
|
"time": "HH:MM", |
|
"chief_complaint": "Summary of chief complaint", |
|
"presenting_symptoms": "Summary of symptoms" |
|
}, |
|
"history": { |
|
"psychiatric": "Summary of psychiatric history", |
|
"medical": "Summary of other medical history" |
|
}, |
|
"mental_status_exam": { |
|
"appearance": "Details of appearance", |
|
"behavior": "Details of behavior", |
|
"speech": "Details of speech", |
|
"mood": "Details of mood", |
|
"affect": "Details of affect", |
|
"thought_process": "Details of thought process", |
|
"thought_content": "Details of thought content", |
|
"perception": "Details of perception", |
|
"cognition": "Details of cognition", |
|
"insight": "Details of insight", |
|
"judgment": "Details of judgment" |
|
}, |
|
"assessment": "Clinician's assessment and diagnosis", |
|
"risk_assessment": "Risk factors (e.g., suicidal ideation)", |
|
"treatment_plan": "Summary of treatment recommendations and follow-up plans", |
|
"follow_up": "Next steps or appointment details", |
|
"key_points": [ |
|
"Key point 1", |
|
"Key point 2" |
|
] |
|
} |
|
""" |
|
|
|
|
|
@router.post("/transcribe") |
|
async def transcribe(files: List[UploadFile] = File(...)): |
|
results = await process_uploaded_files(files) |
|
print("Audio Text") |
|
print(results) |
|
return JSONResponse(content={'results': results}) |
|
|
|
@router.post("/summarize") |
|
async def transcribe(request:Request): |
|
body = await request.json() |
|
text = body.get('text') |
|
result = text |
|
|
|
response = client.chat.completions.create( |
|
messages=[ |
|
{"role": "system", "content": CLINICAL_SUMMARY_PROMPT}, |
|
{ |
|
"role": "user", |
|
"content": f"I am providing you with the transcription of the recording of doctor and patient , follow the exact instructions given in the system prompt and generate the json response accordingly . Make sure to cover all the points from the transcribed text given below. : \n {result}", |
|
}, |
|
|
|
], |
|
model="gpt-4o-mini", |
|
temperature = 0.5, |
|
response_format = { "type": "json_object" } |
|
) |
|
|
|
print("summarized response") |
|
print(response.choices[0].message.content) |
|
return JSONResponse(content={'summarized_results': response.choices[0].message.content}) |
|
|
|
|