from flask import Flask, render_template, request, jsonify
import requests
app = Flask(__name__)
YANDEX_SPEECHKIT_API_KEY = ""
@app.route('/')
def index():
return render_template('index.html')
@app.route('/process_voice', methods=['POST'])
def process_voice():
audio_data = request.files['audio']
speech_to_text_url = "https://stt.api.cloud.yandex.net/speech/v1/stt:recognize"
headers = {
"Authorization": f"Api-Key {YANDEX_SPEECHKIT_API_KEY}",
}
try:
response = requests.post(speech_to_text_url, headers=headers, files={"file": audio_data})
response.raise_for_status() # Поднимет ошибку, если статус не 2xx
# Получение ответа в формате JSON
result = response.json()
print(f"Response from Yandex: {result}")
if 'result' in result:
text = result['result']
return jsonify({'recognized_text': text})
else:
print(f"Error in response: {result}")
return jsonify({'error': f"Failed to recognize speech. Error details: {result}"}), 500
except requests.exceptions.RequestException as e:
error_msg = e.response.json() if e.response else str(e)
print(f"Request failed: {error_msg}")
return jsonify({'error': f"Failed to recognize speech. Request failed: {error_msg}"}), 500
if __name__ == '__main__':
app.run(debug=True)
{
"error_code": "UNAUTHORIZED",
"error_message": "rpc error: code = Unauthenticated desc = IAM token or API key has to be passed in request"
}