Skip to content

Commit a30368a

Browse files
...
1 parent bbf6bff commit a30368a

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

app.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1+
import os
2+
import sys
3+
4+
# --- THE MAGIC SHIELD: MUST BE AT THE TOP ---
5+
os.environ["TF_USE_LEGACY_KERAS"] = "1"
6+
import tensorflow as tf
7+
import tf_keras as keras
8+
sys.modules["keras"] = keras
9+
# --------------------------------------------
10+
111
from flask import Flask, render_template, request
212
import transformers
313
from transformers import TFAutoModelForCausalLM, AutoTokenizer
4-
import tensorflow as tf
514
import logging
615
from scripts.system.generate_text import generate_text
716
import webbrowser
817

18+
# Suppress the noise
919
transformers.logging.set_verbosity_error()
1020
tf.get_logger().setLevel(logging.ERROR)
1121

1222
app = Flask(__name__, static_url_path='/static')
1323

1424
model_name = "gpt2"
25+
26+
# Now this will load without the 'NoneType' error
1527
model = TFAutoModelForCausalLM.from_pretrained(model_name)
1628
tokenizer = AutoTokenizer.from_pretrained(model_name, pad_token_id=50256)
1729

@@ -26,5 +38,8 @@ def generate():
2638
return render_template('index.html', prompt=prompt, generated_text=generated_text)
2739

2840
if __name__ == "__main__":
41+
# Note: debug=True can sometimes cause double-loading of the model
42+
# which might crash your RAM (the 10% warning you keep seeing).
43+
# If it crashes, try setting debug=False.
2944
webbrowser.open('http://127.0.0.1:5000/')
3045
app.run(debug=True, use_reloader=False)

scripts/system/play_audio.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,31 @@
33
import os
44

55
def play_audio(audio_path):
6+
if not os.path.exists(audio_path):
7+
print(f"File not found: {audio_path}")
8+
return
9+
10+
# 1. Initialize
611
pygame.mixer.init()
7-
pygame.mixer.music.load(audio_path)
812

9-
play_count = 0
10-
while play_count < 1:
13+
try:
14+
# 2. Load and Play
15+
pygame.mixer.music.load(audio_path)
1116
pygame.mixer.music.play()
17+
18+
# 3. Wait for it to finish
1219
while pygame.mixer.music.get_busy():
13-
time.sleep(1)
20+
time.sleep(0.1) # Shorter sleep for better responsiveness
21+
22+
# 4. CRITICAL: Stop and Unload the file
1423
pygame.mixer.music.stop()
15-
play_count += 1
16-
24+
pygame.mixer.music.unload() # Releases the file handle
25+
26+
finally:
27+
# 5. CRITICAL: Shut down the mixer to release the ALSA device
28+
pygame.mixer.quit()
29+
30+
# 6. Cleanup
1731
try:
1832
os.remove(audio_path)
1933
except Exception as e:

0 commit comments

Comments
 (0)