Spaces:
Running
Running
Commit
Β·
97c98c3
1
Parent(s):
885792f
try new mistral
Browse files
app.py
CHANGED
@@ -9,7 +9,9 @@ load_dotenv()
|
|
9 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
10 |
|
11 |
if not HF_API_KEY:
|
12 |
-
raise ValueError(
|
|
|
|
|
13 |
|
14 |
# Inisialisasi klien API Hugging Face
|
15 |
huggingface_client = InferenceClient(api_key=HF_API_KEY)
|
@@ -19,22 +21,29 @@ model = faster_whisper.WhisperModel("turbo", device="cpu", compute_type="int8")
|
|
19 |
|
20 |
# Daftar model yang dapat dipilih
|
21 |
MODEL_OPTIONS = [
|
|
|
22 |
"mistralai/Mistral-7B-Instruct-v0.3",
|
23 |
"deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
|
24 |
-
"
|
25 |
-
"Qwen/Qwen2.5-Coder-32B-Instruct"
|
26 |
]
|
27 |
|
|
|
28 |
def save_to_file(content, filename):
|
29 |
-
with open(filename,
|
30 |
file.write(content)
|
31 |
return filename
|
32 |
|
|
|
33 |
def transcribe_audio(audio_path):
|
34 |
"""Transkripsi audio menggunakan Faster Whisper tanpa koreksi model Hugging Face."""
|
35 |
segments, _ = model.transcribe(audio_path)
|
36 |
raw_transcription = " ".join(segment.text for segment in segments)
|
37 |
-
return
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
def generate_soap_summary(transcription_text, selected_model):
|
40 |
"""Membuat ringkasan SOAP dari teks transkripsi menggunakan model yang dipilih."""
|
@@ -53,15 +62,15 @@ def generate_soap_summary(transcription_text, selected_model):
|
|
53 |
|
54 |
### Catatan SOAP:
|
55 |
"""
|
56 |
-
messages = [
|
|
|
|
|
57 |
response = huggingface_client.chat.completions.create(
|
58 |
-
model=selected_model,
|
59 |
-
messages=messages,
|
60 |
-
max_tokens=1000,
|
61 |
-
stream=False
|
62 |
)
|
63 |
soap = response.choices[0].message.content.strip()
|
64 |
-
return soap, save_to_file(soap,
|
|
|
65 |
|
66 |
def detect_medical_tags(transcription_text, selected_model):
|
67 |
"""Mendeteksi tags Diagnosis, Obat, Hasil Lab, dan Radiologi menggunakan model yang dipilih."""
|
@@ -75,37 +84,49 @@ def detect_medical_tags(transcription_text, selected_model):
|
|
75 |
### Percakapan:
|
76 |
{dialogue}
|
77 |
"""
|
78 |
-
messages = [
|
|
|
|
|
79 |
response = huggingface_client.chat.completions.create(
|
80 |
-
model=selected_model,
|
81 |
-
messages=messages,
|
82 |
-
max_tokens=500,
|
83 |
-
stream=False
|
84 |
)
|
85 |
tags = response.choices[0].message.content.strip()
|
86 |
-
return tags, save_to_file(tags,
|
|
|
87 |
|
88 |
# Antarmuka Gradio
|
89 |
-
with gr.Blocks(
|
90 |
-
|
|
|
|
|
|
|
|
|
91 |
|
92 |
with gr.Row():
|
93 |
with gr.Column():
|
94 |
model_selector = gr.Dropdown(
|
95 |
choices=MODEL_OPTIONS,
|
96 |
-
value="mistralai/
|
97 |
-
label="π Pilih Model AI"
|
98 |
)
|
99 |
audio_input = gr.Audio("microphone", type="filepath", label="ποΈ Rekam Suara")
|
100 |
transcribe_button = gr.Button("π§ Transkripsi dengan Whisper Large")
|
101 |
-
transcription_edit_box = gr.Textbox(
|
|
|
|
|
|
|
|
|
102 |
update_transcription_button = gr.Button("πΎ Simpan Hasil Edit")
|
103 |
soap_button = gr.Button("π Buat SOAP")
|
104 |
tags_button = gr.Button("π·οΈ Deteksi Tags")
|
105 |
|
106 |
with gr.Column():
|
107 |
soap_output = gr.Textbox(label="π Hasil SOAP", lines=10, interactive=False)
|
108 |
-
tags_output = gr.Textbox(
|
|
|
|
|
|
|
|
|
109 |
download_audio = gr.File(label="β¬οΈ Download Rekaman")
|
110 |
download_transcription = gr.File(label="β¬οΈ Download Transkripsi")
|
111 |
download_soap = gr.File(label="β¬οΈ Download SOAP")
|
@@ -115,28 +136,28 @@ with gr.Blocks(title="AI-based Medical SOAP Summarization and Tag Detection with
|
|
115 |
transcribe_button.click(
|
116 |
transcribe_audio,
|
117 |
inputs=[audio_input],
|
118 |
-
outputs=[transcription_edit_box, download_transcription, download_audio]
|
119 |
)
|
120 |
|
121 |
# Tombol Simpan Hasil Edit
|
122 |
update_transcription_button.click(
|
123 |
-
lambda text: (text, save_to_file(text,
|
124 |
inputs=[transcription_edit_box],
|
125 |
-
outputs=[transcription_edit_box, download_transcription]
|
126 |
)
|
127 |
|
128 |
# Tombol SOAP
|
129 |
soap_button.click(
|
130 |
generate_soap_summary,
|
131 |
inputs=[transcription_edit_box, model_selector],
|
132 |
-
outputs=[soap_output, download_soap]
|
133 |
)
|
134 |
|
135 |
# Tombol Tags
|
136 |
tags_button.click(
|
137 |
detect_medical_tags,
|
138 |
inputs=[transcription_edit_box, model_selector],
|
139 |
-
outputs=[tags_output, download_tags]
|
140 |
)
|
141 |
|
142 |
# Jalankan aplikasi
|
|
|
9 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
10 |
|
11 |
if not HF_API_KEY:
|
12 |
+
raise ValueError(
|
13 |
+
"API Key Hugging Face tidak ditemukan. Pastikan file .env berisi HF_API_KEY."
|
14 |
+
)
|
15 |
|
16 |
# Inisialisasi klien API Hugging Face
|
17 |
huggingface_client = InferenceClient(api_key=HF_API_KEY)
|
|
|
21 |
|
22 |
# Daftar model yang dapat dipilih
|
23 |
MODEL_OPTIONS = [
|
24 |
+
"mistralai/Mistral-Small-3.1-24B-Instruct-2503",
|
25 |
"mistralai/Mistral-7B-Instruct-v0.3",
|
26 |
"deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
|
27 |
+
"Qwen/Qwen2.5-Coder-32B-Instruct",
|
|
|
28 |
]
|
29 |
|
30 |
+
|
31 |
def save_to_file(content, filename):
|
32 |
+
with open(filename, "w", encoding="utf-8") as file:
|
33 |
file.write(content)
|
34 |
return filename
|
35 |
|
36 |
+
|
37 |
def transcribe_audio(audio_path):
|
38 |
"""Transkripsi audio menggunakan Faster Whisper tanpa koreksi model Hugging Face."""
|
39 |
segments, _ = model.transcribe(audio_path)
|
40 |
raw_transcription = " ".join(segment.text for segment in segments)
|
41 |
+
return (
|
42 |
+
raw_transcription,
|
43 |
+
save_to_file(raw_transcription, "transcription_large.txt"),
|
44 |
+
audio_path,
|
45 |
+
)
|
46 |
+
|
47 |
|
48 |
def generate_soap_summary(transcription_text, selected_model):
|
49 |
"""Membuat ringkasan SOAP dari teks transkripsi menggunakan model yang dipilih."""
|
|
|
62 |
|
63 |
### Catatan SOAP:
|
64 |
"""
|
65 |
+
messages = [
|
66 |
+
{"role": "user", "content": template.format(dialogue=transcription_text)}
|
67 |
+
]
|
68 |
response = huggingface_client.chat.completions.create(
|
69 |
+
model=selected_model, messages=messages, max_tokens=1000, stream=False
|
|
|
|
|
|
|
70 |
)
|
71 |
soap = response.choices[0].message.content.strip()
|
72 |
+
return soap, save_to_file(soap, "soap_summary.txt")
|
73 |
+
|
74 |
|
75 |
def detect_medical_tags(transcription_text, selected_model):
|
76 |
"""Mendeteksi tags Diagnosis, Obat, Hasil Lab, dan Radiologi menggunakan model yang dipilih."""
|
|
|
84 |
### Percakapan:
|
85 |
{dialogue}
|
86 |
"""
|
87 |
+
messages = [
|
88 |
+
{"role": "user", "content": template.format(dialogue=transcription_text)}
|
89 |
+
]
|
90 |
response = huggingface_client.chat.completions.create(
|
91 |
+
model=selected_model, messages=messages, max_tokens=500, stream=False
|
|
|
|
|
|
|
92 |
)
|
93 |
tags = response.choices[0].message.content.strip()
|
94 |
+
return tags, save_to_file(tags, "medical_tags.txt")
|
95 |
+
|
96 |
|
97 |
# Antarmuka Gradio
|
98 |
+
with gr.Blocks(
|
99 |
+
title="AI-based Medical SOAP Summarization and Tag Detection with Faster Whisper Large"
|
100 |
+
) as app:
|
101 |
+
gr.Markdown(
|
102 |
+
"## Medical SOAP Summarization and Tag Detection with Faster Whisper Large"
|
103 |
+
)
|
104 |
|
105 |
with gr.Row():
|
106 |
with gr.Column():
|
107 |
model_selector = gr.Dropdown(
|
108 |
choices=MODEL_OPTIONS,
|
109 |
+
value="mistralai/Mistral-Small-3.1-24B-Instruct-2503",
|
110 |
+
label="π Pilih Model AI",
|
111 |
)
|
112 |
audio_input = gr.Audio("microphone", type="filepath", label="ποΈ Rekam Suara")
|
113 |
transcribe_button = gr.Button("π§ Transkripsi dengan Whisper Large")
|
114 |
+
transcription_edit_box = gr.Textbox(
|
115 |
+
label="π Hasil Transkripsi (Faster Whisper Large) - Bisa Diedit",
|
116 |
+
lines=3,
|
117 |
+
interactive=True,
|
118 |
+
)
|
119 |
update_transcription_button = gr.Button("πΎ Simpan Hasil Edit")
|
120 |
soap_button = gr.Button("π Buat SOAP")
|
121 |
tags_button = gr.Button("π·οΈ Deteksi Tags")
|
122 |
|
123 |
with gr.Column():
|
124 |
soap_output = gr.Textbox(label="π Hasil SOAP", lines=10, interactive=False)
|
125 |
+
tags_output = gr.Textbox(
|
126 |
+
label="π·οΈ Hasil Tags Diagnosis, Obat, Hasil Lab, Radiologi",
|
127 |
+
lines=10,
|
128 |
+
interactive=False,
|
129 |
+
)
|
130 |
download_audio = gr.File(label="β¬οΈ Download Rekaman")
|
131 |
download_transcription = gr.File(label="β¬οΈ Download Transkripsi")
|
132 |
download_soap = gr.File(label="β¬οΈ Download SOAP")
|
|
|
136 |
transcribe_button.click(
|
137 |
transcribe_audio,
|
138 |
inputs=[audio_input],
|
139 |
+
outputs=[transcription_edit_box, download_transcription, download_audio],
|
140 |
)
|
141 |
|
142 |
# Tombol Simpan Hasil Edit
|
143 |
update_transcription_button.click(
|
144 |
+
lambda text: (text, save_to_file(text, "user_edited_transcription.txt")),
|
145 |
inputs=[transcription_edit_box],
|
146 |
+
outputs=[transcription_edit_box, download_transcription],
|
147 |
)
|
148 |
|
149 |
# Tombol SOAP
|
150 |
soap_button.click(
|
151 |
generate_soap_summary,
|
152 |
inputs=[transcription_edit_box, model_selector],
|
153 |
+
outputs=[soap_output, download_soap],
|
154 |
)
|
155 |
|
156 |
# Tombol Tags
|
157 |
tags_button.click(
|
158 |
detect_medical_tags,
|
159 |
inputs=[transcription_edit_box, model_selector],
|
160 |
+
outputs=[tags_output, download_tags],
|
161 |
)
|
162 |
|
163 |
# Jalankan aplikasi
|
ya.txt
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
halo
|
|
|
|