Spaces:
Running
Running
Commit
·
6ab2eb1
1
Parent(s):
365476b
try deploy goog
Browse files- .gitignore +2 -1
- app.py +72 -114
- requirements.txt +1 -3
.gitignore
CHANGED
@@ -1 +1,2 @@
|
|
1 |
-
.env
|
|
|
|
1 |
+
.env
|
2 |
+
__pycache__
|
app.py
CHANGED
@@ -1,68 +1,47 @@
|
|
1 |
import os
|
2 |
-
import faster_whisper
|
3 |
import gradio as gr
|
4 |
-
from
|
5 |
-
from
|
6 |
-
|
7 |
-
|
8 |
-
# Load API key dari .env
|
9 |
-
load_dotenv()
|
10 |
-
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
11 |
-
|
12 |
-
if not GROQ_API_KEY:
|
13 |
-
raise ValueError("GROQ API NOT FOUND!")
|
14 |
-
gclient = Groq(api_key=GROQ_API_KEY)
|
15 |
-
|
16 |
-
|
17 |
-
def chat_with_groq(message):
|
18 |
-
"""Handles conversation with Groq LLM."""
|
19 |
-
response = gclient.chat.completions.create(
|
20 |
-
model="gemma2-9b-it",
|
21 |
-
messages=[
|
22 |
-
{
|
23 |
-
"role": "system",
|
24 |
-
"content": """Anda adalah asisten medis yang membantu dokter dalam menyusun catatan medis dalam bentuk paragraf menggunakan bahasa Indonesia.""",
|
25 |
-
},
|
26 |
-
{"role": "user", "content": message},
|
27 |
-
],
|
28 |
-
temperature=0.0,
|
29 |
-
max_tokens=248,
|
30 |
-
)
|
31 |
-
return response.choices[0].message.content # Extract response text
|
32 |
|
33 |
|
34 |
-
def
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
def transcribe_audio(audio_file):
|
41 |
-
"""Transkripsi audio menggunakan Whisper tanpa koreksi model Hugging Face."""
|
42 |
-
# segments, _ = model.transcribe(audio_file)
|
43 |
-
# raw_transcription = " ".join(segment.text for segment in segments)
|
44 |
-
with open(audio_file, "rb") as file:
|
45 |
-
res = gclient.audio.transcriptions.create(
|
46 |
-
file=(audio_file, file.read()),
|
47 |
-
model="whisper-large-v3-turbo",
|
48 |
-
language="id",
|
49 |
-
)
|
50 |
-
print(res)
|
51 |
-
raw_transcription = res.text
|
52 |
-
soap_output, download_soap = generate_soap_summary(raw_transcription)
|
53 |
-
tags_output, download_tags = detect_medical_tags(raw_transcription)
|
54 |
-
return (
|
55 |
-
save_to_file(raw_transcription, "raw_transcription.txt"),
|
56 |
-
audio_file,
|
57 |
-
soap_output,
|
58 |
-
download_soap,
|
59 |
-
tags_output,
|
60 |
-
download_tags,
|
61 |
-
)
|
62 |
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
Subjective:
|
68 |
ICD10:
|
@@ -70,66 +49,45 @@ def generate_soap_summary(transcription_text):
|
|
70 |
Assessment:
|
71 |
Plan:
|
72 |
|
73 |
-
|
74 |
-
{dialogue}
|
75 |
-
"""
|
76 |
-
|
77 |
-
soap = chat_with_groq(template.format(dialogue=transcription_text))
|
78 |
-
return soap, save_to_file(soap, "soap_summary.txt")
|
79 |
-
|
80 |
-
|
81 |
-
def detect_medical_tags(transcription_text):
|
82 |
-
"""Mendeteksi tags Diagnosis, Obat, Hasil Lab, dan Radiologi menggunakan model yang dipilih."""
|
83 |
-
template = """
|
84 |
-
Identifikasi dan berikan saran dalam bahasa Indonesia tindakan logis selanjutnya dalam format:
|
85 |
ICD10:
|
86 |
Obat:
|
87 |
Laboratorium:
|
88 |
Radiologi:
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
with gr.Row():
|
104 |
-
with gr.Column():
|
105 |
-
audio_input = gr.Audio("microphone", type="filepath", label="🎙️ Rekam Suara")
|
106 |
-
transcribe_button = gr.Button("🎧 Tulis Rekam Medis")
|
107 |
-
|
108 |
-
with gr.Column():
|
109 |
-
soap_output = gr.Textbox(label="📃 Hasil SOAP", lines=10, interactive=False)
|
110 |
-
tags_output = gr.Textbox(
|
111 |
-
label="🏷️ Hasil Saran Tags ICD 10, Obat, Laboratorium, Radiologi",
|
112 |
-
lines=10,
|
113 |
-
interactive=False,
|
114 |
-
)
|
115 |
-
download_audio = gr.File(label="⬇️ Download Rekaman")
|
116 |
-
download_transcription = gr.File(label="⬇️ Download Transkripsi")
|
117 |
-
download_soap = gr.File(label="⬇️ Download SOAP")
|
118 |
-
download_tags = gr.File(label="⬇️ Download Tags")
|
119 |
-
|
120 |
-
# Tombol Transkripsi
|
121 |
-
transcribe_button.click(
|
122 |
-
transcribe_audio,
|
123 |
-
inputs=[audio_input],
|
124 |
-
outputs=[
|
125 |
-
download_transcription,
|
126 |
-
download_audio,
|
127 |
-
soap_output,
|
128 |
-
download_soap,
|
129 |
-
tags_output,
|
130 |
-
download_tags,
|
131 |
-
],
|
132 |
)
|
133 |
|
134 |
-
#
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
+
from google import genai
|
4 |
+
from google.genai import types
|
5 |
+
import asyncio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
|
8 |
+
def audio_from_bytes(audio_file_path: str):
|
9 |
+
"""Converts an audio file into Gemini-compatible format."""
|
10 |
+
try:
|
11 |
+
with open(audio_file_path, "rb") as f:
|
12 |
+
audio_data = f.read()
|
13 |
|
14 |
+
mime_type = "audio/mp3" # Adjust based on your audio type
|
15 |
+
return types.Part.from_bytes(data=audio_data, mime_type=mime_type)
|
16 |
+
|
17 |
+
except FileNotFoundError:
|
18 |
+
return "Error: Audio file not found!"
|
19 |
+
except Exception as e:
|
20 |
+
return f"An error occurred: {e}"
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
def transcribe_and_summarize(audio_file):
|
24 |
+
"""Processes audio with Gemini API and returns a SOAP summary."""
|
25 |
+
if audio_file is None:
|
26 |
+
return "No audio file uploaded."
|
27 |
|
28 |
+
# Ensure API Key is set
|
29 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
30 |
+
if not GEMINI_API_KEY:
|
31 |
+
return "Error: GEMINI_API_KEY environment variable is missing."
|
32 |
+
asyncio.set_event_loop(asyncio.new_event_loop())
|
33 |
+
|
34 |
+
client = genai.Client(api_key=GEMINI_API_KEY)
|
35 |
+
model = "gemini-2.0-flash"
|
36 |
+
|
37 |
+
# Prepare the request
|
38 |
+
contents = [
|
39 |
+
types.Content(
|
40 |
+
role="user",
|
41 |
+
parts=[
|
42 |
+
types.Part.from_text(
|
43 |
+
text="""Anda adalah asisten medis yang membantu dokter dalam menyusun catatan medis dalam bentuk paragraf menggunakan bahasa Indonesia.
|
44 |
+
Buat ringkasan SOAP berdasarkan percakapan dokter dan pasien dalam format berikut:
|
45 |
|
46 |
Subjective:
|
47 |
ICD10:
|
|
|
49 |
Assessment:
|
50 |
Plan:
|
51 |
|
52 |
+
Identifikasi dan berikan saran dalam bahasa Indonesia tindakan logis selanjutnya dalam format:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
ICD10:
|
54 |
Obat:
|
55 |
Laboratorium:
|
56 |
Radiologi:
|
57 |
+
"""
|
58 |
+
),
|
59 |
+
audio_from_bytes(audio_file),
|
60 |
+
],
|
61 |
+
)
|
62 |
+
]
|
63 |
+
|
64 |
+
generate_content_config = types.GenerateContentConfig(
|
65 |
+
temperature=0,
|
66 |
+
top_p=0.95,
|
67 |
+
top_k=40,
|
68 |
+
max_output_tokens=8192,
|
69 |
+
response_mime_type="text/plain",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
)
|
71 |
|
72 |
+
# Process the audio
|
73 |
+
response_text = ""
|
74 |
+
for chunk in client.models.generate_content_stream(
|
75 |
+
model=model,
|
76 |
+
contents=contents,
|
77 |
+
config=generate_content_config,
|
78 |
+
):
|
79 |
+
response_text += chunk.text
|
80 |
+
|
81 |
+
return response_text
|
82 |
+
|
83 |
+
|
84 |
+
# Gradio UI
|
85 |
+
demo = gr.Interface(
|
86 |
+
fn=transcribe_and_summarize,
|
87 |
+
inputs=gr.Audio(type="filepath"),
|
88 |
+
outputs="text",
|
89 |
+
title="eH Medical Transcription",
|
90 |
+
description="Upload an Indonesian audio file of a doctor-patient conversation and get a SOAP summary.",
|
91 |
+
)
|
92 |
+
|
93 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
faster_whisper==1.1.1
|
2 |
gradio==5.17.0
|
3 |
-
huggingface_hub==0.28.1
|
4 |
python-dotenv==1.0.1
|
5 |
-
|
|
|
|
|
1 |
gradio==5.17.0
|
|
|
2 |
python-dotenv==1.0.1
|
3 |
+
google-genai
|