I'm encountering a "permission denied" error when saving recordings in my Flask app deployed on Hugging Face Spaces. I'd appreciate any guidance on resolving this issue.
Error message:
[Errno 13] Permission denied: 'temp/recording_.mp3'
Experimented with file paths: I attempted using absolute paths and different directory names to rule out path-related issues, but it didn't resolve the problem
Expected:
The audio_file.save(filename) function should successfully save the recording to the temp directory without any permission errors.
Actual Result:
The app throws a [Errno 13] Permission denied: 'temp/recording_.mp3' error when attempting to save the recording.
app/server.py:
import os
os.environ['TRANSFORMERS_CACHE'] = "./cache"
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS
import random
import glob
from modules import Module
UPLOAD_DIR = 'temp'
model = Module()
model.warmup(UPLOAD_DIR)
app = Flask(__name__)
CORS(app)
# Set path to temporary directory
# Ensure directory exists
if not os.path.exists(UPLOAD_DIR):
os.makedirs(UPLOAD_DIR)
@app.route('/')
def home():
print("Entering home function...")
return render_template('index.html')
@app.route('/save_recording', methods=['POST'])
def save_recording():
try:
print("Received POST request to save recording...")
audio_file = request.files['audio']
filename = os.path.join(UPLOAD_DIR, 'recording_' + '.mp3')
audio_file.save(filename)
# Optionally trigger prediction with saved filename
text, emotion = generate(filename)
print("Recording saved successfully")
return jsonify({'status': 'success', 'filename': filename, 'text': text, 'emotion': emotion})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)})
@app.route('/generate', methods=['GET'])
def generate(filename=None):
if not filename:
# Get latest recording if filename not provided
list_of_files = glob.glob(os.path.join(UPLOAD_DIR, '*.mp3'))
latest_file = max(list_of_files, key=os.path.getctime) if list_of_files else None
if not latest_file:
return jsonify({'status': 'error', 'message': 'No recorded audio found'})
filename = latest_file
# Perform prediction using specified filename
text, emotion = model.predict(audio_path=filename, upload_dir=UPLOAD_DIR)
print("text:", text)
print("emotion:", emotion)
return jsonify({'status': 'success', 'text': text, 'emotion': emotion})
if __name__ == '__main__':
app.run(debug=True, port=7860, host='0.0.0.0')
`
app/modules/init.py
import os
import time
from modules.emotion import Emotion
from modules.transcription import Transcription
transcription_model = os.getenv("TRANSCRIPTION_MODEL", "/models/distil-medium.en/")
emotion_model = os.getenv("EMOTION_MODEL", "/models/distilbert-base-uncased-go-emotions-student/")
transcription_obj = Transcription(model_name=transcription_model)
emotion_obj = Emotion(model_name=emotion_model)
class Module:
def warmup(self, upload_dir: str):
UPLOAD_DIR = '/home/adarsh/bardproject/app/temp'
text, emotion = self.predict(audio_path=os.path.join(upload_dir, 'recording_.mp3'), upload_dir=upload_dir) # Pass upload_dir
print("text: ", text)
print("emotion: ", emotion)
def predict(self, audio_path: str, upload_dir: str) -> str:
"""Loads audio, gets transcription and detects emotion
Args:
audio_path (str): path to the audio file
Returns:
str: emotion
"""
print("Getting transcription...")
start_time = time.time()
if text := transcription_obj.transcribe(audio_path=audio_path):
print("Text: ", text, time.time() - start_time)
start_time = time.time()
emotion = emotion_obj.detect_emotion(text=text)
print("Emotion: ", emotion, time.time() - start_time)
return text, emotion
return None