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

new gradio commit

Browse files
Files changed (1) hide show
  1. app.py +76 -40
app.py CHANGED
@@ -3,7 +3,6 @@ import threading
3
  import os
4
  import requests
5
  import string
6
- import pygame
7
  import time
8
  from pydub import AudioSegment
9
  from nltk.tokenize import word_tokenize
@@ -11,13 +10,24 @@ import nltk
11
  from nltk.corpus import words, stopwords
12
  from dotenv import load_dotenv
13
 
14
- # Initialize pygame mixer for sound playback
15
- pygame.mixer.init()
 
 
 
 
 
 
 
 
16
 
17
  # Download resource NLTK (hanya sekali)
18
- nltk.download('punkt')
19
- nltk.download('words')
20
- nltk.download('stopwords')
 
 
 
21
 
22
  load_dotenv()
23
  API_TRANSCRIBE = os.getenv("API_TRANSCRIBE")
@@ -27,8 +37,13 @@ API_TEXT = os.getenv("API_TEXT")
27
  NOTIFICATION_SOUND_PATH = "Berhenti.mp3" # Sound ketika recording selesai
28
  START_RECORDING_SOUND_PATH = "Dimulai.mp3" # Sound ketika mulai recording
29
 
30
- english_words = set(words.words())
31
- indonesian_stopwords = set(stopwords.words('indonesian'))
 
 
 
 
 
32
 
33
  def load_indonesian_wordlist(filepath='wordlist.lst'):
34
  try:
@@ -47,14 +62,18 @@ indonesian_words = load_indonesian_wordlist()
47
  valid_words = english_words.union(indonesian_words)
48
 
49
  def contains_medical_terms_auto_threshold(text, medical_words):
50
- tokens = word_tokenize(text.lower())
51
- tokens = [w.strip(string.punctuation) for w in tokens if w.isalpha()]
52
- if not tokens:
53
- return False
54
- medical_count = sum(1 for w in tokens if w in medical_words)
55
- ratio = medical_count / len(tokens)
56
- threshold = 0.4 if len(tokens) <= 5 else 0.1
57
- return ratio >= threshold
 
 
 
 
58
 
59
  medical_words = load_indonesian_wordlist('wordlist.lst')
60
 
@@ -72,6 +91,10 @@ def validate_audio_duration(audio_file):
72
 
73
  def play_notification_sound():
74
  """Function untuk memainkan sound notification ketika recording selesai"""
 
 
 
 
75
  try:
76
  if os.path.exists(NOTIFICATION_SOUND_PATH):
77
  pygame.mixer.music.load(NOTIFICATION_SOUND_PATH)
@@ -84,6 +107,10 @@ def play_notification_sound():
84
 
85
  def play_start_recording_sound():
86
  """Function untuk memainkan sound ketika mulai recording"""
 
 
 
 
87
  try:
88
  if os.path.exists(START_RECORDING_SOUND_PATH):
89
  # Menggunakan Sound effect untuk play bersamaan tanpa interrupt music
@@ -532,10 +559,11 @@ with gr.Blocks(
532
  ) as app:
533
 
534
  # Header
535
- gr.HTML("""
 
536
  <div class="main-header">
537
  <h1>πŸŽ™οΈ Realtime Recording</h1>
538
- <p>High Quality Audio Recording with Smart Notifications</p>
539
  </div>
540
  """)
541
 
@@ -667,9 +695,13 @@ with gr.Blocks(
667
  )
668
 
669
  # Footer
670
- gr.HTML("""
 
 
 
 
671
  <div style="text-align: center; padding: 2rem; color: rgba(255,255,255,0.7);">
672
- <p>Use via API πŸ”₯ β€’ Built with Gradio πŸš€</p>
673
  </div>
674
  """)
675
 
@@ -757,28 +789,32 @@ if __name__ == "__main__":
757
  print("5. Set up your .env file with API_TRANSCRIBE and API_TEXT")
758
  print()
759
 
760
- # Cek apakah file sound ada
761
- sounds_status = []
762
- if os.path.exists(START_RECORDING_SOUND_PATH):
763
- print(f"βœ… Start recording sound found: {START_RECORDING_SOUND_PATH}")
764
- sounds_status.append("start")
765
- else:
766
- print(f"⚠️ Start recording sound not found: {START_RECORDING_SOUND_PATH}")
767
-
768
- if os.path.exists(NOTIFICATION_SOUND_PATH):
769
- print(f"βœ… Completion sound found: {NOTIFICATION_SOUND_PATH}")
770
- sounds_status.append("completion")
 
 
 
 
 
 
 
 
 
 
771
  else:
772
- print(f"⚠️ Completion sound not found: {NOTIFICATION_SOUND_PATH}")
773
-
774
- if not sounds_status:
775
- print("πŸ“ Note: Add sound files to enable audio notifications for realtime recording")
776
- elif len(sounds_status) == 1:
777
- print("πŸ“ Note: Add the missing sound file for complete audio experience")
778
 
779
  print("\n🌐 Application will start at: http://localhost:7860")
780
  print("πŸŽ™οΈ Make sure to allow microphone access when using Realtime Recording!")
781
  print("✨ New Modern UI with enhanced visual experience!")
782
- print()
783
-
784
- app.launch()
 
3
  import os
4
  import requests
5
  import string
 
6
  import time
7
  from pydub import AudioSegment
8
  from nltk.tokenize import word_tokenize
 
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")
 
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
  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
 
 
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)
 
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
 
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
  )
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
 
 
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()