File size: 5,125 Bytes
60494ed
 
 
 
 
230eb9d
60494ed
 
 
230eb9d
 
 
 
 
 
97c98c3
230eb9d
 
 
61a4214
230eb9d
 
 
61a4214
038c199
230eb9d
 
 
61a4214
038c199
230eb9d
61a4214
230eb9d
 
60494ed
97c98c3
60494ed
 
 
97c98c3
d308880
60494ed
d308880
5f358d0
d308880
 
4cee811
d308880
 
 
 
 
97c98c3
 
 
d308880
97c98c3
 
60494ed
885792f
 
038c199
 
 
 
 
 
 
60494ed
 
 
 
 
d308880
61a4214
97c98c3
 
60494ed
885792f
 
60494ed
038c199
 
60494ed
038c199
60494ed
 
 
 
 
038c199
97c98c3
 
60494ed
 
97c98c3
 
 
 
 
 
60494ed
 
 
 
 
5f358d0
 
97c98c3
 
 
 
 
60494ed
 
 
 
97c98c3
038c199
97c98c3
 
 
60494ed
 
 
 
 
 
 
 
 
97c98c3
60494ed
 
 
 
97c98c3
60494ed
97c98c3
60494ed
 
 
 
 
96d76e6
97c98c3
60494ed
 
 
 
 
96d76e6
97c98c3
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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 SOAP berdasarkan percakapan dokter dan pasien.
                            Ringkaskan dalam bentuk paragraf tanpa adanya bullet point dan gunakan 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 Faster 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
    return (
        raw_transcription,
        save_to_file(raw_transcription, "transcription_large.txt"),
        audio_file,
    )


def generate_soap_summary(transcription_text, selected_model):
    """Membuat ringkasan SOAP dari teks transkripsi menggunakan model yang dipilih."""
    template = """Harap buat ringkasan dalam format berikut:

    Subjective:
    ICD10:
    Objective:
    Assessment:
    Plan:
    ### Percakapan:
    {dialogue}

    ### Catatan SOAP:
    """

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


def detect_medical_tags(transcription_text, selected_model):
    """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 Faster Whisper Large"
) as app:
    gr.Markdown(
        "## Medical SOAP Summarization and Tag Detection with Faster Whisper Large"
    )

    with gr.Row():
        with gr.Column():
            audio_input = gr.Audio("microphone", type="filepath", label="🎙️ Rekam Suara")
            transcribe_button = gr.Button("🎧 Transkripsi dengan Whisper Large")
            soap_button = gr.Button("📝 Buat SOAP")
            tags_button = gr.Button("🏷️ Deteksi Saran Tags")
            transcription_edit_box = gr.Textbox(
                label="📄 Hasil Transkripsi (Faster Whisper Large) - Bisa Diedit",
                lines=3,
                interactive=True,
            )
            update_transcription_button = gr.Button("💾 Simpan Hasil Edit")

        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=[transcription_edit_box, download_transcription, download_audio],
    )

    # Tombol Simpan Hasil Edit
    update_transcription_button.click(
        lambda text: (text, save_to_file(text, "user_edited_transcription.txt")),
        inputs=[transcription_edit_box],
        outputs=[transcription_edit_box, download_transcription],
    )

    # Tombol SOAP
    soap_button.click(
        generate_soap_summary,
        inputs=[transcription_edit_box],
        outputs=[soap_output, download_soap],
    )

    # Tombol Tags
    tags_button.click(
        detect_medical_tags,
        inputs=[transcription_edit_box],
        outputs=[tags_output, download_tags],
    )

# Jalankan aplikasi
app.launch(share=True)