Spaces:
Sleeping
Sleeping
Upload ailib.py
Browse files
ailib.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
OPENAI_TRANSCRIPTION_MODEL='whisper-1'
|
| 5 |
+
OPENAI_COMPLETION_MODEL = "gpt-4o-mini"
|
| 6 |
+
TOKEN_LIMIT = 4096
|
| 7 |
+
|
| 8 |
+
openai_client = openai.OpenAI()
|
| 9 |
+
|
| 10 |
+
def openai_query(prompt):
|
| 11 |
+
response = openai_client.chat.completions.create(
|
| 12 |
+
model = OPENAI_COMPLETION_MODEL,
|
| 13 |
+
messages = [{'role': 'user',
|
| 14 |
+
'content': prompt}
|
| 15 |
+
],
|
| 16 |
+
temperature = 0, max_tokens = TOKEN_LIMIT,
|
| 17 |
+
top_p=1.0, frequency_penalty=0.0,
|
| 18 |
+
presence_penalty=0.0)
|
| 19 |
+
resp_text = response.choices[0].message.content
|
| 20 |
+
return resp_text
|
| 21 |
+
|
| 22 |
+
def nueva_ficha(resumen_historial, resumen_hoy):
|
| 23 |
+
|
| 24 |
+
new_prompt = f'''Escribe una nueva ficha m茅dica, considerando el historial m茅dico: "{resumen_historial}",
|
| 25 |
+
y tambi茅n lo que ocurri贸 hoy: "{resumen_hoy}"
|
| 26 |
+
'''
|
| 27 |
+
nueva = openai_query(new_prompt)
|
| 28 |
+
|
| 29 |
+
return nueva
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def whisper_transcribe(fn, temperature=0):
|
| 33 |
+
print('WT:', type(fn))
|
| 34 |
+
t0 = time.time()
|
| 35 |
+
|
| 36 |
+
if isinstance(fn, str):
|
| 37 |
+
audio_file = open(fn,'rb') # redundant?
|
| 38 |
+
else:
|
| 39 |
+
open('temp.wav','wb').write(fn)
|
| 40 |
+
audio_file = 'temp.wav'
|
| 41 |
+
|
| 42 |
+
whisper = openai_client.audio.transcriptions.create
|
| 43 |
+
transcript = whisper(model=OPENAI_TRANSCRIPTION_MODEL,
|
| 44 |
+
file = audio_file, language='es',
|
| 45 |
+
temperature=0.0) # to do, explore temperature
|
| 46 |
+
dt = round(time.time()-t0,2)
|
| 47 |
+
|
| 48 |
+
transcript = transcript.text
|
| 49 |
+
print(f'Whisper transcribe [dt={dt} secs]')
|
| 50 |
+
return transcript
|
| 51 |
+
|
| 52 |
+
def summarize(text):
|
| 53 |
+
prompt = f"Resume todos los aspectos m茅dicos de esta consulta: '{text}'"
|
| 54 |
+
resp_text = openai_query(prompt)
|
| 55 |
+
return resp_text
|