syafiqq02 commited on
Commit
60494ed
·
1 Parent(s): 82c41ee

gradio file

Browse files
Files changed (1) hide show
  1. app.py +130 -0
app.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import faster_whisper
3
+ import gradio as gr
4
+ from dotenv import load_dotenv
5
+ from huggingface_hub import InferenceClient
6
+
7
+ # Load API key dari .env
8
+ load_dotenv()
9
+ HF_API_KEY = os.getenv("HF_API_KEY")
10
+
11
+ if not HF_API_KEY:
12
+ raise ValueError("API Key Hugging Face tidak ditemukan. Pastikan file .env berisi HF_API_KEY.")
13
+
14
+ # Inisialisasi klien API Hugging Face
15
+ huggingface_client = InferenceClient(api_key=HF_API_KEY)
16
+
17
+ # Load Faster Whisper model versi large
18
+ model = faster_whisper.WhisperModel("turbo", device="cpu", compute_type="int8")
19
+
20
+ def save_to_file(content, filename):
21
+ with open(filename, 'w', encoding='utf-8') as file:
22
+ file.write(content)
23
+ return filename
24
+
25
+ def transcribe_audio(audio_path):
26
+ """Transkripsi audio menggunakan Faster Whisper tanpa koreksi model Hugging Face."""
27
+ segments, _ = model.transcribe(audio_path)
28
+ raw_transcription = " ".join(segment.text for segment in segments)
29
+ return raw_transcription, save_to_file(raw_transcription, 'transcription_large.txt'), audio_path
30
+
31
+ def generate_soap_summary(transcription_text):
32
+ """Membuat ringkasan SOAP dari teks transkripsi."""
33
+ template = """
34
+ Anda adalah asisten medis yang membantu dokter dalam menyusun catatan SOAP berdasarkan percakapan dokter dan pasien.
35
+ Ringkaskan dalam bentuk paragraf tanpa adanya bullet point dan gunakan bahasa Indonesia.
36
+ Harap buat ringkasan dalam format berikut:
37
+
38
+ Subjective:
39
+ Objective:
40
+ Assessment:
41
+ Plan:
42
+
43
+ ### Percakapan:
44
+ {dialogue}
45
+
46
+ ### Catatan SOAP:
47
+ """
48
+ messages = [{"role": "user", "content": template.format(dialogue=transcription_text)}]
49
+ response = huggingface_client.chat.completions.create(
50
+ model="mistralai/Mixtral-8x7B-Instruct-v0.1",
51
+ messages=messages,
52
+ max_tokens=1000,
53
+ stream=False
54
+ )
55
+ soap = response.choices[0].message.content.strip()
56
+ return soap, save_to_file(soap, 'soap_summary.txt')
57
+
58
+ def detect_medical_tags(transcription_text):
59
+ """Mendeteksi tags Diagnosis, Obat, Hasil Lab, dan Radiologi."""
60
+ template = """
61
+ Identifikasi dan berikan luaran dalam bahasa indonesia tags berikut dari percakapan:
62
+ Diagnosis:
63
+ Obat:
64
+ Hasil Lab:
65
+ Radiologi:
66
+
67
+ ### Percakapan:
68
+ {dialogue}
69
+ """
70
+ messages = [{"role": "user", "content": template.format(dialogue=transcription_text)}]
71
+ response = huggingface_client.chat.completions.create(
72
+ model="mistralai/Mixtral-8x7B-Instruct-v0.1",
73
+ messages=messages,
74
+ max_tokens=500,
75
+ stream=False
76
+ )
77
+ tags = response.choices[0].message.content.strip()
78
+ return tags, save_to_file(tags, 'medical_tags.txt')
79
+
80
+ # Antarmuka Gradio
81
+ with gr.Blocks(title="AI-based Medical SOAP Summarization and Tag Detection with Faster Whisper Large") as app:
82
+ gr.Markdown("## Medical SOAP Summarization and Tag Detection with Faster Whisper Large")
83
+
84
+ with gr.Row():
85
+ with gr.Column():
86
+ audio_input = gr.Audio("microphone", type="filepath", label="🎙️ Rekam Suara")
87
+ transcribe_button = gr.Button("🎧 Transkripsi dengan Whisper Large")
88
+ transcription_edit_box = gr.Textbox(label="📄 Hasil Transkripsi (Faster Whisper Large) - Bisa Diedit", lines=12, interactive=True)
89
+ update_transcription_button = gr.Button("💾 Simpan Hasil Edit")
90
+ soap_button = gr.Button("📝 Buat SOAP")
91
+ tags_button = gr.Button("🏷️ Deteksi Tags")
92
+
93
+ with gr.Column():
94
+ soap_output = gr.Textbox(label="📃 Hasil SOAP", lines=10, interactive=False)
95
+ tags_output = gr.Textbox(label="🏷️ Hasil Tags Diagnosis, Obat, Hasil Lab, Radiologi", lines=10, interactive=False)
96
+ download_audio = gr.File(label="⬇️ Download Rekaman")
97
+ download_transcription = gr.File(label="⬇️ Download Transkripsi")
98
+ download_soap = gr.File(label="⬇️ Download SOAP")
99
+ download_tags = gr.File(label="⬇️ Download Tags")
100
+
101
+ # Tombol Transkripsi
102
+ transcribe_button.click(
103
+ transcribe_audio,
104
+ inputs=[audio_input],
105
+ outputs=[transcription_edit_box, download_transcription, download_audio]
106
+ )
107
+
108
+ # Tombol Simpan Hasil Edit
109
+ update_transcription_button.click(
110
+ lambda text: (text, save_to_file(text, 'user_edited_transcription.txt')),
111
+ inputs=[transcription_edit_box],
112
+ outputs=[transcription_edit_box, download_transcription]
113
+ )
114
+
115
+ # Tombol SOAP
116
+ soap_button.click(
117
+ generate_soap_summary,
118
+ inputs=[transcription_edit_box],
119
+ outputs=[soap_output, download_soap]
120
+ )
121
+
122
+ # Tombol Tags
123
+ tags_button.click(
124
+ detect_medical_tags,
125
+ inputs=[transcription_edit_box],
126
+ outputs=[tags_output, download_tags]
127
+ )
128
+
129
+ # Jalankan aplikasi
130
+ app.launch()