Skip to content

Commit

Permalink
lint black 2
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew0022 committed Dec 4, 2023
1 parent 4028046 commit 6df642a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 42 deletions.
42 changes: 14 additions & 28 deletions web-app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
"""
Expand All @@ -54,37 +44,33 @@ 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:
return jsonify({"status": "error", "message": "No audio file provided"})

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)
23 changes: 9 additions & 14 deletions web-app/app_tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
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")
print("Response status code:", response.status_code)
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:
Expand All @@ -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:
Expand All @@ -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"])

0 comments on commit 6df642a

Please sign in to comment.