syafiqq02 commited on
Commit
cac1418
Β·
1 Parent(s): 89266f3

last commit

Browse files
Files changed (1) hide show
  1. app.py +24 -120
app.py CHANGED
@@ -10,40 +10,17 @@ import nltk
10
  from nltk.corpus import words, stopwords
11
  from dotenv import load_dotenv
12
 
13
- # Initialize pygame mixer for sound playback (with error handling)
14
- AUDIO_AVAILABLE = False
15
- try:
16
- import pygame
17
- pygame.mixer.init()
18
- AUDIO_AVAILABLE = True
19
- print("βœ… Audio system initialized successfully")
20
- except Exception as e:
21
- print(f"⚠️ Audio system not available: {e}")
22
- print("πŸ“ Note: Sound notifications will be disabled")
23
-
24
  # Download resource NLTK (hanya sekali)
25
- try:
26
- nltk.download('punkt', quiet=True)
27
- nltk.download('words', quiet=True)
28
- nltk.download('stopwords', quiet=True)
29
- except Exception as e:
30
- print(f"⚠️ NLTK download warning: {e}")
31
 
32
  load_dotenv()
33
  API_TRANSCRIBE = os.getenv("API_TRANSCRIBE")
34
  API_TEXT = os.getenv("API_TEXT")
35
 
36
- # Path ke file sound yang akan diputar (sesuaikan dengan file Anda)
37
- NOTIFICATION_SOUND_PATH = "Berhenti.mp3" # Sound ketika recording selesai
38
- START_RECORDING_SOUND_PATH = "Dimulai.mp3" # Sound ketika mulai recording
39
-
40
- try:
41
- english_words = set(words.words())
42
- indonesian_stopwords = set(stopwords.words('indonesian'))
43
- except Exception as e:
44
- print(f"⚠️ NLTK data warning: {e}")
45
- english_words = set()
46
- indonesian_stopwords = set()
47
 
48
  def load_indonesian_wordlist(filepath='wordlist.lst'):
49
  try:
@@ -62,18 +39,14 @@ indonesian_words = load_indonesian_wordlist()
62
  valid_words = english_words.union(indonesian_words)
63
 
64
  def contains_medical_terms_auto_threshold(text, medical_words):
65
- try:
66
- tokens = word_tokenize(text.lower())
67
- tokens = [w.strip(string.punctuation) for w in tokens if w.isalpha()]
68
- if not tokens:
69
- return False
70
- medical_count = sum(1 for w in tokens if w in medical_words)
71
- ratio = medical_count / len(tokens)
72
- threshold = 0.4 if len(tokens) <= 5 else 0.1
73
- return ratio >= threshold
74
- except Exception as e:
75
- print(f"⚠️ Medical terms detection warning: {e}")
76
- return len(text.strip()) > 0 # Fallback: accept non-empty text
77
 
78
  medical_words = load_indonesian_wordlist('wordlist.lst')
79
 
@@ -89,52 +62,15 @@ def validate_audio_duration(audio_file):
89
  except Exception as e:
90
  return False, -1
91
 
92
- def play_notification_sound():
93
- """Function untuk memainkan sound notification ketika recording selesai"""
94
- if not AUDIO_AVAILABLE:
95
- print("πŸ”‡ Audio system not available - notification sound skipped")
96
- return
97
-
98
- try:
99
- if os.path.exists(NOTIFICATION_SOUND_PATH):
100
- pygame.mixer.music.load(NOTIFICATION_SOUND_PATH)
101
- pygame.mixer.music.play()
102
- print("πŸ”Š Playing completion notification sound...")
103
- else:
104
- print(f"⚠️ File completion sound tidak ditemukan: {NOTIFICATION_SOUND_PATH}")
105
- except Exception as e:
106
- print(f"❌ Error playing completion sound: {e}")
107
-
108
- def play_start_recording_sound():
109
- """Function untuk memainkan sound ketika mulai recording"""
110
- if not AUDIO_AVAILABLE:
111
- print("πŸ”‡ Audio system not available - start recording sound skipped")
112
- return
113
-
114
- try:
115
- if os.path.exists(START_RECORDING_SOUND_PATH):
116
- # Menggunakan Sound effect untuk play bersamaan tanpa interrupt music
117
- start_sound = pygame.mixer.Sound(START_RECORDING_SOUND_PATH)
118
- start_sound.play()
119
- print("🎡 Playing start recording sound...")
120
- else:
121
- print(f"⚠️ File start recording sound tidak ditemukan: {START_RECORDING_SOUND_PATH}")
122
- except Exception as e:
123
- print(f"❌ Error playing start recording sound: {e}")
124
-
125
  def start_recording():
