Alesmikes commited on
Commit
6e89e8b
·
verified ·
1 Parent(s): efa525e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -28
app.py CHANGED
@@ -2,45 +2,86 @@ import streamlit as st
2
  from audiorecorder import audiorecorder
3
  import openai
4
  import os
5
- import tempfile
6
-
7
- # Nastavení OpenAI klíče
8
  openai.api_key = os.environ['OPENAI_API_KEY']
9
 
10
- def transcribe(audio_bytes):
11
- # Vytvoření dočasného souboru pro uložení audio dat
12
- with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as temp_audio_file:
13
- temp_audio_file.write(audio_bytes)
14
- temp_audio_file_path = temp_audio_file.name
15
-
16
- # Otevření dočasného souboru a jeho předání modelu Whisper
17
- with open(temp_audio_file_path, "rb") as audio_file:
18
- transcript = openai.Audio.transcribe(
19
- file=audio_file,
20
- model="whisper-1"
21
- )
22
- return transcript["choices"][0]["text"]
23
-
24
- # Inicializace session state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  if 'vignette' not in st.session_state:
26
  st.session_state['vignette'] = ''
27
 
 
 
 
 
 
 
28
  if 'length' not in st.session_state:
29
  st.session_state['length'] = 0
30
 
31
- # Hlavní titulek aplikace
32
- st.title("AI Transcription for Healthcare Providers")
33
- st.markdown("Record your patient presentation and get the transcription.")
 
34
  st.divider()
35
 
36
- # Nahrávání audia
37
  audio = audiorecorder("Record", "Stop")
38
 
39
- # Zpracování nahrávky
40
- if len(audio) != st.session_state['length']:
41
  st.session_state['length'] = len(audio)
42
- transcript = transcribe(audio.tobytes())
43
- st.session_state['vignette'] += transcript
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- # Zobrazení transkripce
46
- st.session_state['vignette'] = st.text_area("Transcription", value=st.session_state['vignette'])
 
 
2
  from audiorecorder import audiorecorder
3
  import openai
4
  import os
 
 
 
5
  openai.api_key = os.environ['OPENAI_API_KEY']
6
 
7
+
8
+ def get_completion(messages, model="gpt-3.5-turbo"):
9
+ response = openai.ChatCompletion.create(
10
+ model=model,
11
+ messages=messages,
12
+ temperature=0)
13
+ return response.choices[0].message["content"]
14
+
15
+
16
+ def transcribe(audio_path):
17
+ audio_file = open(audio_path, "rb")
18
+ transcript = openai.Audio.transcribe("whisper-1", audio_file, filename = '1.mp3')
19
+ return transcript["text"]
20
+
21
+
22
+ def get_ddx(vignette):
23
+ messages_ddx = [
24
+ {'role': 'system', 'content': 'Jste nástroj AI asistenta lékaře. Napište sadu příkazů pro pacienta k odlišení mezi stavy. Napište pouze příkazy a zdůvodnění. Nepište žádné další informace. Nepište žádný úvod.'},
25
+ {'role': 'user', 'content': vignette}]
26
+ ddx = get_completion(messages_ddx)
27
+ return ddx
28
+
29
+
30
+ def get_orders(vignette, ddx):
31
+ messages_orders = [
32
+ {'role': 'system', 'content': 'Jste nástroj AI asistenta lékaře. Napište sadu příkazů pro pacienta k odlišení mezi stavy. Napište pouze příkazy a zdůvodnění. Nepište žádné další informace. Nepište žádný úvod.'},
33
+ {'role': 'user', 'content': f'Informace o pacientovi: {vignette}. Rozdílné diagnózy: {ddx}'}]
34
+ orders = get_completion(messages_orders)
35
+ return orders
36
+
37
+
38
  if 'vignette' not in st.session_state:
39
  st.session_state['vignette'] = ''
40
 
41
+ if 'ddx' not in st.session_state:
42
+ st.session_state['ddx'] = ''
43
+
44
+ if 'orders' not in st.session_state:
45
+ st.session_state['orders'] = ''
46
+
47
  if 'length' not in st.session_state:
48
  st.session_state['length'] = 0
49
 
50
+
51
+ st.title("AI loop for healthcare providers")
52
+ st.markdown(
53
+ "Record your patient presentation and get the differential diagnoses and orders.")
54
  st.divider()
55
 
 
56
  audio = audiorecorder("Record", "Stop")
57
 
58
+
59
+ if (len(audio) != st.session_state['length']):
60
  st.session_state['length'] = len(audio)
61
+ # wav_file = open("audio.mp3", "wb")
62
+ # wav_file.write(audio.tobytes())
63
+ transcript = openai.Audio.translate_raw("whisper-1", audio.tobytes(), filename = '1.mp3')
64
+ transcript["text"]
65
+ st.session_state['vignette'] += transcript["text"]
66
+
67
+
68
+ st.session_state['vignette'] = st.text_area(
69
+ "Vignette", value=st.session_state['vignette'])
70
+
71
+
72
+ if st.button("Get DDX and Orders"):
73
+ vignette = st.session_state['vignette']
74
+ ddx = get_ddx(vignette)
75
+ st.session_state['ddx'] = ddx
76
+ st.session_state['orders'] = get_orders(vignette, ddx)
77
+
78
+
79
+ col1, col2 = st.columns(2)
80
+
81
+ with col1:
82
+ st.markdown(
83
+ f"**DDX**\n\n{st.session_state['ddx']}", unsafe_allow_html=True)
84
 
85
+ with col2:
86
+ st.markdown(
87
+ f"**ORDERS**\n\n{st.session_state['orders']}", unsafe_allow_html=True)