Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,52 @@ from transformers import pipeline
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 4 |
|
| 5 |
# Initialize the tokenizer and model
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
|
| 9 |
|
| 10 |
@app.route('/classify', methods=['POST'])
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 4 |
|
| 5 |
# Initialize the tokenizer and model
|
| 6 |
+
import whisper
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Load the model once at startup (better performance for multiple requests)
|
| 14 |
+
model = whisper.load_model("small")
|
| 15 |
+
|
| 16 |
+
def allowed_file(filename):
|
| 17 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in {'wav', 'mp3', 'ogg', 'flac', 'm4a'}
|
| 18 |
+
|
| 19 |
+
@app.route('/transcribe', methods=['POST'])
|
| 20 |
+
def transcribe_audio():
|
| 21 |
+
# Check if a file was uploaded
|
| 22 |
+
if 'file' not in request.files:
|
| 23 |
+
return jsonify({'error': 'No file uploaded'}), 400
|
| 24 |
+
|
| 25 |
+
file = request.files['file']
|
| 26 |
+
|
| 27 |
+
# Check if the file is empty
|
| 28 |
+
if file.filename == '':
|
| 29 |
+
return jsonify({'error': 'No selected file'}), 400
|
| 30 |
+
|
| 31 |
+
# Check allowed file types
|
| 32 |
+
if not allowed_file(file.filename):
|
| 33 |
+
return jsonify({'error': 'Unsupported file type'}), 400
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
# Save the temporary file
|
| 37 |
+
temp_path = "temp_audio"
|
| 38 |
+
file.save(temp_path)
|
| 39 |
+
|
| 40 |
+
# Transcribe the audio
|
| 41 |
+
result = model.transcribe(temp_path)
|
| 42 |
+
transcription = result["text"]
|
| 43 |
+
|
| 44 |
+
# Clean up the temporary file
|
| 45 |
+
if os.path.exists(temp_path):
|
| 46 |
+
os.remove(temp_path)
|
| 47 |
+
|
| 48 |
+
return jsonify({'transcription': transcription})
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return jsonify({'error': str(e)}), 500
|
| 52 |
classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
|
| 53 |
|
| 54 |
@app.route('/classify', methods=['POST'])
|