126
  """Function yang dipanggil ketika tombol record ditekan"""
127
  print("πŸŽ™οΈ Recording started...")
128
- # Play start recording sound
129
- threading.Thread(target=play_start_recording_sound, daemon=True).start()
130
  return "πŸŽ™οΈ Sedang merekam... Klik stop untuk menyelesaikan"
131
 
132
  def stop_recording(audio):
133
  """Function yang dipanggil ketika recording selesai"""
134
  if audio is not None:
135
  print("βœ… Recording completed!")
136
- # Play notification sound when recording is completed
137
- threading.Thread(target=play_notification_sound, daemon=True).start()
138
  return "βœ… Recording selesai! Audio siap diproses"
139
  else:
140
  print("❌ No audio recorded")
@@ -559,11 +495,10 @@ with gr.Blocks(
559
  ) as app:
560
 
561
  # Header
562
- audio_status = "πŸ”Š With Sound Notifications" if AUDIO_AVAILABLE else "πŸ”‡ Silent Mode"
563
- gr.HTML(f"""
564
  <div class="main-header">
565
  <h1>πŸŽ™οΈ Realtime Recording</h1>
566
- <p>High Quality Audio Recording {audio_status}</p>
567
  </div>
568
  """)
569
 
@@ -695,13 +630,9 @@ with gr.Blocks(
695
  )
696
 
697
  # Footer
698
- footer_text = "Built with Gradio πŸš€"
699
- if not AUDIO_AVAILABLE:
700
- footer_text += " β€’ Running in Silent Mode (No Audio Hardware Detected)"
701
-
702
- gr.HTML(f"""
703
  <div style="text-align: center; padding: 2rem; color: rgba(255,255,255,0.7);">
704
- <p>Use via API πŸ”₯ β€’ {footer_text}</p>
705
  </div>
706
  """)
707
 
