Commit b716a0e4 authored by B.A.D.A.Sathsarani's avatar B.A.D.A.Sathsarani

Upload New File

parent 201aec00
from flask import Flask, request, send_file, jsonify, make_response, after_this_request
import os
from chatbot import ChatBot
app = Flask(__name__)
UPLOAD_FOLDER = os.path.join(os.path.dirname(
os.path.abspath(__file__)), 'uploads')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/chatbot', methods=['POST'])
def process_audio():
try:
script_folder = os.path.dirname(os.path.abspath(__file__))
audio_file = request.files['audio']
language = request.form['language']
grade = int(request.form['grade'])
audio_path = os.path.join(
app.config['UPLOAD_FOLDER'], audio_file.filename)
audio_file.save(audio_path)
chatbot = ChatBot(audio_path, language, grade)
response_text = chatbot.run_chatbot()
res_path = os.path.join(script_folder, 'response')
processed_audio_path = os.path.join(res_path, 'output.mp3')
if os.path.exists(processed_audio_path):
# Prepare the response data
response_data = {
'response_text': response_text,
'audio_path': processed_audio_path # Include the audio file path
}
# Send the audio file as an attachment
@after_this_request
def add_file_to_response(response):
with open(processed_audio_path, 'rb') as audio_file:
response.data = audio_file.read()
response.headers['Content-Disposition'] = f'attachment; filename=output.mp3'
return response
# Return the JSON response data
return jsonify(response_data)
else:
# If audio file doesn't exist, return response text only
return jsonify({'response_text': response_text})
except Exception as e:
return jsonify({'error_message': str(e)})+
#if __name__ == '__main__':
# app.run(port=5000, host='192.168.8.133', debug=True)
#
# # ##when ngrock up
if __name__ == '__main__':
app.run(port=5000, debug=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment