File size: 24,133 Bytes
49e7846
4bfa09f
49e7846
 
4bfa09f
 
 
 
 
 
 
49e7846
 
4bfa09f
 
 
 
 
 
 
 
49e7846
 
 
 
4bfa09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49e7846
4bfa09f
49e7846
4bfa09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49e7846
 
4bfa09f
49e7846
4bfa09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49e7846
b55b713
4bfa09f
49e7846
4bfa09f
 
 
 
 
 
 
 
 
 
 
49e7846
 
b55b713
 
4bfa09f
 
 
 
 
 
 
 
 
 
b55b713
 
 
4bfa09f
 
 
 
 
b55b713
4bfa09f
 
 
 
 
 
 
 
 
 
 
b55b713
4bfa09f
 
 
 
 
 
 
 
 
 
 
b55b713
4bfa09f
 
 
 
 
 
 
 
 
 
 
b55b713
4bfa09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b55b713
4bfa09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49e7846
b55b713
4bfa09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b55b713
4bfa09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b55b713
4bfa09f
 
 
 
 
 
b55b713
4bfa09f
 
 
 
 
 
 
 
 
 
 
 
 
 
b55b713
49e7846
4bfa09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49e7846
4bfa09f
 
 
 
 
 
49e7846
4bfa09f
b55b713
4bfa09f
 
 
 
 
 
 
 
 
 
 
 
b55b713
4bfa09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b55b713
49e7846
b55b713
 
4bfa09f
 
 
 
 
 
 
 
 
 
 
 
49e7846
 
 
b55b713
 
4bfa09f
 
 
 
 
 
 
 
 
49e7846
 
4bfa09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
import gradio as gr
import threading
import os
import requests
import string
import pygame
import time
from pydub import AudioSegment
from nltk.tokenize import word_tokenize
import nltk
from nltk.corpus import words, stopwords
from dotenv import load_dotenv

# Initialize pygame mixer for sound playback
pygame.mixer.init()

# Download resource NLTK (hanya sekali)
nltk.download('punkt')
nltk.download('words')
nltk.download('stopwords')

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

# Path ke file sound yang akan diputar (sesuaikan dengan file Anda)
NOTIFICATION_SOUND_PATH = "Berhenti.mp3"  # Sound ketika recording selesai
START_RECORDING_SOUND_PATH = "Dimulai.mp3"  # Sound ketika mulai recording

english_words = set(words.words())
indonesian_stopwords = set(stopwords.words('indonesian'))

def load_indonesian_wordlist(filepath='wordlist.lst'):
    try:
        with open(filepath, encoding='utf-8') as f:
            return set(line.strip().lower() for line in f if line.strip())
    except UnicodeDecodeError:
        try:
            with open(filepath, encoding='latin-1') as f:
                return set(line.strip().lower() for line in f if line.strip())
        except Exception:
            return set()
    except Exception:
        return set()

indonesian_words = load_indonesian_wordlist()
valid_words = english_words.union(indonesian_words)

def contains_medical_terms_auto_threshold(text, medical_words):
    tokens = word_tokenize(text.lower())
    tokens = [w.strip(string.punctuation) for w in tokens if w.isalpha()]
    if not tokens:
        return False
    medical_count = sum(1 for w in tokens if w in medical_words)
    ratio = medical_count / len(tokens)
    threshold = 0.4 if len(tokens) <= 5 else 0.1
    return ratio >= threshold

medical_words = load_indonesian_wordlist('wordlist.lst')

MAX_DURATION_SECONDS = 600

def validate_audio_duration(audio_file):
    try:
        audio = AudioSegment.from_file(audio_file)
        duration_sec = len(audio) / 1000.0
        if duration_sec > MAX_DURATION_SECONDS:
            return False, duration_sec
        return True, duration_sec
    except Exception as e:
        return False, -1

