Update routers/transcription.py
Browse files- routers/transcription.py +80 -4
routers/transcription.py
CHANGED
@@ -2,9 +2,71 @@ from fastapi import FastAPI, File, UploadFile , APIRouter , Request
|
|
2 |
from fastapi.responses import JSONResponse
|
3 |
from typing import List
|
4 |
from controllers.transcription_controller import process_uploaded_files
|
|
|
|
|
|
|
5 |
|
6 |
router = APIRouter()
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
@router.post("/transcribe")
|
9 |
async def transcribe(files: List[UploadFile] = File(...)):
|
10 |
results = await process_uploaded_files(files)
|
@@ -16,8 +78,22 @@ async def transcribe(files: List[UploadFile] = File(...)):
|
|
16 |
async def transcribe(request:Request):
|
17 |
body = await request.json()
|
18 |
text = body.get('text')
|
19 |
-
result =
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
|
|
2 |
from fastapi.responses import JSONResponse
|
3 |
from typing import List
|
4 |
from controllers.transcription_controller import process_uploaded_files
|
5 |
+
import openai
|
6 |
+
import os
|
7 |
+
|
8 |
|
9 |
router = APIRouter()
|
10 |
|
11 |
+
client = OpenAI(
|
12 |
+
api_key=os.getenv('OPENAI_API_KEY')
|
13 |
+
)
|
14 |
+
|
15 |
+
CLINICAL_SUMMARY_PROMPT = """
|
16 |
+
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:
|
17 |
+
|
18 |
+
- Patient Info: e.g., identifier, demographics.
|
19 |
+
- Session Details: date, time, chief complaint, presenting symptoms.
|
20 |
+
- History: psychiatric history and any other relevant medical history.
|
21 |
+
- Mental Status Exam: appearance, behavior, speech, mood, affect, thought process, thought content, perception, cognition, insight, and judgment.
|
22 |
+
- Assessment: clinician’s assessment/diagnosis.
|
23 |
+
- Risk Assessment: any risks (e.g., suicidal ideation).
|
24 |
+
- Treatment Plan: recommendations, follow-up plans.
|
25 |
+
- Key Points: a list of critical details.
|
26 |
+
|
27 |
+
For any missing data, use "N/A". Output only the JSON in the exact structure below without additional commentary.
|
28 |
+
|
29 |
+
JSON Structure Example:
|
30 |
+
{
|
31 |
+
"patient_info": {
|
32 |
+
"id": "N/A",
|
33 |
+
"demographics": "N/A"
|
34 |
+
},
|
35 |
+
"session_details": {
|
36 |
+
"date": "YYYY-MM-DD",
|
37 |
+
"time": "HH:MM",
|
38 |
+
"chief_complaint": "Summary of chief complaint",
|
39 |
+
"presenting_symptoms": "Summary of symptoms"
|
40 |
+
},
|
41 |
+
"history": {
|
42 |
+
"psychiatric": "Summary of psychiatric history",
|
43 |
+
"medical": "Summary of other medical history"
|
44 |
+
},
|
45 |
+
"mental_status_exam": {
|
46 |
+
"appearance": "Details of appearance",
|
47 |
+
"behavior": "Details of behavior",
|
48 |
+
"speech": "Details of speech",
|
49 |
+
"mood": "Details of mood",
|
50 |
+
"affect": "Details of affect",
|
51 |
+
"thought_process": "Details of thought process",
|
52 |
+
"thought_content": "Details of thought content",
|
53 |
+
"perception": "Details of perception",
|
54 |
+
"cognition": "Details of cognition",
|
55 |
+
"insight": "Details of insight",
|
56 |
+
"judgment": "Details of judgment"
|
57 |
+
},
|
58 |
+
"assessment": "Clinician's assessment and diagnosis",
|
59 |
+
"risk_assessment": "Risk factors (e.g., suicidal ideation)",
|
60 |
+
"treatment_plan": "Summary of treatment recommendations and follow-up plans",
|
61 |
+
"follow_up": "Next steps or appointment details",
|
62 |
+
"key_points": [
|
63 |
+
"Key point 1",
|
64 |
+
"Key point 2"
|
65 |
+
]
|
66 |
+
}
|
67 |
+
"""
|
68 |
+
|
69 |
+
|
70 |
@router.post("/transcribe")
|
71 |
async def transcribe(files: List[UploadFile] = File(...)):
|
72 |
results = await process_uploaded_files(files)
|
|
|
78 |
async def transcribe(request:Request):
|
79 |
body = await request.json()
|
80 |
text = body.get('text')
|
81 |
+
result = text
|
82 |
+
|
83 |
+
response = client.chat.completions.create(
|
84 |
+
messages=[
|
85 |
+
{"role": "system", "content": CLINICAL_SUMMARY_PROMPT},
|
86 |
+
{
|
87 |
+
"role": "user",
|
88 |
+
"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 recording accordingly : \n {result}",
|
89 |
+
},
|
90 |
+
|
91 |
+
],
|
92 |
+
model="gpt-4o-mini",
|
93 |
+
response_format = { "type": "json_object" }
|
94 |
+
)
|
95 |
+
|
96 |
+
print("summarized response")
|
97 |
+
print(response.choices[0].message.content)
|
98 |
+
return JSONResponse(content={'summarized_results': response.choices[0].message.content})
|
99 |
|