Skip to content

Commit

Permalink
feat: add specific ask web service for web client
Browse files Browse the repository at this point in the history
  • Loading branch information
msadeqsirjani committed Sep 12, 2023
1 parent fc4e6d8 commit f4a7ded
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import io
import os
import base64
from uuid import uuid4
from config import MEDIA_DIR, ALLOWED_LANGUAGES
from utilities.file_extensions import allowed_file, get_file_extension
from flask import Flask, send_file, jsonify, request, url_for, after_this_request
from flask import Flask, send_file, jsonify, request, url_for
from werkzeug.utils import secure_filename
from voice_engine import speech_to_text, text_to_speech
from brain_engine import gpt_engine
Expand Down Expand Up @@ -97,5 +98,67 @@ def ask(language: str):
"status": "error"}), 400


@app.route("/ask/web/<language>", methods=["POST"])
def ask_web(language: str):
data = request.get_json()

file_name = data['file_name']
file_content = data['file_content']

if file_name == "":
return jsonify({"data": None,
"message": "No selected file is available",
"status": "error"}), 400

if file_name and file_content and allowed_file(file_name=file_name):
secure_file_name = secure_filename(file_name)
secure_file_extension = get_file_extension(file_name=secure_file_name)
secure_file_path = os.path.join(MEDIA_DIR, f"{str(uuid4())}{secure_file_extension}")

print(secure_file_path)

with open(secure_file_path, 'wb') as file:
file.write(base64.b64decode(file_content))

prompt = speech_to_text.convert(file_path=secure_file_path,
language=language)

if prompt is None:
return jsonify({"data": None,
"message": "Google Speech Recognition could not understand audio",
"status": "success"}), 400

os.remove(secure_file_path)

engine = gpt_engine.GptEngine()

answer_text = engine.command(prompt=prompt)

exact_answer_voice_name = text_to_speech.convert(text=answer_text,
language=language)

data = {
"prompt": prompt,
"prompt_language": language,
"answer": {
"text": answer_text,
"file_name": exact_answer_voice_name,
"answer_language": language,
"voice_path": url_for("download", file_name=exact_answer_voice_name, _external=True, _scheme="http")
}
}

return jsonify({"data": data,
"message": None,
"status": "success"}), 200

# return jsonify({"data": None,
# "message": "DONE",
# "status": "success"}), 200
else:
return jsonify({"data": None,
"message": "File type not allowed. Only mp3 and wav files are acceptable",
"status": "error"}), 400

if __name__ == '__main__':
app.run()

0 comments on commit f4a7ded

Please sign in to comment.