def play_notification_sound():
    """Function untuk memainkan sound notification ketika recording selesai"""
    try:
        if os.path.exists(NOTIFICATION_SOUND_PATH):
            pygame.mixer.music.load(NOTIFICATION_SOUND_PATH)
            pygame.mixer.music.play()
            print("πŸ”Š Playing completion notification sound...")
        else:
            print(f"⚠️ File completion sound tidak ditemukan: {NOTIFICATION_SOUND_PATH}")
    except Exception as e:
        print(f"❌ Error playing completion sound: {e}")

def play_start_recording_sound():
    """Function untuk memainkan sound ketika mulai recording"""
    try:
        if os.path.exists(START_RECORDING_SOUND_PATH):
            # Menggunakan Sound effect untuk play bersamaan tanpa interrupt music
            start_sound = pygame.mixer.Sound(START_RECORDING_SOUND_PATH)
            start_sound.play()
            print("🎡 Playing start recording sound...")
        else:
            print(f"⚠️ File start recording sound tidak ditemukan: {START_RECORDING_SOUND_PATH}")
    except Exception as e:
        print(f"❌ Error playing start recording sound: {e}")

def start_recording():
    """Function yang dipanggil ketika tombol record ditekan"""
    print("πŸŽ™οΈ Recording started...")
    # Play start recording sound
    threading.Thread(target=play_start_recording_sound, daemon=True).start()
    return "πŸŽ™οΈ Sedang merekam... Klik stop untuk menyelesaikan"

def stop_recording(audio):
    """Function yang dipanggil ketika recording selesai"""
    if audio is not None:
        print("βœ… Recording completed!")
        # Play notification sound when recording is completed
        threading.Thread(target=play_notification_sound, daemon=True).start()
        return "βœ… Recording selesai! Audio siap diproses"
    else:
        print("❌ No audio recorded")
        return "❌ Tidak ada audio yang direkam"

def test_microphone():
    """Function untuk test microphone"""
    print("πŸ”§ Testing microphone...")
    return "πŸ”§ Testing microphone... Silakan coba record lagi"

def reset_recording_status():
    """Function untuk reset status recording"""
    return "πŸ“± Siap untuk merekam - Klik tombol record"

def handle_audio(audio_file):
    """Handle audio processing - returns (validation_message, transcript, soap, tags)"""
    if audio_file is None:
        return "❌ Tidak ada file audio", "", "", ""
    
    valid, duration = validate_audio_duration(audio_file)
    if not valid:
        if duration == -1:
            msg = "⚠️ Gagal memproses file audio."
        else:
            msg = f"⚠️ Durasi rekaman terlalu panjang ({duration:.1f}s). Maksimal {MAX_DURATION_SECONDS}s."
        return msg, "", "", ""
    
    try:
        with open(audio_file, "rb") as f:
            files = {"audio": f}
            response = requests.post(API_TRANSCRIBE, files=files)
            result = response.json()
        
        transcription = result.get("transcription", "")
        soap_content = result.get("soap_content", "")
        tags_content = result.get("tags_content", "")
        
        if not transcription and not soap_content and not tags_content:
            return "⚠️ Tidak ada hasil dari proses audio", "", "", ""
        
        return "", transcription, soap_content, tags_content
    
    except Exception as e:
        return f"❌ Error processing audio: {str(e)}", "", "", ""

def handle_text(dialogue):
    """Handle text processing - returns (validation_message, transcript, soap, tags)"""
    if not dialogue.strip():
        return "⚠️ Teks tidak boleh kosong", "", "", ""
    
    if not contains_medical_terms_auto_threshold(dialogue, medical_words):
        return "⚠️ Teks tidak mengandung istilah medis yang cukup untuk diproses.", "", "", ""
    
    try:
        response = requests.post(API_TEXT, json={"dialogue": dialogue})
        result = response.json()
        
        soap_content = result.get("soap_content", "")
        tags_content = result.get("tags_content", "")
        
        if not soap_content and not tags_content:
            return "⚠️ Tidak ada hasil dari proses teks", "", "", ""
        
        return "", dialogue, soap_content, tags_content
    
    except Exception as e:
        return f"❌ Error processing text: {str(e)}", "", "", ""

def toggle_inputs_with_refresh(choice):
    # Tampilkan input dan validasi yang sesuai, sembunyikan lainnya
    return (
        gr.update(visible=(choice == "Upload Audio"), value=None),   # audio upload
        gr.update(visible=(choice == "Realtime Recording"), value=None),  # audio record
        gr.update(visible=(choice == "Input Teks"), value=""),  # text input
        gr.update(visible=(choice == "Upload Audio")),  # validasi upload
        gr.update(visible=(choice == "Realtime Recording")),  # validasi realtime
        gr.update(visible=(choice == "Input Teks")),  # validasi teks
        gr.update(visible=(choice == "Realtime Recording")),  # recording status group
        gr.update(visible=(choice == "Realtime Recording")),  # record audio group
        gr.update(value=""),  # transcript
        gr.update(value=""),  # soap
        gr.update(value=""),  # tags
    )

def clear_all_data():
    return (
        gr.update(value=None),  # audio_upload
        gr.update(value=None),  # audio_record
        gr.update(value=""),    # text_input
        gr.update(value=""),    # validation_upload
        gr.update(value=""),    # validation_realtime
        gr.update(value=""),    # validation_text
        gr.update(value="πŸ“± Siap untuk merekam"),  # recording_status
        gr.update(value=""),    # transcript_output
        gr.update(value=""),    # soap_output
        gr.update(value=""),    # tags_output
    )

def process_data(choice, audio_upload, audio_record, text_input):
    """
    Process data based on choice and return results in correct order:
    Returns: (validation_upload, validation_realtime, validation_text, transcript, soap, tags)
    """
    
    if choice == "Upload Audio":
        # Process upload audio
        validation_msg, transcript, soap, tags = handle_audio(audio_upload)
        return (
            validation_msg,  # validation_upload
            "",              # validation_realtime (empty)
            "",              # validation_text (empty)
            transcript,      # transcript_output
            soap,           # soap_output
            tags            # tags_output
        )
    
    elif choice == "Realtime Recording":
        # Process realtime recording
        validation_msg, transcript, soap, tags = handle_audio(audio_record)
        return (
            "",              # validation_upload (empty)
            validation_msg,  # validation_realtime
            "",              # validation_text (empty)
            transcript,      # transcript_output
            soap,           # soap_output
            tags            # tags_output
        )
    
    elif choice == "Input Teks":
        # Process text input
        validation_msg, transcript, soap, tags = handle_text(text_input)
        return (
            "",              # validation_upload (empty)
            "",              # validation_realtime (empty)
            validation_msg,  # validation_text
            transcript,      # transcript_output (will be same as input for text)
            soap,           # soap_output
            tags            # tags_output
        )
    
    else:
        # Default case - clear all
        return ("", "", "", "", "", "")

# Custom CSS untuk tampilan modern dengan alignment yang diperbaiki
modern_css = """
<style>
/* Background gradient yang modern */
.gradio-container {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* Header styling */
.main-header {
    text-align: center;
    padding: 2rem 0;
    color: white;
    margin-bottom: 2rem;
}

.main-header h1 {
    font-size: 3rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
    text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.main-header p {
    font-size: 1.2rem;
    opacity: 0.9;
    font-weight: 300;
}

/* Card styling */
.input-card {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    padding: 2rem;
    margin: 0.8rem 0;
    box-shadow: 0 8px 32px rgba(0,0,0,0.1);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255,255,255,0.2);
}

.output-card {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    padding: 2rem;
    box-shadow: 0 8px 32px rgba(0,0,0,0.1);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255,255,255,0.2);
}

/* Record audio section with padding */
.record-audio-section {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    padding: 2rem;
    margin: 0.8rem 0;
    box-shadow: 0 8px 32px rgba(0,0,0,0.1);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255,255,255,0.2);
}

/* Output header styling - aligned properly */
.output-header {
    text-align: left;
    margin-bottom: 1.5rem;
    padding-left: 0;
}

.output-header h3 {
    font-size: 1.5rem;
    font-weight: 600;
    color: #2d3436;
    margin: 0;
    display: flex;
    align-items: center;
}

/* Spacing adjustments */
.input-section {
    margin-bottom: 1rem;
}

.button-section {
    margin-top: 0.5rem;
    margin-bottom: 1rem;
}

/* Updated output section styling with proper alignment */
.output-section {
    background: rgba(255, 255, 255, 0.98);
    border-radius: 15px;
    padding: 1.5rem;
    margin: 1rem 0 0 0;
    box-shadow: 0 4px 20px rgba(0,0,0,0.08);
}

/* Ensure all output components are aligned consistently */
.output-container {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* Button styling */
.record-btn {
    background: linear-gradient(45deg, #ff6b6b, #ee5a24);
    border: none;
    border-radius: 50px;
    padding: 1rem 2rem;
    color: white;
    font-weight: 600;
    font-size: 1.1rem;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(238, 90, 36, 0.4);
}

.record-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(238, 90, 36, 0.6);
}

.test-btn {
    background: linear-gradient(45deg, #74b9ff, #0984e3);
    border: none;
    border-radius: 50px;
    padding: 0.8rem 1.5rem;
    color: white;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
}

.test-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 15px rgba(116, 185, 255, 0.4);
}

.reset-btn {
    background: linear-gradient(45deg, #a29bfe, #6c5ce7);
    border: none;
    border-radius: 15px;
    padding: 0.4rem 1rem;
    font-size: 0.8rem;
    color: white;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
    height: 2rem;
    min-width: 80px;
}

.reset-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 10px rgba(162, 155, 254, 0.4);
}

.process-btn {
    background: linear-gradient(45deg, #00b894, #00cec9);
    border: none;
    border-radius: 50px;
    padding: 1rem 2.5rem;
    color: white;
    font-weight: 600;
    font-size: 1.2rem;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(0, 184, 148, 0.4);
    margin: 1rem 0;
}

.process-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(0, 184, 148, 0.6);
}

/* Status indicator */
.status-indicator {
    background: rgba(255, 255, 255, 0.95);
    border: 2px solid #74b9ff;
    color: #2d3436;
    padding: 2rem;
    border-radius: 15px;
    font-weight: 500;
    margin: 1rem 0;
    box-shadow: 0 4px 15px rgba(116, 185, 255, 0.2);
}

/* Input styling */
.audio-input {
    border: 2px dashed #ddd;
    border-radius: 15px;
    padding: 2rem;
    text-align: center;
    background: rgba(255,255,255,0.5);
    transition: all 0.3s ease;
}

.audio-input:hover {
    border-color: #74b9ff;
    background: rgba(116, 185, 255, 0.1);
}

/* Ensure proper text alignment in outputs */
.output-section textarea {
    text-align: left;
    vertical-align: top;
    resize: vertical;
}

/* Responsive design */
@media (max-width: 768px) {
    .main-header h1 {
        font-size: 2rem;
    }
    
    .input-card, .output-card, .record-audio-section {
        padding: 2rem;
    }
    
    .record-btn, .process-btn {
        width: 100%;
        margin: 0.5rem 0;
    }
    
    .reset-btn {
        min-width: 70px;
        font-size: 0.75rem;
    }
}

/* Animation untuk elemen yang muncul */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(30px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.input-card, .output-card, .record-audio-section {
    animation: fadeInUp 0.6s ease-out;
}

/* Microphone icon animation */
@keyframes pulse {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.05);
    }
    100% {
        transform: scale(1);
    }
}

.recording-active {
    animation: pulse 1s infinite;
}
</style>
"""

# Buat interface dengan theme modern
with gr.Blocks(
    title="🩺 SOAP AI - Modern Interface",
    css=modern_css,
    theme=gr.themes.Soft(
        primary_hue="blue",
        secondary_hue="cyan",
        neutral_hue="slate",
        font=gr.themes.GoogleFont("Inter")
    )
) as app:
    
    # Header
    gr.HTML("""
    <div class="main-header">
        <h1>πŸŽ™οΈ Realtime Recording</h1>
        <p>High Quality Audio Recording with Smart Notifications</p>
    </div>
    """)

    with gr.Row():
        with gr.Column(scale=8):
            input_choice = gr.Dropdown(
                choices=["Upload Audio", "Realtime Recording", "Input Teks"],
                value="Realtime Recording",
                label="🎯 Pilih Metode Input",
                container=True,
                elem_classes=["input-dropdown"]
            )
        with gr.Column(scale=2):
            clear_button = gr.Button(
                "πŸ—‘οΈ Clear",
                variant="secondary",
                size="sm",
                elem_classes=["reset-btn"]
            )

    # Input Section - Upload Audio
    with gr.Group(elem_classes=["input-card"], visible=False) as upload_audio_group:
        gr.HTML("<h3>πŸ“ Upload Audio File</h3>")
        
        audio_upload = gr.Audio(
            sources=["upload"], 
            label="πŸ“ Upload File Audio",
            type="filepath", 
            show_download_button=False,
            show_share_button=False,
            interactive=True,
            elem_classes=["audio-input"]
        )

    # Input Section - Record Audio with proper padding
    with gr.Group(elem_classes=["record-audio-section"], visible=True) as record_audio_group:
        gr.HTML("<h3>🎡 Record Your Audio</h3>")
        
        audio_record = gr.Audio(
            sources=["microphone"], 
            label="πŸŽ™οΈ Realtime Recording",
            type="filepath", 
            show_download_button=True,
            show_share_button=False,
            interactive=True,
            streaming=False,
            elem_classes=["audio-input"]
        )

    # Input Section - Text Input (without record audio section)
    with gr.Group(elem_classes=["input-card"], visible=False) as text_input_group:
        gr.HTML("<h3>πŸ“ Input Teks</h3>")
        
        text_input = gr.Textbox(
            label="πŸ“ Masukkan Percakapan Dokter-Pasien", 
            lines=6, 
            placeholder="Ketik percakapan antara dokter dan pasien di sini...",
            elem_classes=["text-input"]
        )

    # Status Section - hanya untuk Realtime Recording
    recording_status_group = gr.Group(elem_classes=["status-indicator"], visible=True)
    with recording_status_group:
        gr.HTML("<h4>πŸ“Š Status Recording</h4>")
        recording_status = gr.Textbox(
            value="Siap untuk merekam",
            interactive=False,
            show_label=False,
            lines=1,
            elem_classes=["status-display"]
        )

    # Validation Section
    validation_upload = gr.Textbox(
        label="⚠️ Validasi Upload Audio", 
        lines=1, 
        interactive=False, 
        visible=False,
        elem_classes=["validation-msg"]
    )
    validation_realtime = gr.Textbox(
        label="⚠️ Validasi Realtime Recording", 
        lines=1, 
        interactive=False, 
        visible=True,
        elem_classes=["validation-msg"]
    )
    validation_text = gr.Textbox(
        label="⚠️ Validasi Input Teks", 
        lines=1, 
        interactive=False, 
        visible=False,
        elem_classes=["validation-msg"]
    )

    # Process Button
    process_button = gr.Button(
        "πŸš€ Proses ke SOAP",
        variant="primary",
        size="lg",
        elem_classes=["process-btn"]
    )

    # Output Section with proper alignment - Fixed Layout
    with gr.Group(elem_classes=["output-card"]):
        # Header aligned properly
        with gr.Row():
            with gr.Column():
                gr.HTML('<div class="output-header"><h3>πŸ“‹ Hasil Analisis</h3></div>')
        
        # All outputs in aligned container
        with gr.Column(elem_classes=["output-container"]):
            transcript_output = gr.Textbox(
                label="πŸ“ Hasil Transkripsi", 
                lines=4,
                elem_classes=["output-section"]
            )
            
            soap_output = gr.Textbox(
                label="πŸ“‹ Ringkasan SOAP", 
                lines=8,
                elem_classes=["output-section"]
            )
            
            tags_output = gr.Textbox(
                label="🏷️ Medical Tags", 
                lines=6,
                elem_classes=["output-section"]
            )

    # Footer
    gr.HTML("""
    <div style="text-align: center; padding: 2rem; color: rgba(255,255,255,0.7);">
        <p>Use via API πŸ”₯ β€’ Built with Gradio πŸš€</p>
    </div>
    """)

    # Event handlers untuk toggle inputs
    input_choice.change(
        fn=lambda choice: (
            gr.update(visible=(choice == "Upload Audio")),   # upload_audio_group
            gr.update(visible=(choice == "Realtime Recording")),  # record_audio_group
            gr.update(visible=(choice == "Input Teks")),  # text_input_group
            gr.update(visible=(choice == "Upload Audio")),  # validation_upload
            gr.update(visible=(choice == "Realtime Recording")),  # validation_realtime
            gr.update(visible=(choice == "Input Teks")),  # validation_text
            gr.update(visible=(choice == "Realtime Recording")),  # recording_status_group
            gr.update(value=""),  # transcript
            gr.update(value=""),  # soap
            gr.update(value=""),  # tags
        ),
        inputs=input_choice,
        outputs=[
            upload_audio_group,
            record_audio_group,
            text_input_group,
            validation_upload,
            validation_realtime,
            validation_text,
            recording_status_group,
            transcript_output,
            soap_output,
            tags_output,
        ],
    )

    # Event handlers untuk recording
    audio_record.start_recording(
        fn=start_recording,
        outputs=recording_status
    )
    
    audio_record.stop_recording(
        fn=stop_recording,
        inputs=audio_record,
        outputs=recording_status
    )

    clear_button.click(
        fn=clear_all_data,
        outputs=[
            audio_upload,
            audio_record,
            text_input,
            validation_upload,
            validation_realtime,
            validation_text,
            recording_status,
            transcript_output,
            soap_output,
            tags_output,
        ],
    )

    process_button.click(
        fn=process_data,
        inputs=[input_choice, audio_upload, audio_record, text_input],
        outputs=[
            validation_upload,
            validation_realtime,
            validation_text,
            transcript_output,
            soap_output,
            tags_output,
        ],
        show_progress="minimal",
    )

# Startup information
if __name__ == "__main__":
    print("πŸš€ Starting Enhanced SOAP AI Application with Modern UI...")
    print("πŸ“‹ Setup Instructions:")
    print("1. Install dependencies: pip install gradio pygame pydub nltk requests python-dotenv")
    print(f"2. Place your sound files:")
    print(f"   - Start recording sound: {START_RECORDING_SOUND_PATH}")
    print(f"   - Completion sound: {NOTIFICATION_SOUND_PATH}")
    print("3. Supported sound formats: WAV, MP3, OGG")
    print("4. Make sure wordlist.lst file is available")
    print("5. Set up your .env file with API_TRANSCRIBE and API_TEXT")
    print()
    
    # Cek apakah file sound ada
    sounds_status = []
    if os.path.exists(START_RECORDING_SOUND_PATH):
        print(f"βœ… Start recording sound found: {START_RECORDING_SOUND_PATH}")
        sounds_status.append("start")
    else:
        print(f"⚠️  Start recording sound not found: {START_RECORDING_SOUND_PATH}")
    
    if os.path.exists(NOTIFICATION_SOUND_PATH):
        print(f"βœ… Completion sound found: {NOTIFICATION_SOUND_PATH}")
        sounds_status.append("completion")
    else:
        print(f"⚠️  Completion sound not found: {NOTIFICATION_SOUND_PATH}")
    
    if not sounds_status:
        print("πŸ“ Note: Add sound files to enable audio notifications for realtime recording")
    elif len(sounds_status) == 1:
        print("πŸ“ Note: Add the missing sound file for complete audio experience")
    
    print("\n🌐 Application will start at: http://localhost:7860")
    print("πŸŽ™οΈ Make sure to allow microphone access when using Realtime Recording!")
    print("✨ New Modern UI with enhanced visual experience!")
    print()

app.launch()