Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,15 @@
|
|
1 |
from flask import Flask, render_template, request, jsonify
|
|
|
|
|
|
|
2 |
import speech_recognition as sr
|
3 |
|
4 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
@app.route('/')
|
7 |
def index():
|
@@ -16,25 +24,36 @@ def send_message():
|
|
16 |
def echo_response(user_input):
|
17 |
return user_input
|
18 |
|
19 |
-
|
20 |
-
def process_audio():
|
21 |
-
if 'audio' not in request.files:
|
22 |
-
return "No audio file provided", 400
|
23 |
-
|
24 |
-
audio_file = request.files['audio']
|
25 |
recognizer = sr.Recognizer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
text = recognizer.recognize_google(audio)
|
32 |
-
print("Transcribed Text:", text)
|
33 |
-
return text
|
34 |
-
except sr.UnknownValueError:
|
35 |
-
return "Could not understand audio", 400
|
36 |
-
except sr.RequestError as e:
|
37 |
-
return f"Could not request results from Google Speech Recognition service; {e}", 500
|
38 |
|
39 |
if __name__ == '__main__':
|
40 |
app.run(host="0.0.0.0", port=7860)
|
|
|
1 |
from flask import Flask, render_template, request, jsonify
|
2 |
+
import os
|
3 |
+
from werkzeug.utils import secure_filename
|
4 |
+
from pydub import AudioSegment
|
5 |
import speech_recognition as sr
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
+
UPLOAD_FOLDER = 'uploads'
|
9 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
10 |
+
|
11 |
+
if not os.path.exists(UPLOAD_FOLDER):
|
12 |
+
os.makedirs(UPLOAD_FOLDER)
|
13 |
|
14 |
@app.route('/')
|
15 |
def index():
|
|
|
24 |
def echo_response(user_input):
|
25 |
return user_input
|
26 |
|
27 |
+
def respond(audio_path):
|
|
|
|
|
|
|
|
|
|
|
28 |
recognizer = sr.Recognizer()
|
29 |
+
audio = AudioSegment.from_file(audio_path)
|
30 |
+
audio.export("temp.wav", format="wav")
|
31 |
+
with sr.AudioFile("temp.wav") as source:
|
32 |
+
audio_data = recognizer.record(source)
|
33 |
+
text = recognizer.recognize_google(audio_data)
|
34 |
+
os.remove("temp.wav")
|
35 |
+
return text
|
36 |
+
|
37 |
+
@app.route('/upload', methods=['POST'])
|
38 |
+
def upload_file():
|
39 |
+
if 'file' not in request.files:
|
40 |
+
return jsonify({'error': 'No file part'})
|
41 |
+
|
42 |
+
file = request.files['file']
|
43 |
+
if file.filename == '':
|
44 |
+
return jsonify({'error': 'No selected file'})
|
45 |
+
|
46 |
+
filename = secure_filename(file.filename)
|
47 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
48 |
+
file.save(file_path)
|
49 |
+
|
50 |
+
# Process the audio file and get the response text
|
51 |
+
response_text = respond(file_path)
|
52 |
|
53 |
+
# Remove the audio file after processing
|
54 |
+
os.remove(file_path)
|
55 |
|
56 |
+
return jsonify({'text': response_text})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
if __name__ == '__main__':
|
59 |
app.run(host="0.0.0.0", port=7860)
|