Spaces:
Sleeping
Sleeping
last commit
Browse files
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 |
-
|
26 |
-
|
27 |
-
|
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 |
-
|
37 |
-
|
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 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
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 |
-
|
563 |
-
gr.HTML(f"""
|
564 |
<div class="main-header">
|
565 |
<h1>ποΈ Realtime Recording</h1>
|
566 |
-
<p>High Quality Audio Recording
|
567 |
</div>
|
568 |
""")
|
569 |
|
@@ -695,13 +630,9 @@ with gr.Blocks(
|
|
695 |
)
|
696 |
|
697 |
# Footer
|
698 |
-
|
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 π₯ β’
|
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
|
784 |
-
print(
|
785 |
-
print(
|
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("β¨
|
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()
|