Spaces:
Sleeping
Sleeping
last commit gradio
Browse files
app.py
CHANGED
|
@@ -1,135 +1,75 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
from
|
| 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 |
-
summarizer = LsaSummarizer()
|
| 35 |
-
summary_sentences = summarizer(parser.document, 5)
|
| 36 |
-
summarized_text = " ".join([str(sentence) for sentence in summary_sentences])
|
| 37 |
-
|
| 38 |
-
# Token info
|
| 39 |
-
original_tokens = len(nltk.word_tokenize(transcription))
|
| 40 |
-
summarized_tokens = len(nltk.word_tokenize(summarized_text))
|
| 41 |
-
token_info = f"Asli: {original_tokens} token | Ringkasan: {summarized_tokens} token"
|
| 42 |
-
|
| 43 |
-
# SOAP
|
| 44 |
-
prompt_soap = f"""
|
| 45 |
-
Anda adalah asisten medis yang membantu dokter dalam menyusun catatan SOAP berdasarkan percakapan dokter dan pasien.
|
| 46 |
-
Ringkaskan dalam bentuk paragraf tanpa adanya bullet point dan gunakan bahasa Indonesia.
|
| 47 |
-
Harap buat ringkasan dalam format berikut:
|
| 48 |
-
Subjective:
|
| 49 |
-
Objective:
|
| 50 |
-
Assessment:
|
| 51 |
-
Plan:
|
| 52 |
-
|
| 53 |
-
### Percakapan:
|
| 54 |
-
{transcription}
|
| 55 |
-
|
| 56 |
-
Tolong jangan tambahkan informasi tambahan selain yang berkaitan dengan diagnosis, obat, hasil lab, dan radiologi.
|
| 57 |
-
"""
|
| 58 |
-
response_soap = groq_client.chat.completions.create(
|
| 59 |
-
model="llama3-8b-8192",
|
| 60 |
-
messages=[{"role": "user", "content": prompt_soap}]
|
| 61 |
)
|
| 62 |
-
soap_content = response_soap.choices[0].message.content
|
| 63 |
-
|
| 64 |
-
# Tags
|
| 65 |
-
prompt_tags = f"""
|
| 66 |
-
Identifikasi dan berikan luaran dalam bahasa Indonesia tags berikut dari percakapan dengan format:
|
| 67 |
-
Diagnosis:
|
| 68 |
-
Obat:
|
| 69 |
-
Hasil Lab:
|
| 70 |
-
Radiologi:
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
messages=[{"role": "user", "content": prompt_tags}]
|
| 80 |
)
|
| 81 |
-
tags_content = response_tags.choices[0].message.content
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
tags_file = save_to_file(tags_content, 'medical_tags.txt')
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
soap_content,
|
| 91 |
-
tags_content,
|
| 92 |
-
token_info,
|
| 93 |
-
summarized_file,
|
| 94 |
-
soap_file,
|
| 95 |
-
tags_file,
|
| 96 |
-
audio_path
|
| 97 |
-
)
|
| 98 |
|
| 99 |
-
#
|
| 100 |
-
|
| 101 |
-
gr.
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
transcribe_button = gr.Button("π©Ί Jalankan Proses Lengkap (Transkripsi + Ringkasan + SOAP + Tags)")
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
with gr.Row():
|
| 115 |
-
download_summary = gr.File(label="β¬οΈ Download Ringkasan LSA")
|
| 116 |
-
download_soap = gr.File(label="β¬οΈ Download SOAP")
|
| 117 |
-
download_tags = gr.File(label="β¬οΈ Download Tags")
|
| 118 |
-
download_audio = gr.File(label="β¬οΈ Download Audio")
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
inputs=
|
| 123 |
-
outputs=[
|
| 124 |
-
|
| 125 |
-
soap_box,
|
| 126 |
-
tags_box,
|
| 127 |
-
token_box,
|
| 128 |
-
download_summary,
|
| 129 |
-
download_soap,
|
| 130 |
-
download_tags,
|
| 131 |
-
download_audio
|
| 132 |
-
]
|
| 133 |
)
|
| 134 |
|
| 135 |
-
app.launch(
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
API_TRANSCRIBE = os.getenv("API_TRANSCRIBE")
|
| 8 |
+
API_TEXT = os.getenv("API_TEXT")
|
| 9 |
+
|
| 10 |
+
# ==== Function for backend calls ====
|
| 11 |
+
def handle_audio(audio_file):
|
| 12 |
+
if audio_file is None:
|
| 13 |
+
return "-", "-", "-"
|
| 14 |
+
with open(audio_file, "rb") as f:
|
| 15 |
+
files = {"audio": f}
|
| 16 |
+
response = requests.post(API_TRANSCRIBE, files=files)
|
| 17 |
+
result = response.json()
|
| 18 |
+
return result.get("transcription", "-"), result.get("soap_content", "-"), result.get("tags_content", "-")
|
| 19 |
+
|
| 20 |
+
def handle_text(dialogue):
|
| 21 |
+
if not dialogue.strip():
|
| 22 |
+
return "-", "-", "-"
|
| 23 |
+
response = requests.post(API_TEXT, json={"dialogue": dialogue})
|
| 24 |
+
result = response.json()
|
| 25 |
+
return dialogue, result.get("soap_content", "-"), result.get("tags_content", "-")
|
| 26 |
+
|
| 27 |
+
# ==== Function to toggle inputs ====
|
| 28 |
+
def toggle_inputs(choice):
|
| 29 |
+
return (
|
| 30 |
+
gr.update(visible=(choice == "Upload Audio")),
|
| 31 |
+
gr.update(visible=(choice == "Rekam Audio")),
|
| 32 |
+
gr.update(visible=(choice == "Input Teks")),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# ==== UI ====
|
| 36 |
+
with gr.Blocks(title="SOAP AI Dropdown Input") as app:
|
| 37 |
+
gr.Markdown("## π©Ί SOAP AI β Pilih Jenis Input")
|
| 38 |
|
| 39 |
+
input_choice = gr.Dropdown(
|
| 40 |
+
choices=["Upload Audio", "Input Teks"],
|
| 41 |
+
value="Upload Audio",
|
| 42 |
+
label="Pilih Metode Input"
|
|
|
|
| 43 |
)
|
|
|
|
| 44 |
|
| 45 |
+
# Input fields (hidden by default except selected)
|
| 46 |
+
audio_upload = gr.Audio("microphone",label="π Upload File Audio", type="filepath", visible=True)
|
| 47 |
+
text_input = gr.Textbox(label="π Masukkan Percakapan Dokter-Pasien", lines=6, visible=False)
|
|
|
|
| 48 |
|
| 49 |
+
# Tombol proses
|
| 50 |
+
process_button = gr.Button("π Proses ke SOAP")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# Output
|
| 53 |
+
transcript_output = gr.Textbox(label="π Hasil Transkripsi", lines=3)
|
| 54 |
+
soap_output = gr.Textbox(label="π Ringkasan SOAP", lines=6)
|
| 55 |
+
tags_output = gr.Textbox(label="π·οΈ Medical Tags", lines=6)
|
| 56 |
|
| 57 |
+
# === Events ===
|
| 58 |
+
input_choice.change(fn=toggle_inputs, inputs=input_choice,
|
| 59 |
+
outputs=[audio_upload, text_input])
|
|
|
|
| 60 |
|
| 61 |
+
process_button.click(
|
| 62 |
+
fn=handle_audio,
|
| 63 |
+
inputs=audio_upload,
|
| 64 |
+
outputs=[transcript_output, soap_output, tags_output],
|
| 65 |
+
show_progress="minimal"
|
| 66 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
process_button.click(
|
| 69 |
+
fn=handle_text,
|
| 70 |
+
inputs=text_input,
|
| 71 |
+
outputs=[transcript_output, soap_output, tags_output],
|
| 72 |
+
show_progress="minimal"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
)
|
| 74 |
|
| 75 |
+
app.launch()
|