-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel_routes.py
55 lines (44 loc) · 2.2 KB
/
model_routes.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
45
46
47
48
49
50
51
52
53
54
55
from flask import Blueprint, request, jsonify
from werkzeug.utils import secure_filename
import os
from audio import transcribe_audio
from chatbot import generate_response # Import generate_response to handle retrieval + response generation
from flask import Response
import json
# Set up embedding model and Flask Blueprint
model_routes = Blueprint('model_routes', __name__)
UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'uploads')
ALLOWED_EXTENSIONS = {'wav', 'mp3', 'ogg', 'flac', 'm4a', 'webm'}
# Ensure the uploads folder exists
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# Function to check allowed file types
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@model_routes.route('/model/upload', methods=['POST'])
def upload_audio():
if 'audio' not in request.files:
return jsonify({'error': 'No audio file provided'}), 400
audio = request.files['audio']
# Validate and save audio file
if audio and allowed_file(audio.filename):
filename = secure_filename(audio.filename)
file_path = os.path.join(UPLOAD_FOLDER, filename)
audio.save(file_path)
try:
# Step 1: Transcribe the audio to get the user's question
transcription = transcribe_audio(file_path)
print(f"Transcription result: {transcription}") # Debugging output
# Step 2: Generate a response using the RAG model (retrieval + response generation in chatbot.py)
response_text = generate_response(transcription) # This now handles retrieval and response generation
print(f"-------------------------------- I am here _________________________") # Debugging output
response = {
'message': 'Audio processed and vector stored successfully',
'filename': response_text
}
return jsonify(response)
except Exception as e:
print(f"Error processing audio: {e}")
return jsonify({'error': str(e)}), 500
else:
return jsonify({'error': 'Invalid file type. Allowed types are: .wav, .mp3, .ogg, .flac, .m4a, .webm'}), 400