flask-bot-4 / app.py
tonyassi's picture
Update app.py
362a65a verified
raw
history blame
1.18 kB
from flask import Flask, render_template, request, jsonify
import speech_recognition as sr
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/send_message', methods=['POST'])
def send_message():
user_input = request.form['user_input']
response = echo_response(user_input)
return jsonify({'response': response, 'user_input': user_input})
def echo_response(user_input):
return user_input
@app.route('/process_audio', methods=['POST'])
def process_audio():
if 'audio' not in request.files:
return "No audio file provided", 400
audio_file = request.files['audio']
recognizer = sr.Recognizer()
with sr.AudioFile(audio_file) as source:
audio = recognizer.record(source)
try:
text = recognizer.recognize_google(audio)
print("Transcribed Text:", text)
return text
except sr.UnknownValueError:
return "Could not understand audio", 400
except sr.RequestError as e:
return f"Could not request results from Google Speech Recognition service; {e}", 500
if __name__ == '__main__':
app.run(host="0.0.0.0", port=7860)