diff --git a/web-app/app.py b/web-app/app.py index 67a9fd7..b60889f 100644 --- a/web-app/app.py +++ b/web-app/app.py @@ -3,43 +3,33 @@ """ import os -from flask import ( - Flask, - Response, - render_template, - request, - redirect, - send_file, - jsonify, -) -import sys +from flask import Flask, render_template, request, jsonify import pymongo from pymongo import MongoClient import requests - app = Flask(__name__, template_folder="templates") +# Connect to MongoDB +client = MongoClient("mongodb://localhost:27017/") +db = client["ml_database"] +collection = db["transcription"] @app.route("/test_render_template") def test_render_template(): + """ + Request received for /test_render_template + """ print("Request received for /test_render_template") return render_template("root.html") - -client = MongoClient("mongodb://localhost:27017/") -db = client["ml_databse"] -collection = db["transcription"] - - @app.route("/") def root_page(): """ - template to render root page + Template to render root page """ return render_template("root.html") - @app.route("/results") def display_results(): """ @@ -54,11 +44,10 @@ def display_results(): "results.html", transcription_result=my_transcript, activePage="results.html" ) - @app.route("/analyzeData", methods=["POST"]) def analyze_data(): """ - Function to send genreated audio file to the machine learning client + Function to send generated audio file to the machine learning client """ try: if "audio" not in request.files: @@ -66,25 +55,22 @@ def analyze_data(): audio_file = request.files["audio"] ml_client_url = "http://127.0.0.1:5001/analyzeAudio" + # Use the converted audio file - response = requests.post( - ml_client_url, files={"audio": audio_file}, timeout=100 - ) + response = requests.post(ml_client_url, files={"audio": audio_file}, timeout=100) print("sent over") + if response.status_code == 200: result = response.json() return jsonify(result) else: return ( - jsonify( - {"error": "Failed to send and process audio. Please try again."} - ), + jsonify({"error": "Failed to send and process audio. Please try again."}), 500, ) except FileNotFoundError as e: return jsonify({"status": "error", "message": f"File not found: {str(e)}"}) - if __name__ == "__main__": app.run(host="0.0.0.0", port=int(os.getenv("PORT", "5000")), debug=True) diff --git a/web-app/app_tests/test_app.py b/web-app/app_tests/test_app.py index d3c97c0..63308ff 100644 --- a/web-app/app_tests/test_app.py +++ b/web-app/app_tests/test_app.py @@ -1,21 +1,20 @@ -import sys -import os import tempfile import pytest from flask import jsonify -from flask import Flask -from flask import render_template -from app import app, collection +from app import app -client = app.test_client() +# Remove unused imports +# import sys +# import os +# Rename either the fixture or the variable to avoid confusion +app_client = app.test_client() @pytest.fixture def client(): app.config["TESTING"] = True - with app.test_client() as client: - yield client - + with app.test_client() as test_client: + yield test_client def test_root_page(client): response = client.get("/test_render_template") @@ -23,7 +22,6 @@ def test_root_page(client): print("Response data:", response.data) assert b"Recording audio..." in response.data - def test_analyze_data(client, monkeypatch): def mock_post(*args, **kwargs): class MockResponse: @@ -44,13 +42,11 @@ def status_code(self): assert response.status_code == 200 assert b"mocked" in response.data - def test_analyze_data_no_audio(client): response = client.post("/analyzeData") assert response.status_code == 200 assert b"No audio file provided" in response.data - def test_analyze_data_failed_request(client, monkeypatch): def mock_post(*args, **kwargs): class MockResponse: @@ -71,6 +67,5 @@ def status_code(self): assert response.status_code == 500 assert b"Failed to send and process audio" in response.data - if __name__ == "__main__": - pytest.main(["-v", "test_app.py", "--cov=web-app"]) + pytest.main(["-v", "test_app.py", "--cov=web-app"]) \ No newline at end of file