File size: 4,418 Bytes
49e7846
 
 
 
 
 
 
 
 
 
 
 
b55b713
49e7846
 
 
 
 
 
 
 
b55b713
49e7846
 
 
 
b55b713
 
 
49e7846
b55b713
 
 
 
 
 
49e7846
 
b55b713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49e7846
 
 
 
b55b713
 
 
 
 
 
 
 
49e7846
 
b55b713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49e7846
 
 
 
 
 
 
 
 
 
b55b713
 
 
 
 
 
49e7846
b55b713
 
 
 
49e7846
 
b55b713
49e7846
b55b713
 
49e7846
 
 
 
b55b713
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
import gradio as gr
import os
import requests
from dotenv import load_dotenv

load_dotenv()
API_TRANSCRIBE = os.getenv("API_TRANSCRIBE")
API_TEXT = os.getenv("API_TEXT")

# ==== Function for backend calls ====
def handle_audio(audio_file):
    if audio_file is None:
        return "", "", ""
    with open(audio_file, "rb") as f:
        files = {"audio": f}
        response = requests.post(API_TRANSCRIBE, files=files)
        result = response.json()
    return result.get("transcription", "-"), result.get("soap_content", "-"), result.get("tags_content", "-")

def handle_text(dialogue):
    if not dialogue.strip():
        return "", "", ""
    response = requests.post(API_TEXT, json={"dialogue": dialogue})
    result = response.json()
    return dialogue, result.get("soap_content", "-"), result.get("tags_content", "-")

# ==== Function to toggle inputs with refresh ====
def toggle_inputs_with_refresh(choice):
    # Clear semua output saat dropdown berubah
    return (
        gr.update(visible=(choice == "Upload Audio"), value=None),  # Clear audio upload
        gr.update(visible=(choice == "Realtime Recording"), value=None),  # Clear audio record
        gr.update(visible=(choice == "Input Teks"), value=""),  # Clear text input
        gr.update(value=""),  # Clear transcript output
        gr.update(value=""),  # Clear SOAP output
        gr.update(value="")   # Clear tags output
    )

# ==== Function to clear all data ====
def clear_all_data():
    return (
        gr.update(value=None),  # Clear audio upload
        gr.update(value=None),  # Clear audio record
        gr.update(value=""),    # Clear text input
        gr.update(value=""),    # Clear transcript output
        gr.update(value=""),    # Clear SOAP output
        gr.update(value="")     # Clear tags output
    )

# ==== Combined processing function ====
def process_data(choice, audio_upload, audio_record, text_input):
    if choice == "Upload Audio":
        return handle_audio(audio_upload)
    elif choice == "Realtime Recording":
        return handle_audio(audio_record)
    elif choice == "Input Teks":
        return handle_text(text_input)
    else:
        return "", "", ""

# ==== UI ====
with gr.Blocks(title="SOAP AI Dropdown Input") as app:
    gr.Markdown("## 🩺 SOAP AI β€” Pilih Jenis Input")

    with gr.Row():
        input_choice = gr.Dropdown(
            choices=["Upload Audio", "Realtime Recording", "Input Teks"],
            value="Upload Audio",
            label="Pilih Metode Input",
            scale=4
        )
        clear_button = gr.Button("πŸ—‘οΈClear all", variant="secondary", size="sm", scale=0.5, min_width=25)

    # Input fields (hidden by default except selected)
    # Upload Audio - seperti gambar 1 (drag & drop + upload button)
    audio_upload = gr.Audio(
        sources=["upload"], 
        label="πŸ”Š Upload File Audio", 
        type="filepath", 
        visible=True
    )
    
    # Realtime Recording - seperti gambar 2 (tombol record + microphone selector)
    audio_record = gr.Audio(
        sources=["microphone"], 
        label="πŸ”Š Realtime Recording", 
        type="filepath", 
        visible=False
    )
    
    # Text Input
    text_input = gr.Textbox(
        label="πŸ“ Masukkan Percakapan Dokter-Pasien", 
        lines=6, 
        visible=False
    )

    # Tombol proses
    process_button = gr.Button("πŸš€ Proses ke SOAP")

    # Output
    transcript_output = gr.Textbox(label="πŸ“ Hasil Transkripsi", lines=3)
    soap_output = gr.Textbox(label="πŸ“‹ Ringkasan SOAP", lines=6)
    tags_output = gr.Textbox(label="🏷️ Medical Tags", lines=6)

    # === Events ===
    # Event untuk dropdown change - akan clear semua data
    input_choice.change(
        fn=toggle_inputs_with_refresh, 
        inputs=input_choice,
        outputs=[audio_upload, audio_record, text_input, transcript_output, soap_output, tags_output]
    )

    # Event untuk tombol clear - akan clear semua data
    clear_button.click(
        fn=clear_all_data,
        outputs=[audio_upload, audio_record, text_input, transcript_output, soap_output, tags_output]
    )

    # Single event handler untuk semua jenis input
    process_button.click(
        fn=process_data,
        inputs=[input_choice, audio_upload, audio_record, text_input],
        outputs=[transcript_output, soap_output, tags_output],
        show_progress="minimal"
    )

app.launch()