@@ -780,41 +711,14 @@ with gr.Blocks(
780
  if __name__ == "__main__":
781
  print("πŸš€ Starting Enhanced SOAP AI Application with Modern UI...")
782
  print("πŸ“‹ Setup Instructions:")
783
- print("1. Install dependencies: pip install gradio pygame pydub nltk requests python-dotenv")
784
- print(f"2. Place your sound files:")
785
- print(f" - Start recording sound: {START_RECORDING_SOUND_PATH}")
786
- print(f" - Completion sound: {NOTIFICATION_SOUND_PATH}")
787
- print("3. Supported sound formats: WAV, MP3, OGG")
788
- print("4. Make sure wordlist.lst file is available")
789
- print("5. Set up your .env file with API_TRANSCRIBE and API_TEXT")
790
  print()
791
 
792
- # Audio system status
793
- if AUDIO_AVAILABLE:
794
- print("βœ… Audio system: ENABLED")
795
- # Cek apakah file sound ada
796
- sounds_status = []
797
- if os.path.exists(START_RECORDING_SOUND_PATH):
798
- print(f"βœ… Start recording sound found: {START_RECORDING_SOUND_PATH}")
799
- sounds_status.append("start")
800
- else:
801
- print(f"⚠️ Start recording sound not found: {START_RECORDING_SOUND_PATH}")
802
-
803
- if os.path.exists(NOTIFICATION_SOUND_PATH):
804
- print(f"βœ… Completion sound found: {NOTIFICATION_SOUND_PATH}")
805
- sounds_status.append("completion")
806
- else:
807
- print(f"⚠️ Completion sound not found: {NOTIFICATION_SOUND_PATH}")
808
-
809
- if not sounds_status:
810
- print("πŸ“ Note: Add sound files to enable audio notifications for realtime recording")
811
- elif len(sounds_status) == 1:
812
- print("πŸ“ Note: Add the missing sound file for complete audio experience")
813
- else:
814
- print("πŸ”‡ Audio system: DISABLED (No audio hardware detected)")
815
- print("πŸ“ Note: Running in silent mode - sound notifications are disabled")
816
-
817
  print("\n🌐 Application will start at: http://localhost:7860")
818
  print("πŸŽ™οΈ Make sure to allow microphone access when using Realtime Recording!")
819
- print("✨ New Modern UI with enhanced visual experience!")
820
- print()
 
 
 
10
  from nltk.corpus import words, stopwords
11
  from dotenv import load_dotenv
12
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Download resource NLTK (hanya sekali)
14
+ nltk.download('punkt')
15
+ nltk.download('words')
16
+ nltk.download('stopwords')
 
 
 
17
 
18
  load_dotenv()
19
  API_TRANSCRIBE = os.getenv("API_TRANSCRIBE")
20
  API_TEXT = os.getenv("API_TEXT")
21
 
22
+ english_words = set(words.words())
23
+ indonesian_stopwords = set(stopwords.words('indonesian'))
 
 
 
 
 
 
 
 
 
24
 
25
  def load_indonesian_wordlist(filepath='wordlist.lst'):
26
  try:
 
39
  valid_words = english_words.union(indonesian_words)
40
 
41
  def contains_medical_terms_auto_threshold(text, medical_words):
42
+ tokens = word_tokenize(text.lower())
43
+ tokens = [w.strip(string.punctuation) for w in tokens if w.isalpha()]
44
+ if not tokens:
45
+ return False
46
+ medical_count = sum(1 for w in tokens if w in medical_words)
47
+ ratio = medical_count / len(tokens)
48
+ threshold = 0.4 if len(tokens) <= 5 else 0.1
49
+ return ratio >= threshold
 
 
 
 
50
 
51
  medical_words = load_indonesian_wordlist('wordlist.lst')
52
 
 
62
  except Exception as e:
63
  return False, -1
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def start_recording():
66
  """Function yang dipanggil ketika tombol record ditekan"""
67
  print("πŸŽ™οΈ Recording started...")
 
 
68
  return "πŸŽ™οΈ Sedang merekam... Klik stop untuk menyelesaikan"
69
 
70
  def stop_recording(audio):
71
  """Function yang dipanggil ketika recording selesai"""
72
  if audio is not None:
73
  print("βœ… Recording completed!")
 
 
74
  return "βœ… Recording selesai! Audio siap diproses"
75
  else:
76
  print("❌ No audio recorded")
 
495
  ) as app:
496
 
497
  # Header
498
+ gr.HTML("""
 
499
  <div class="main-header">
500
  <h1>πŸŽ™οΈ Realtime Recording</h1>
501
+ <p>High Quality Audio Recording</p>
502
  </div>
503
  """)
504
 
 
630
  )
631
 
632
  # Footer
633
+ gr.HTML("""
 
 
 
 
634
  <div style="text-align: center; padding: 2rem; color: rgba(255,255,255,0.7);">
635
+ <p>Use via API πŸ”₯ β€’ Built with Gradio πŸš€</p>
636
  </div>
637
  """)
638
 
 
711
  if __name__ == "__main__":
712
  print("πŸš€ Starting Enhanced SOAP AI Application with Modern UI...")
713
  print("πŸ“‹ Setup Instructions:")
714
+ print("1. Install dependencies: pip install gradio pydub nltk requests python-dotenv")
715
+ print("2. Make sure wordlist.lst file is available")
716
+ print("3. Set up your .env file with API_TRANSCRIBE and API_TEXT")
 
 
 
 
717
  print()
718
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
719
  print("\n🌐 Application will start at: http://localhost:7860")
720
  print("πŸŽ™οΈ Make sure to allow microphone access when using Realtime Recording!")
721
+ print("✨ Modern UI with clean recording status!")
722
+ print()
723
+
724
+ app.launch()