-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
44 lines (32 loc) · 1.27 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from flask import Flask, request, jsonify
from flask_cors import CORS
import speech_recognition as sr
import requests
app = Flask(__name__)
CORS(app)
@app.route('/transcribe', methods=['POST'])
def transcribe_audio():
try:
# Get the audio URL from the request
audio_url = request.json['audio_url']
# Fetch the audio file from the remote URL
audio_response = requests.get(audio_url, stream=True)
audio_response.raise_for_status()
# Initialize the SpeechRecognition recognizer
recognizer = sr.Recognizer()
# Open the audio stream from the fetched data
with sr.AudioFile(audio_response.raw) as audio_file:
audio_data = recognizer.record(audio_file)
# Perform Speech-to-Text
transcript = recognizer.recognize_google(audio_data)
return jsonify({'transcript': transcript})
except requests.exceptions.RequestException as e:
return jsonify({'error': f'Request error: {str(e)}'}), 500
except Exception as e:
return jsonify({'error': str(e)}), 500
# New endpoint for the root path
@app.route('/')
def home():
return '<html><body><p>Hello, this is a simple Flask application!</p></body></html>'
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')