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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# Ref https://github.com/ezzcodeezzlife/dalle2-in-python
# Ref https://towardsdatascience.com/speech-to-text-with-openais-whisper-53d5cea9005e
# Ref https://python.plainenglish.io/creating-an-awesome-web-app-with-python-and-streamlit-728fe100cf7
import logging
import logging.handlers
import queue
import threading
import time
import urllib.request
from collections import deque
from pathlib import Path
from typing import List
# import whisper
# import av
import numpy as np
import pydub
import streamlit as st
from tqdm import tqdm
from streamlit_webrtc import WebRtcMode, webrtc_streamer

from dalle2 import Dalle2
from PIL import Image

HERE = Path(__file__).parent

logger = logging.getLogger(__name__)

# Initialize the OpenAI API with your API key
import openai
from openai import OpenAI
api_key = st.secrets["OPENAI_API_KEY"]
client = OpenAI(api_key=api_key)

prompt = """I am a doctor, I would like you to check my prescription:
medical history: Hypertension, Type 2 Diabetes, and Asthma.
symptoms: Persistent cough, fever, and fatigue.
My prescription: Lisinopril 10mg daily, Metformin 500mg twice daily, and Albuterol as needed for asthma attacks.
Drug contexts:
- Lisinopril: Ingredients: ACE inhibitor. Adverse effects: Dizziness, dry cough, elevated blood potassium levels.
- Metformin: Ingredients: Oral antihyperglycemic agent. Adverse effects: Stomach upset, diarrhea, low blood sugar.
- Albuterol: Ingredients: Bronchodilator. Adverse effects: Tremors, nervousness, increased heart rate.

Please answer the following questions in concise point form, taking into account the provided drug context:
- Possible interactions between prescribed drugs?
- Adverse effect of given drugs that are specifically related to patient’s pre-existing conditions and medical history?

At the end of your answer, evaluate the level of dangerousness of this treatment, based on interactions and adverse effects. Dangerousness is categorized as: LOW, MEDIUM, HIGH
Your answer should look like this:
`
* interactions:
- <interaction 1>
- <interaction 2>
- ...

* adverse effects:
- <adverse effect 1>
- <adverse effect 2>
- ...`

* dangerousness: <LOW / MEDIUM / HIGH>

Note that you don't have to include any interactions or adverse effect, only those that are necessary.
"""
def get_drug_info_string(drug_names):
    # Make the drug_to_info dictionary into a string with each line of the form drug: info
    drug_info_string = ""
    for drug in drug_names:
        info = search_openfda_drug(drug)
        drug_info_string += drug + ": " + str(trim_openfda_response(search_openfda_drug(drug))) + "\r\n"
    return drug_info_string
import requests

def trim_openfda_response(json_response):
    """Trim the openFDA JSON response to include only specific fields.

    Parameters:
    - json_response (dict): The raw JSON response from the openFDA API.

    Returns:
    - dict: A trimmed version of the JSON response.
    """

    # List of desired fields
    desired_fields = [
        "spl_product_data_elements",
        "boxed_warning",
        "contraindications",
        "drug_interactions",
        "adverse_reactions",
        "warnings"
    ]

    trimmed_response = {}

    # Check if results are present in the response
    if 'results' in json_response:
        for field in desired_fields:
            if field in json_response['results'][0]:
                trimmed_response[field] = json_response['results'][0][field]

    return trimmed_response

def search_openfda_drug(drug_name):
    """Search for a drug in the openFDA database.

    Parameters:
    - drug_name (str): The name of the drug to search for.

    Returns:
    - dict: The JSON response from the openFDA API containing drug information, or None if there's an error.
    """

    base_url = "https://api.fda.gov/drug/label.json"
    query = f"?search=openfda.generic_name:{drug_name}&limit=1"

    try:
        response = requests.get(base_url + query)

        # Check for successful request
        if response.status_code == 200:
            return response.json()

    except requests.RequestException:
        # If any request-related exception occurs, simply return None
        print(f"Error encountered searching for drug {drug_name} with code {response.status_code}.")

    return None

def ask_gpt(question, model="gpt-3.5-turbo"):
    """
    Query the GPT-3.5 Turbo model with a given question.

    Parameters:
    - question (str): The input question or prompt for the model.

    Returns:
    - str: The model's response.
    """

    response = client.chat.completions.create(
        model=model,
        # model="gpt-4-vision-preview",
        # model = "gpt-4-0125-preview",
        # model = "gpt-4",
        # model = "gpt-4-turbo-preview",
        messages=[
            {"role": "system", "content": "You are a knowledgeable medical database designed to provide concise and direct answers to medical questions."},
            {"role": "user", "content": question}
        ]
    )

    # print(response.choices[0])
    # return response.choices.message['content']
    return response.choices[0].message.content

