File size: 3,601 Bytes
ea5fab8
a0f7dcb
 
26ef07e
6eed537
18687ca
 
9f559c6
 
 
18687ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0f7dcb
 
 
b19cd5f
 
ea5fab8
 
 
 
b21a26b
ea5fab8
18687ca
 
 
 
 
 
 
6cea9eb
18687ca
 
 
 
6cea9eb
18687ca
 
 
 
 
 
ea5fab8
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
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})