Files
Doctor_Patient_Summary/test_diarize_rules.py
T

62 lines
4.2 KiB
Python

import os, urllib.request, json, sys
sys.stdout.reconfigure(encoding='utf-8')
api_key = None
with open('.env') as f:
for line in f:
if line.startswith('SARVAM_API_KEY='):
api_key = line.strip().split('=', 1)[1].strip('"\' ')
test_text = """
नमस्कार या बसा काय त्रास होतोय तुम्हाला नमस्कार डॉक्टर मला गेले तीन दिवस झाले खूप पोटात दुखत आहे आणि काल रात्रीपासून थोडी उलटी पण झाली ठीक आहे पोटात नेमके कुठे दुखते आहे वरच्या भागात की खालच्या भागात खालच्या भागात उजव्या बाजूला खूप जास्त दुखते आहे काही खाल्ल्यावर जास्त दुखते का हो जेवण केले की त्रास वाढतो ताप किंवा चक्कर येते का हो थोडा बारीक ताप आहे ठीक आहे मी तपासून पाहतो तुम्ही आधी कोणती औषधे घेतली होती का नाही काहीच घेतले नाही ठीक आहे आपण एक सोनोग्राफी आणि ब्लड टेस्ट करू आणि तोपर्यंत मी वेदना कमी करण्यासाठी औषध देतो
"""
DIARIZE_SYSTEM_PROMPT = """You are an expert Marathi, Hindi, and English medical conversation diarization AI.
Your task is to accurately separate a continuous medical consultation transcript into clear speaking turns for Doctor and Patient.
SPEAKER IDENTIFICATION RULES:
1. **Doctor**:
- Greets and invites the patient ("या, बसा", "काय त्रास होतोय?")
- Asks diagnostic questions ("कधीपासून दुखतंय?", "ताप आहे का?", "कुठे दुखतंय?", "काही खाल्ल्यावर त्रास होतो का?")
- Inquires about past history / medications ("आधी कोणती औषधे घेतली का?")
- Gives medical instructions, advice, prescriptions, and orders tests ("तपासून पाहतो", "सोनोग्राफी करू", "गोळ्या लिहून देतो", "3 दिवस घ्या").
2. **Patient**:
- Describes complaints, pain, and symptoms ("पोटात दुखत आहे", "उलटी झाली", "खोकला येतोय", "ताप आलाय")
- Answers the doctor's questions ("खालच्या भागात उजव्या बाजूला", "हो जेवण केले की वाढतो", "नाही काही घेतले नाही", "दोन दिवसांपासून")
- Gives short confirmations ("हो डॉक्टर", "नाही", "ठीक आहे").
OUTPUT FORMAT:
Output ONLY alternating speaking turns starting strictly with "**Doctor:** " or "**Patient:** ".
Do NOT add extra narration, thoughts, or markdown headings.
Example output format:
**Doctor:** [Doctor statement or question]
**Patient:** [Patient statement or answer]
**Doctor:** [Doctor statement or question]
**Patient:** [Patient statement or answer]"""
req = urllib.request.Request(
'https://api.sarvam.ai/v1/chat/completions',
data=json.dumps({
'model': 'sarvam-105b-conversations',
'temperature': 0.1,
'max_tokens': 2000,
'messages': [
{'role': 'system', 'content': DIARIZE_SYSTEM_PROMPT},
{'role': 'user', 'content': f"Transcript:\n{test_text.strip()}"}
]
}).encode('utf-8'),
headers={
'Content-Type': 'application/json',
'api-subscription-key': api_key
}
)
try:
with urllib.request.urlopen(req) as resp:
res = json.loads(resp.read().decode('utf-8'))
print("Diarization Result:\n" + res['choices'][0]['message']['content'])
except Exception as e:
print("Error:", e)