from flask import Flask, render_template, request, jsonify import os from werkzeug.utils import secure_filename from pydub import AudioSegment import speech_recognition as sr app = Flask(__name__) UPLOAD_FOLDER = 'uploads' app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) @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 def respond(audio_path): recognizer = sr.Recognizer() audio = AudioSegment.from_file(audio_path) audio.export("temp.wav", format="wav") with sr.AudioFile("temp.wav") as source: audio_data = recognizer.record(source) text = recognizer.recognize_google(audio_data) os.remove("temp.wav") print(text) return text @app.route('/upload', methods=['POST']) def upload_file(): if 'file' not in request.files: return jsonify({'error': 'No file part'}) file = request.files['file'] if file.filename == '': return jsonify({'error': 'No selected file'}) filename = secure_filename(file.filename) file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(file_path) # Process the audio file and get the response text response_text = respond(file_path) # Remove the audio file after processing os.remove(file_path) return jsonify({'text': response_text}) if __name__ == '__main__': app.run(host="0.0.0.0", port=7860)