48 lines
3.3 KiB
Python
48 lines
3.3 KiB
Python
import os, json, urllib.request, urllib.error
|
|
|
|
_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:
|
|
if '=' in line and not line.startswith('#'):
|
|
k, v = line.strip().split('=', 1)
|
|
os.environ.setdefault(k, v)
|
|
|
|
SARVAM_API_KEY = os.getenv("SARVAM_API_KEY", "")
|
|
|
|
sample_text = """आपण हे dissection accelerated हे केले तुम्ही आधी इथं पहिल्यांदा सरांना दाखवले फक्त त्याच्या आधी हो इथं आधी पहिल्यापासून इथं treatment चालू आता एक फक्त आपल्या इथला एक नवीन हे चालू आहे EMR की जे directly audio connect करून इथं notes करतात त्याचं experimentation चालू आहे त्यासाठी फक्त जर तुमची परवानगी असेल तर आपलं जे conversation आहे ते record करतायत. चालेल तुम्हाला IT team-चं? म्हणजे आपण जे बोलतोय ना तेच फक्त record करणार जसं AI असतं ना AI घेऊन direct notes generate करतील असं त्यांचा प्रयोग चाललाय. अच्छा अच्छा. त्या intention-साठी आहे बाकी काही नाही. ठीक आहे. Okay that's okay. आधी तुमची treatment 4 cycle chemo झालेले आहेत. हो. 2006-मध्ये त्याच्यानंतर त्याच्यानंतर 1 वर्षाने. MRI 5 वर्षांनी MRI आणि बाकी mammography आणि sonography दर वर्षाला. म्हणजे 2007 पासून continuous follow-up आहे सरांचा. आणि आता त्रास काय व्हायला लागला तुम्हाला? त्रास काहीच नव्हता follow-up-ला आलो होतो आम्ही."""
|
|
|
|
prompt = """You are a medical conversation transcript editor.
|
|
Given a consultation transcript between a Doctor and Patient in Marathi/English, split it into alternating speaker turns.
|
|
Output strict JSON array with this schema:
|
|
[
|
|
{"speaker": "Doctor", "text": "..."},
|
|
{"speaker": "Patient", "text": "..."}
|
|
]
|
|
Do not output anything other than JSON array."""
|
|
|
|
payload = json.dumps({
|
|
"model": "sarvam-105b",
|
|
"temperature": 0.1,
|
|
"max_tokens": 2048,
|
|
"messages": [
|
|
{"role": "system", "content": prompt},
|
|
{"role": "user", "content": sample_text}
|
|
]
|
|
}).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}
|
|
)
|
|
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=40) as resp:
|
|
data = json.loads(resp.read().decode('utf-8'))
|
|
import sys
|
|
sys.stdout.buffer.write(json.dumps(data, ensure_ascii=False, indent=2).encode('utf-8'))
|
|
except urllib.error.HTTPError as e:
|
|
print("STATUS:", e.code)
|
|
print("BODY:", e.read().decode('utf-8'))
|