def parse_gpt(question):
    """
    Query the GPT-3.5 Turbo model with a given question.

    Parameters:
    - question (str): The input question or prompt for the model.

    Returns:
    - str: The model's response.
    """

    # 1 parse text to replace critical information in the prompt
    # 2 send parsed text in OpenAPI

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a knowledgeable medical database designed to provide concise and direct answers to medical questions."},
            {"role": "user", "content": question}
        ]
    )

    return response.choices[0].message['content']

# This code is based on https://github.com/streamlit/demo-self-driving/blob/230245391f2dda0cb464008195a470751c01770b/streamlit_app.py#L48  # noqa: E501
def download_file(url, download_to: Path, expected_size=None):
    # Don't download the file twice.
    # (If possible, verify the download using the file length.)
    if download_to.exists():
        if expected_size:
            if download_to.stat().st_size == expected_size:
                return
        else:
            st.info(f"{url} is already downloaded.")
            if not st.button("Download again?"):
                return

    download_to.parent.mkdir(parents=True, exist_ok=True)

    # These are handles to two visual elements to animate.
    weights_warning, progress_bar = None, None
    try:
        weights_warning = st.warning("Downloading %s..." % url)
        progress_bar = st.progress(0)
        with open(download_to, "wb") as output_file:
            with urllib.request.urlopen(url) as response:
                length = int(response.info()["Content-Length"])
                counter = 0.0
                MEGABYTES = 2.0 ** 20.0
                while True:
                    data = response.read(8192)
                    if not data:
                        break
                    counter += len(data)
                    output_file.write(data)

                    # We perform animation by overwriting the elements.
                    weights_warning.warning(
                        "Downloading %s... (%6.2f/%6.2f MB)"
                        % (url, counter / MEGABYTES, length / MEGABYTES)
                    )
                    progress_bar.progress(min(counter / length, 1.0))
    # Finally, we remove these visual elements by calling .empty().
    finally:
        if weights_warning is not None:
            weights_warning.empty()
        if progress_bar is not None:
            progress_bar.empty()


