72 lines
3.2 KiB
Python
72 lines
3.2 KiB
Python
import os
|
|
import sys
|
|
import json
|
|
import urllib.request
|
|
|
|
# Load env from .env file manually (no external deps needed)
|
|
_env_path = os.path.join(os.path.dirname(__file__), '.env')
|
|
if os.path.exists(_env_path):
|
|
with open(_env_path) as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if line and not line.startswith('#') and '=' in line:
|
|
k, v = line.split('=', 1)
|
|
os.environ.setdefault(k.strip(), v.strip())
|
|
|
|
SARVAM_API_KEY = os.getenv("SARVAM_API_KEY", "")
|
|
|
|
def test():
|
|
prompt = """You are a medical transcription editor. You will receive a transcript of a doctor-patient conversation with acoustic timestamps.
|
|
Your task is to identify the speakers, group consecutive speech from the same speaker into single segments, and output a strict JSON array.
|
|
|
|
RULES:
|
|
1. Identify who is speaking ("Doctor" or "Patient"). Do not use generic labels like "speaker_0".
|
|
2. Group consecutive lines by the same speaker into one block.
|
|
3. The "start" timestamp should be the start time of the first phrase in the block.
|
|
4. The "end" timestamp should be the end time of the last phrase in the block.
|
|
5. Filter out obvious ASR hallucinations like "subscribe", "leave a comment", "follow the channel".
|
|
6. Return EXACTLY and ONLY a JSON array, no markdown, no other text.
|
|
|
|
Example Input:
|
|
[00:00:00 - 00:00:04] नमस्कार तुम्हाला काय त्रास होतो आहे?
|
|
[00:00:04 - 00:00:09] डॉक्टर मला दोन दिवसांपासून खूप ताप आहे.
|
|
[00:00:09 - 00:00:11] बरं, खोकला आहे का?
|
|
|
|
Example Output:
|
|
[
|
|
{ "speaker": "Doctor", "start": "00:00:00", "end": "00:00:04", "text": "नमस्कार तुम्हाला काय त्रास होतो आहे?" },
|
|
{ "speaker": "Patient", "start": "00:00:04", "end": "00:00:09", "text": "डॉक्टर मला दोन दिवसांपासून खूप ताप आहे." },
|
|
{ "speaker": "Doctor", "start": "00:00:09", "end": "00:00:11", "text": "बरं, खोकला आहे का?" }
|
|
]"""
|
|
|
|
transcript = """[00:00:00 - 00:00:05] नमस्कार या बसा तुम्हाला काय त्रास होतो?
|
|
[00:00:05 - 00:00:10] मला कालपासून ताप आहे."""
|
|
|
|
payload = json.dumps({
|
|
"model": "sarvam-105b",
|
|
"temperature": 0.1,
|
|
"messages": [
|
|
{"role": "system", "content": prompt},
|
|
{"role": "user", "content": transcript}
|
|
]
|
|
}).encode('utf-8')
|
|
|
|
req = urllib.request.Request(
|
|
'https://api.sarvam.ai/v1/chat/completions',
|
|
data=payload,
|
|
headers={
|
|
'Content-Type': 'application/json',
|
|
'api-subscription-key': SARVAM_API_KEY
|
|
}
|
|
)
|
|
|
|
with urllib.request.urlopen(req, timeout=60) as resp:
|
|
body = resp.read().decode('utf-8')
|
|
data = json.loads(body)
|
|
content = data['choices'][0]['message']['content']
|
|
sys.stdout.buffer.write(("STATUS: " + str(resp.status) + "\n").encode('utf-8'))
|
|
sys.stdout.buffer.write(("OUTPUT:\n" + content + "\n").encode('utf-8'))
|
|
sys.stdout.flush()
|
|
|
|
test()
|