File size: 4,195 Bytes
60494ed
 
 
 
 
230eb9d
60494ed
 
 
230eb9d
 
 
 
 
 
97c98c3
230eb9d
 
 
61a4214
230eb9d
 
 
ec31743
230eb9d
 
 
61a4214
038c199
230eb9d
61a4214
230eb9d
 
60494ed
97c98c3
60494ed
 
 
97c98c3
d308880
ec31743
d308880
5f358d0
d308880
 
4cee811
d308880
 
 
 
 
ec31743
 
97c98c3
ec31743
d308880
ec31743
 
 
 
97c98c3
 
60494ed
ec31743
 
038c199
 
 
 
 
 
ec31743
60494ed
 
 
d308880
61a4214
97c98c3
 
60494ed
ec31743
885792f
60494ed
038c199
 
60494ed
038c199
60494ed
 
 
 
 
038c199
97c98c3
 
60494ed
 
97c98c3
ec31743
97c98c3
ec31743
60494ed
 
 
365476b
ec31743
60494ed
 
 
97c98c3
038c199
97c98c3
 
 
60494ed
 
 
 
 
 
 
 
 
ec31743
 
 
 
 
 
 
 
60494ed
 
 
4a1a17e
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
import os
import faster_whisper
import gradio as gr
from dotenv import load_dotenv
from huggingface_hub import InferenceClient
from groq import Groq

# Load API key dari .env
load_dotenv()
GROQ_API_KEY = os.getenv("GROQ_API_KEY")

if not GROQ_API_KEY:
    raise ValueError("GROQ API NOT FOUND!")
gclient = Groq(api_key=GROQ_API_KEY)


def chat_with_groq(message):
    """Handles conversation with Groq LLM."""
    response = gclient.chat.completions.create(
        model="gemma2-9b-it",
        messages=[
            {
                "role": "system",
                "content": """Anda adalah asisten medis yang membantu dokter dalam menyusun catatan medis dalam bentuk paragraf menggunakan bahasa Indonesia.""",
            },
            {"role": "user", "content": message},
        ],
        temperature=0.0,
        max_tokens=248,
    )
    return response.choices[0].message.content  # Extract response text


def save_to_file(content, filename):
    with open(filename, "w", encoding="utf-8") as file:
        file.write(content)
    return filename


def transcribe_audio(audio_file):
    """Transkripsi audio menggunakan Whisper tanpa koreksi model Hugging Face."""
    # segments, _ = model.transcribe(audio_file)
    # raw_transcription = " ".join(segment.text for segment in segments)
    with open(audio_file, "rb") as file:
        res = gclient.audio.transcriptions.create(
            file=(audio_file, file.read()),
            model="whisper-large-v3-turbo",
            language="id",
        )
        print(res)
        raw_transcription = res.text
        soap_output, download_soap = generate_soap_summary(raw_transcription)
        tags_output, download_tags = detect_medical_tags(raw_transcription)
    return (
        save_to_file(raw_transcription, "raw_transcription.txt"),
        audio_file,
        soap_output,
        download_soap,
        tags_output,
        download_tags,
    )


def generate_soap_summary(transcription_text):
    template = """Buat ringkasan SOAP berdasarkan percakapan dokter dan pasien dalam format berikut:

    Subjective:
    ICD10:
    Objective:
    Assessment:
    Plan:

    ### Percakapan:
    {dialogue}
    """

    soap = chat_with_groq(template.format(dialogue=transcription_text))
    return soap, save_to_file(soap, "soap_summary.txt")


def detect_medical_tags(transcription_text):
    """Mendeteksi tags Diagnosis, Obat, Hasil Lab, dan Radiologi menggunakan model yang dipilih."""
    template = """
    Identifikasi dan berikan saran dalam bahasa Indonesia tindakan logis selanjutnya dalam format:
    ICD10:
    Obat:
    Laboratorium:
    Radiologi:

    ### Percakapan:
    {dialogue}
    """
    tags = chat_with_groq(template.format(dialogue=transcription_text))
    return tags, save_to_file(tags, "medical_tags.txt")


# Antarmuka Gradio
with gr.Blocks(
    title="AI-based Medical SOAP Summarization and Tag Detection with Whisper Large"
) as app:
    gr.Markdown("## Medical SOAP Summarization and Tag Detection with Whisper Large")

    with gr.Row():
        with gr.Column():
            audio_input = gr.Audio("microphone", type="filepath", label="🎙️ Rekam Suara")
            transcribe_button = gr.Button("🎧 Tulis Rekam Medis")

        with gr.Column():
            soap_output = gr.Textbox(label="📃 Hasil SOAP", lines=10, interactive=False)
            tags_output = gr.Textbox(
                label="🏷️ Hasil  Saran Tags ICD 10, Obat, Laboratorium, Radiologi",
                lines=10,
                interactive=False,
            )
            download_audio = gr.File(label="⬇️ Download Rekaman")
            download_transcription = gr.File(label="⬇️ Download Transkripsi")
            download_soap = gr.File(label="⬇️ Download SOAP")
            download_tags = gr.File(label="⬇️ Download Tags")

    # Tombol Transkripsi
    transcribe_button.click(
        transcribe_audio,
        inputs=[audio_input],
        outputs=[
            download_transcription,
            download_audio,
            soap_output,
            download_soap,
            tags_output,
            download_tags,
        ],
    )

# Jalankan aplikasi
app.launch(share=True)