def main():
    st.header("openFDA Medical Records Evaluation")
    st.markdown(
        """
This demo app is using [DeepSpeech](https://github.com/mozilla/DeepSpeech),
an open speech-to-text engine.

A pre-trained model released with
[v0.9.3](https://github.com/mozilla/DeepSpeech/releases/tag/v0.9.3),
trained on American English is being served.
"""
    )

    # https://github.com/mozilla/DeepSpeech/releases/tag/v0.9.3
    MODEL_URL = "https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/deepspeech-0.9.3-models.pbmm"  # noqa
    LANG_MODEL_URL = "https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/deepspeech-0.9.3-models.scorer"  # noqa
    MODEL_LOCAL_PATH = HERE / "models/deepspeech-0.9.3-models.pbmm"
    LANG_MODEL_LOCAL_PATH = HERE / "models/deepspeech-0.9.3-models.scorer"

    #download_file(MODEL_URL, MODEL_LOCAL_PATH, expected_size=188915987)
    #download_file(LANG_MODEL_URL, LANG_MODEL_LOCAL_PATH, expected_size=953363776)

    lm_alpha = 0.931289039105002
    lm_beta = 1.1834137581510284
    beam = 100

    medical_text_page = "Medical text evaluation" # summarize notes and identify risk from notes (useful for change in doctors)
    voice_to_text_page = "Voice to medical text"  # use voice to text and identify risk, could be use in case
    image_to_text_page = "Image to medical text"  # use image to text and identify risk, could be use in case
    all_in_one_page = "All modalities"            # use all modalities
    sound_only_page = "Sound only (sendonly)"
    with_video_page = "With video (sendrecv)"
    text_only_page = "Text only for DALLE2"
    app_mode = st.selectbox(
        "Choose the app mode", 
        # [sound_only_page, with_video_page, text_only_page, medical_text_page]
        [medical_text_page, voice_to_text_page, image_to_text_page, all_in_one_page]
    )



    if app_mode == sound_only_page:
        app_sst(
            str(MODEL_LOCAL_PATH), str(LANG_MODEL_LOCAL_PATH), lm_alpha, lm_beta, beam
        )
    elif app_mode == with_video_page:
        app_sst_with_video(
            str(MODEL_LOCAL_PATH), str(LANG_MODEL_LOCAL_PATH), lm_alpha, lm_beta, beam
        )
    elif app_mode == medical_text_page:
        form = st.form(key='my-form')
        text = form.text_input('Medical text description')
        submit = form.form_submit_button('Submit')

        st.write('Press submit to evaluate medical notes')

        if submit:
            # res = parse_gpt(text + "Organize the answers in 4 parts, first is pre-existing conditions, second is symptoms, third is test, fourth is prescriptions. Sample output for drugs should be the end of the answer as DRUG_NAMES: <drug 1>, <drug 2>, <drug 3>...")
            parsed_notes = ask_gpt(f"""
                    Please parse the following medical note in point form, without losing any important information:
                    `{text}`

                    your answer should look like: 
                    `**Patient's medical history**:
                    - <point 1>
                    - <point 2>
                    - ...

                    **Patient's symptoms**:
                    - <point 1>
                    - <point 2>
                    - ...

                    **Patient's health indicators and clinical tests**:
                    - <test 1>
                    - <test 2>

                    **Prescription**:
                    - ...

                    DRUGS: <drug 1>, <drug 2>, ...
                    `
                    Please be reminded to give the generic names for the drugs
                    """)
            st.write(parsed_notes)
            # Extract the drugs portion from the notes
            drug_line = [line for line in parsed_notes.split("\n") if line.startswith("DRUGS:")][0]

            # Strip the "DRUGS: " prefix and split the drugs by ", "
            drugs = drug_line.replace("DRUGS: ", "").strip().split(", ")
            
            # Go to FDA
            drug_info_string = get_drug_info_string(drugs)
            # st.write(drug_info_string)

            #  #
            risk = ask_gpt(f"""I am a doctor, I would like you to check my prescription:
                {parsed_notes}

                Drug contexts:
                {drug_info_string}

                Please answer the following questions in concise point form, taking into account the provided drug context:
                - Possible interactions between prescribed drugs?
                - Adverse effect of given drugs, only answer those that are specifically related to patient’s pre-existing conditions and symptoms?

                At the end of your answer, evaluate the level of dangerousness of this treatment, based on interactions and adverse effects that are specific to the patient. 
                Dangerousness is categorized as: LOW, MEDIUM, HIGH.
                If you know any references cases, show it and hightlight the similarities.
                Your answer should look like this (you should include the * where specified):
                `
                * **INTERACTIONS**:
                - <interaction 1>
                - <interaction 2>
                - ...

                * **ADVERSE EFFECTS**:
                - <adverse effect 1>
                - <adverse effect 2>
                - ...`

                * **DANGEROUSNESS**: <LOW / MEDIUM / HIGH>

                * **REFERENCES**:
                - <reference case 1>
                - <reference case 2>

                Note that you don't have to include any interactions or adverse effect, only those that are necessary.
                """, model = 'gpt-3.5-turbo-16k')
            # st.write(res)
            st.write(risk)

    elif app_mode == text_only_page:
        form = st.form(key='my-form')
        text = form.text_input('Image description')
        submit = form.form_submit_button('Submit')

        st.write('Press submit to generate image')

        if submit:
            app_sst_dalle2(text)

        # form = st.form(key='my_form')
        # text = form.text_input(label='Image Description')
        # submit_button = form.form_submit_button(label='Submit')
        # if submit_button:
        #     app_sst_dalle2(form.text)

        #text = st.text_input('Image description')
        #if st.form_submit_button('Generate') == True:
        #    app_sst_dalle2(text)


def app_sst_dalle2(text):
    dalle = Dalle2("sess-TotC46rSs5pbqdXTRy75cr81ynLJALwa2b3rdxeh")
    #generations = dalle.generate(text)
    file_paths = dalle.generate_and_download(text)
    print(file_paths)
    #generations = dalle.generate_amount(text, 8) # Every generation has batch size 4 -> amount % 4 == 0 works best
    for file in file_paths:
        image = Image.open(file)
        st.image(image, caption=text)

def app_sst(model_path: str, lm_path: str, lm_alpha: float, lm_beta: float, beam: int):
    webrtc_ctx = webrtc_streamer(
        key="speech-to-text",
        mode=WebRtcMode.SENDONLY,
        audio_receiver_size=1024,
        rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
        media_stream_constraints={"video": False, "audio": True},
    )

    status_indicator = st.empty()

    if not webrtc_ctx.state.playing:
        return

    status_indicator.write("Loading...")
    text_output = st.empty()
    stream = None

    while True:
        if webrtc_ctx.audio_receiver:
            if stream is None:
                from deepspeech import Model
                # https://github.com/openai/whisper
                # model = whisper.load_model(“large”)

                model = Model(model_path)
                model.enableExternalScorer(lm_path)
                model.setScorerAlphaBeta(lm_alpha, lm_beta)
                model.setBeamWidth(beam)

                stream = model.createStream()

                status_indicator.write("Model loaded.")

            sound_chunk = pydub.AudioSegment.empty()
            try:
                audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=1)
            except queue.Empty:
                time.sleep(0.1)
                status_indicator.write("No frame arrived.")
                continue

            status_indicator.write("Running. Say something!")

            for audio_frame in audio_frames:
                sound = pydub.AudioSegment(
                    data=audio_frame.to_ndarray().tobytes(),
                    sample_width=audio_frame.format.bytes,
                    frame_rate=audio_frame.sample_rate,
                    channels=len(audio_frame.layout.channels),
                )
                sound_chunk += sound

            if len(sound_chunk) > 0:
                sound_chunk = sound_chunk.set_channels(1).set_frame_rate(
                    model.sampleRate()
                )
                buffer = np.array(sound_chunk.get_array_of_samples())
                stream.feedAudioContent(buffer)
                text = stream.intermediateDecode()
                text_output.markdown(f"**Text:** {text}")
        else:
            status_indicator.write("AudioReciver is not set. Abort.")
            break


