|
from flask import Flask, render_template,request, redirect,url_for, jsonify , session |
|
from helper_functions import predict_class ,predict_sentences_class, inference , predict , align_predictions_with_sentences , load_models , load_fr_models |
|
from helper_functions import predict_fr_class, fr_inference , align_fr_predictions_with_sentences , transcribe_speech |
|
import fitz |
|
import os, shutil |
|
import torch |
|
import tempfile |
|
from pydub import AudioSegment |
|
import logging |
|
import torchaudio |
|
|
|
app = Flask(__name__) |
|
app.config['UPLOAD_FOLDER'] = 'static/uploads' |
|
device = "cpu" |
|
|
|
global_model = None |
|
global_neptune = None |
|
global_pipe = None |
|
global_fr_model = None |
|
global_fr_neptune = None |
|
global_fr_pipe = None |
|
global_fr_wav2vec2_processor = None |
|
global_fr_wav2vec2_model = None |
|
|
|
|
|
def init_app(): |
|
global global_model, global_neptune, global_pipe |
|
global global_fr_model, global_fr_neptune, global_fr_wav2vec2_processor, global_fr_wav2vec2_model |
|
|
|
print("Loading English models...") |
|
global_model, global_neptune, global_pipe = load_models() |
|
|
|
print("Loading French models...") |
|
global_fr_model, global_fr_neptune, global_fr_wav2vec2_processor, global_fr_wav2vec2_model = load_fr_models() |
|
|
|
print("Models loaded successfully!") |
|
|
|
init_app() |
|
|
|
@app.route("/") |
|
def home(): |
|
predict_class = "" |
|
class_probabilities = dict() |
|
chart_data = dict() |
|
return render_template('pdf.html', class_probabilities= class_probabilities, predicted_class=predict_class,chart_data = chart_data) |
|
|
|
@app.route('/pdf') |
|
def pdf(): |
|
predict_class = "" |
|
class_probabilities = dict() |
|
chart_data = dict() |
|
sentences_prediction = dict() |
|
return render_template('pdf.html', class_probabilities= class_probabilities, predicted_class=predict_class,chart_data = chart_data,sentences_prediction=sentences_prediction) |
|
|
|
@app.route('/pdf/upload' , methods = ['POST']) |
|
def treatment(): |
|
global global_model , global_neptune |
|
if request.method == 'POST' : |
|
|
|
file = request.files['file'] |
|
filename = file.filename |
|
|
|
|
|
filepath = app.config['UPLOAD_FOLDER'] + "/" + filename |
|
file.save(filepath) |
|
|
|
|
|
pdf_document = fitz.open(filepath) |
|
|
|
|
|
extracted_text = "" |
|
|
|
|
|
for page_num in range(len(pdf_document)): |
|
|
|
page = pdf_document.load_page(page_num) |
|
|
|
|
|
page_text = page.get_text() |
|
|
|
|
|
extracted_text += f"\nPage {page_num + 1}:\n{page_text}" |
|
|
|
|
|
pdf_document.close() |
|
|
|
predicted_class , class_probabilities = predict_class([extracted_text] , global_model) |
|
print(class_probabilities) |
|
|
|
sentences_prediction = predict_sentences_class(extracted_text , global_model) |
|
chart_data = { |
|
'datasets': [{ |
|
'data': list(class_probabilities.values()), |
|
'backgroundColor': [color[2] for color in class_probabilities.keys()], |
|
'borderColor': [color[2] for color in class_probabilities.keys()] |
|
}], |
|
'labels': [label[0] for label in class_probabilities.keys()] |
|
} |
|
print(predict_class) |
|
print(chart_data) |
|
print(sentences_prediction) |
|
|
|
for filename in os.listdir(app.config['UPLOAD_FOLDER']): |
|
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) |
|
try: |
|
if os.path.isfile(file_path) or os.path.islink(file_path): |
|
os.unlink(file_path) |
|
elif os.path.isdir(file_path): |
|
shutil.rmtree(file_path) |
|
except Exception as e: |
|
print('Failed to delete %s. Reason: %s' % (file_path, e)) |
|
return render_template('pdf.html',extracted_text = extracted_text, class_probabilities=class_probabilities, predicted_class=predicted_class, chart_data = chart_data,sentences_prediction=sentences_prediction) |
|
return render_template('pdf.html') |
|
|
|
|
|
|
|
@app.route('/sentence' , methods = ['GET' , 'POST']) |
|
def sentence(): |
|
global global_model |
|
if request.method == 'POST': |
|
|
|
text = [request.form['text']] |
|
predicted_class , class_probabilities = predict_class(text , global_model) |
|
|
|
chart_data = { |
|
'datasets': [{ |
|
'data': list(class_probabilities.values()), |
|
'backgroundColor': [color[2 ] for color in class_probabilities.keys()], |
|
'borderColor': [color[2] for color in class_probabilities.keys()] |
|
}], |
|
'labels': [label[0] for label in class_probabilities.keys()] |
|
} |
|
print(chart_data) |
|
return render_template('response_sentence.html', text=text, class_probabilities=class_probabilities, predicted_class=predicted_class,chart_data = chart_data) |
|
|
|
|
|
return render_template('sentence.html') |
|
|
|
|
|
@app.route("/voice_backup") |
|
def slu_backup(): |
|
input_file = "static/uploads/2022.jep-architectures-neuronales.pdf" |
|
|
|
pdf_document = fitz.open(input_file) |
|
|
|
extracted_text = "" |
|
|
|
for page_num in range(len(pdf_document)): |
|
|
|
page = pdf_document.load_page(page_num) |
|
|
|
|
|
page_text = page.get_text() |
|
|
|
|
|
extracted_text += f"\nPage {page_num + 1}:\n{page_text}" |
|
|
|
|
|
pdf_document.close() |
|
|
|
inference_batch, sentences = inference(extracted_text) |
|
predictions = predict(inference_batch) |
|
sentences_prediction = align_predictions_with_sentences(sentences, predictions) |
|
predicted_class , class_probabilities = predict_class([extracted_text] , global_model) |
|
|
|
chart_data = { |
|
'datasets': [{ |
|
'data': list(class_probabilities.values()), |
|
'backgroundColor': [color[2 ] for color in class_probabilities.keys()], |
|
'borderColor': [color[2] for color in class_probabilities.keys()] |
|
}], |
|
'labels': [label[0] for label in class_probabilities.keys()] |
|
} |
|
print(class_probabilities) |
|
print(chart_data) |
|
print(sentences_prediction) |
|
return render_template('voice_backup.html',extracted_text = extracted_text, class_probabilities=class_probabilities, predicted_class=predicted_class, chart_data = chart_data, sentences_prediction = sentences_prediction) |
|
|
|
logging.basicConfig(level=logging.DEBUG) |
|
|
|
@app.route("/voice", methods=['GET', 'POST']) |
|
def slu(): |
|
global global_neptune, global_pipe, global_model |
|
|
|
if request.method == 'POST': |
|
logging.debug("Received POST request") |
|
audio_file = request.files.get('audio') |
|
|
|
if audio_file: |
|
logging.debug(f"Received audio file: {audio_file.filename}") |
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_audio: |
|
audio_file.save(temp_audio) |
|
temp_audio_path = temp_audio.name |
|
|
|
logging.debug(f"Saved audio to temporary file: {temp_audio_path}") |
|
|
|
try: |
|
|
|
result = global_pipe(temp_audio_path) |
|
extracted_text = result["text"] |
|
logging.debug(f"Transcribed text: {extracted_text}") |
|
|
|
|
|
|
|
|
|
sentences_prediction = predict_sentences_class(extracted_text , global_model) |
|
predicted_class, class_probabilities = predict_class([extracted_text], global_model) |
|
|
|
chart_data = { |
|
'datasets': [{ |
|
'data': list(class_probabilities.values()), |
|
'backgroundColor': [color[2] for color in class_probabilities.keys()], |
|
'borderColor': [color[2] for color in class_probabilities.keys()] |
|
}], |
|
'labels': [label[0] for label in class_probabilities.keys()] |
|
} |
|
|
|
response_data = { |
|
'extracted_text': extracted_text, |
|
'class_probabilities' : class_probabilities, |
|
'predicted_class': predicted_class, |
|
'chart_data': chart_data, |
|
'sentences_prediction': sentences_prediction |
|
} |
|
logging.debug(f"Prepared response data: {response_data}") |
|
|
|
return render_template('voice.html', |
|
class_probabilities= class_probabilities, |
|
predicted_class= predicted_class, |
|
chart_data= chart_data, |
|
sentences_prediction=sentences_prediction) |
|
|
|
except Exception as e: |
|
logging.error(f"Error processing audio: {str(e)}") |
|
return jsonify({'error': str(e)}), 500 |
|
|
|
finally: |
|
|
|
os.unlink(temp_audio_path) |
|
|
|
else: |
|
logging.error("No audio file received") |
|
return jsonify({'error': 'No audio file received'}), 400 |
|
|
|
|
|
logging.debug("Received GET request") |
|
return render_template('voice.html', |
|
class_probabilities={}, |
|
predicted_class=[""], |
|
chart_data={}, |
|
sentences_prediction={}) |
|
|
|
|
|
@app.route('/pdf_fr') |
|
def pdf_fr(): |
|
predict_class = "" |
|
class_probabilities = dict() |
|
chart_data = dict() |
|
return render_template('pdf_fr.html', class_probabilities= class_probabilities, predicted_class=predict_class,chart_data = chart_data) |
|
|
|
@app.route('/pdf_fr/upload' , methods = ['POST']) |
|
def treatment_fr(): |
|
global global_fr_neptune , global_fr_model |
|
if request.method == 'POST' : |
|
|
|
file = request.files['file'] |
|
filename = file.filename |
|
|
|
|
|
filepath = app.config['UPLOAD_FOLDER'] + "/" + filename |
|
file.save(filepath) |
|
|
|
|
|
pdf_document = fitz.open(filepath) |
|
|
|
|
|
extracted_text = "" |
|
|
|
|
|
for page_num in range(len(pdf_document)): |
|
|
|
page = pdf_document.load_page(page_num) |
|
|
|
|
|
page_text = page.get_text() |
|
|
|
|
|
extracted_text += f"\nPage {page_num + 1}:\n{page_text}" |
|
|
|
|
|
pdf_document.close() |
|
|
|
|
|
|
|
sentences_prediction = predict_sentences_class(extracted_text , global_fr_model) |
|
|
|
predicted_class , class_probabilities = predict_fr_class([extracted_text] , global_fr_model) |
|
|
|
chart_data = { |
|
'datasets': [{ |
|
'data': list(class_probabilities.values()), |
|
'backgroundColor': [color[2] for color in class_probabilities.keys()], |
|
'borderColor': [color[2] for color in class_probabilities.keys()] |
|
}], |
|
'labels': [label[0] for label in class_probabilities.keys()] |
|
} |
|
print(predict_class) |
|
print(chart_data) |
|
|
|
for filename in os.listdir(app.config['UPLOAD_FOLDER']): |
|
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) |
|
try: |
|
if os.path.isfile(file_path) or os.path.islink(file_path): |
|
os.unlink(file_path) |
|
elif os.path.isdir(file_path): |
|
shutil.rmtree(file_path) |
|
except Exception as e: |
|
print('Failed to delete %s. Reason: %s' % (file_path, e)) |
|
return render_template('pdf_fr.html',extracted_text = extracted_text, class_probabilities=class_probabilities, predicted_class=predicted_class, chart_data = chart_data, sentences_prediction=sentences_prediction) |
|
return render_template('pdf_fr.html') |
|
|
|
@app.route('/sentence_fr' , methods = ['GET' , 'POST']) |
|
def sentence_fr(): |
|
global global_fr_model |
|
if request.method == 'POST': |
|
|
|
text = [request.form['text']] |
|
predicted_class , class_probabilities = predict_fr_class(text , global_fr_model) |
|
|
|
chart_data = { |
|
'datasets': [{ |
|
'data': list(class_probabilities.values()), |
|
'backgroundColor': [color[2 ] for color in class_probabilities.keys()], |
|
'borderColor': [color[2] for color in class_probabilities.keys()] |
|
}], |
|
'labels': [label[0] for label in class_probabilities.keys()] |
|
} |
|
print(predicted_class) |
|
print(chart_data) |
|
return render_template('response_fr_sentence.html', text=text, class_probabilities=class_probabilities, predicted_class=predicted_class,chart_data = chart_data) |
|
|
|
|
|
return render_template('sentence_fr.html') |
|
|
|
from pydub import AudioSegment |
|
import io |
|
|
|
@app.route("/voice_fr", methods=['GET', 'POST']) |
|
def slu_fr(): |
|
global global_fr_neptune, global_fr_model, global_fr_wav2vec2_processor, global_fr_wav2vec2_model |
|
|
|
if request.method == 'POST': |
|
logging.info("Received POST request for /voice_fr") |
|
audio_file = request.files.get('audio') |
|
|
|
if audio_file: |
|
logging.info(f"Received audio file: {audio_file.filename}") |
|
|
|
|
|
audio_data = audio_file.read() |
|
|
|
|
|
try: |
|
audio = AudioSegment.from_file(io.BytesIO(audio_data)) |
|
audio = audio.set_frame_rate(16000).set_channels(1) |
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_audio: |
|
audio.export(temp_audio.name, format="wav") |
|
temp_audio_path = temp_audio.name |
|
|
|
logging.info(f"Converted and saved audio to temporary file: {temp_audio_path}") |
|
except Exception as e: |
|
logging.error(f"Error converting audio: {str(e)}") |
|
return jsonify({'error': 'Unable to process audio file'}), 400 |
|
|
|
try: |
|
|
|
extracted_text = transcribe_speech(temp_audio_path, global_fr_wav2vec2_processor, global_fr_wav2vec2_model) |
|
logging.info(f"Transcribed text: {extracted_text}") |
|
|
|
|
|
|
|
|
|
sentences_prediction = predict_sentences_class(extracted_text , global_fr_model) |
|
predicted_class, class_probabilities = predict_fr_class([extracted_text], global_fr_model) |
|
|
|
chart_data = { |
|
'datasets': [{ |
|
'data': list(class_probabilities.values()), |
|
'backgroundColor': [color[2] for color in class_probabilities.keys()], |
|
'borderColor': [color[2] for color in class_probabilities.keys()] |
|
}], |
|
'labels': [label[0] for label in class_probabilities.keys()] |
|
} |
|
|
|
response_data = { |
|
'extracted_text': extracted_text, |
|
'class_probabilities': class_probabilities, |
|
'predicted_class': predicted_class, |
|
'chart_data': chart_data, |
|
'sentences_prediction': sentences_prediction |
|
} |
|
logging.info(f"Prepared response data: {response_data}") |
|
|
|
return render_template('voice_fr.html', |
|
class_probabilities=class_probabilities, |
|
predicted_class=predicted_class, |
|
chart_data=chart_data, |
|
sentences_prediction=sentences_prediction) |
|
|
|
except Exception as e: |
|
logging.error(f"Error processing audio: {str(e)}") |
|
return jsonify({'error': str(e)}), 500 |
|
|
|
finally: |
|
|
|
os.unlink(temp_audio_path) |
|
|
|
else: |
|
logging.error("No audio file received") |
|
return jsonify({'error': 'No audio file received'}), 400 |
|
|
|
|
|
logging.info("Received GET request for /voice_fr") |
|
return render_template('voice_fr.html', |
|
class_probabilities={}, |
|
predicted_class=[""], |
|
chart_data={}, |
|
sentences_prediction={}) |
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |