speech-to-text / app.py
Nechba's picture
Update app.py
5020140 verified
raw
history blame
2.21 kB
from flask import Flask, request, jsonify
from transformers import pipeline
from transformers import AutoTokenizer, AutoModelForTokenClassification
import whisper
import os
import tempfile
import ffmpeg
app = Flask(__name__)
# Initialize Whisper model
whisper_model = whisper.load_model("small") # Renamed variable
# Initialize Emotion Classifier
classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
# Initialize NER pipeline
ner_tokenizer = AutoTokenizer.from_pretrained("dslim/bert-base-NER")
ner_model = AutoModelForTokenClassification.from_pretrained("dslim/bert-base-NER") # Renamed variable
ner_pipeline = pipeline("ner", model=ner_model, tokenizer=ner_tokenizer) # Renamed variable
@app.route('/transcribe', methods=['POST'])
def transcribe():
# Get the audio file from the request
if 'audio_file' not in request.files:
return jsonify({"error": "No audio file provided"}), 400
audio_file = request.files['audio_file']
# Save the file to a temporary location
audio_file_path = "temp_audio.mp3"
audio_file.save(audio_file_path)
# Transcribe the audio to text
result = whisper_model.transcribe(audio_file_path)
return jsonify({"text": result["text"]})
@app.route('/classify', methods=['POST'])
def classify():
try:
data = request.get_json()
if 'text' not in data:
return jsonify({"error": "Missing 'text' field"}), 400
text = data['text']
result = classifier(text)
return jsonify(result)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/ner', methods=['POST'])
def ner_endpoint():
try:
data = request.get_json()
text = data.get("text", "")
# Use the renamed ner_pipeline
ner_results = ner_pipeline(text)
words_and_entities = [
{"word": result['word'], "entity": result['entity']}
for result in ner_results
]
return jsonify({"entities": words_and_entities})
except Exception as e:
return jsonify({"error": str(e)}), 500