def app_sst_with_video(
    model_path: str, lm_path: str, lm_alpha: float, lm_beta: float, beam: int
):
    frames_deque_lock = threading.Lock()
    frames_deque: deque = deque([])

    async def queued_audio_frames_callback(
        frames: List[av.AudioFrame],
    ) -> av.AudioFrame:
        with frames_deque_lock:
            frames_deque.extend(frames)

        # Return empty frames to be silent.
        new_frames = []
        for frame in frames:
            input_array = frame.to_ndarray()
            new_frame = av.AudioFrame.from_ndarray(
                np.zeros(input_array.shape, dtype=input_array.dtype),
                layout=frame.layout.name,
            )
            new_frame.sample_rate = frame.sample_rate
            new_frames.append(new_frame)

        return new_frames

    webrtc_ctx = webrtc_streamer(
        key="speech-to-text-w-video",
        mode=WebRtcMode.SENDRECV,
        queued_audio_frames_callback=queued_audio_frames_callback,
        rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
        media_stream_constraints={"video": True, "audio": True},
    )

    status_indicator = st.empty()

    if not webrtc_ctx.state.playing:
        return

    status_indicator.write("Loading...")
    text_output = st.empty()
    stream = None

    while True:
        if webrtc_ctx.state.playing:
            if stream is None:
                from deepspeech import Model

                model = Model(model_path)
                model.enableExternalScorer(lm_path)
                model.setScorerAlphaBeta(lm_alpha, lm_beta)
                model.setBeamWidth(beam)

                stream = model.createStream()

                status_indicator.write("Model loaded.")

            sound_chunk = pydub.AudioSegment.empty()

            audio_frames = []
            with frames_deque_lock:
                while len(frames_deque) > 0:
                    frame = frames_deque.popleft()
                    audio_frames.append(frame)

            if len(audio_frames) == 0:
                time.sleep(0.1)
                status_indicator.write("No frame arrived.")
                continue

            status_indicator.write("Running. Say something!")

            for audio_frame in audio_frames:
                sound = pydub.AudioSegment(
                    data=audio_frame.to_ndarray().tobytes(),
                    sample_width=audio_frame.format.bytes,
                    frame_rate=audio_frame.sample_rate,
                    channels=len(audio_frame.layout.channels),
                )
                sound_chunk += sound

            if len(sound_chunk) > 0:
                sound_chunk = sound_chunk.set_channels(1).set_frame_rate(
                    model.sampleRate()
                )
                buffer = np.array(sound_chunk.get_array_of_samples())
                stream.feedAudioContent(buffer)
                text = stream.intermediateDecode()
                text_output.markdown(f"**Text:** {text}")
        else:
            status_indicator.write("Stopped.")
            break

# a raccoon astronaut with the cosmos reflecting on the glass of his helmet dreaming of the stars
def add_bg_from_url():
    st.markdown(
         f"""
         <style>
         .stApp {{
             background-image: url("https://i.redd.it/zung2u9zryb91.png");
             background-attachment: fixed;
             background-size: cover
         }}
         </style>
         """,
         unsafe_allow_html=True
     )

#add_bg_from_url() 


if __name__ == "__main__":
    import os

    DEBUG = os.environ.get("DEBUG", "false").lower() not in ["false", "no", "0"]

    logging.basicConfig(
        format="[%(asctime)s] %(levelname)7s from %(name)s in %(pathname)s:%(lineno)d: "
        "%(message)s",
        force=True,
    )

    logger.setLevel(level=logging.DEBUG if DEBUG else logging.INFO)

    st_webrtc_logger = logging.getLogger("streamlit_webrtc")
    st_webrtc_logger.setLevel(logging.DEBUG)

    fsevents_logger = logging.getLogger("fsevents")
    fsevents_logger.setLevel(logging.WARNING)

    main()