mattrguerra/transcription-pipelinemainPrivate on GitHub, published here in full

test_deepinfra.py

Language Python3.3 KB87 lines

tests/test_deepinfra.py

  1. """The DeepInfra engine against a local stand-in server: request shape, errors, normalization."""
  2. import json
  3. import threading
  4. from http.server import BaseHTTPRequestHandler, HTTPServer
  5. import pytest
  6. from transcription_pipeline.engines import deepinfra
  7. class Stub:
  8. status = 200
  9. body = {}
  10. seen = {}
  11. class Handler(BaseHTTPRequestHandler):
  12. def do_POST(self):
  13. n = int(self.headers["Content-Length"])
  14. Stub.seen = {"auth": self.headers["Authorization"], "ctype": self.headers["Content-Type"],
  15. "body": self.rfile.read(n), "path": self.path}
  16. payload = json.dumps(Stub.body).encode()
  17. self.send_response(Stub.status)
  18. self.send_header("Content-Type", "application/json")
  19. self.send_header("Content-Length", str(len(payload)))
  20. self.end_headers()
  21. self.wfile.write(payload)
  22. def log_message(self, *a):
  23. pass
  24. @pytest.fixture
  25. def server():
  26. srv = HTTPServer(("127.0.0.1", 0), Handler)
  27. threading.Thread(target=srv.serve_forever, daemon=True).start()
  28. yield f"http://127.0.0.1:{srv.server_address[1]}/v1/inference/openai/whisper-large-v3-turbo"
  29. srv.shutdown()
  30. @pytest.fixture
  31. def clip(tmp_path):
  32. p = tmp_path / "call.flac"
  33. p.write_bytes(b"fLaC-not-really")
  34. return p
  35. def test_request_shape_and_segments(server, clip):
  36. Stub.status, Stub.body = 200, {"text": " Hi there. Bye.", "language": "en", "segments": [
  37. {"start": 0.0, "end": 1.2, "text": " Hi there."}, {"start": 1.2, "end": 2.0, "text": " "},
  38. {"start": 2.0, "end": 2.6, "text": " Bye."}]}
  39. out = deepinfra.transcribe(clip, "tok-123", language="en", api_url=server)
  40. assert Stub.seen["auth"] == "bearer tok-123"
  41. assert Stub.seen["ctype"].startswith("multipart/form-data; boundary=")
  42. body = Stub.seen["body"]
  43. assert b'name="audio"; filename="call.flac"' in body and b"fLaC-not-really" in body
  44. assert b'name="language"\r\n\r\nen\r\n' in body
  45. assert out["segments"] == [{"start": 0.0, "end": 1.2, "text": "Hi there."},
  46. {"start": 2.0, "end": 2.6, "text": "Bye."}]
  47. assert out["language"] == "en" and "DeepInfra" in out["model"]
  48. def test_text_only_response_becomes_one_segment(server, clip):
  49. Stub.status, Stub.body = 200, {"text": "Just text.", "segments": None}
  50. out = deepinfra.transcribe(clip, "t", api_url=server)
  51. assert out["segments"] == [{"start": 0.0, "end": 0.0, "text": "Just text."}]
  52. assert b'name="language"' not in Stub.seen["body"]
  53. @pytest.mark.parametrize("code,needle", [(401, "rejected the token"), (403, "rejected the token"),
  54. (429, "out of credit"), (413, "too large"),
  55. (500, "DeepInfra error 500")])
  56. def test_error_mapping(server, clip, code, needle):
  57. Stub.status, Stub.body = code, {"detail": "nope"}
  58. with pytest.raises(deepinfra.EngineError, match=needle):
  59. deepinfra.transcribe(clip, "t", api_url=server)
  60. def test_missing_token(clip):
  61. with pytest.raises(deepinfra.EngineError, match="No DeepInfra token"):
  62. deepinfra.transcribe(clip, None)
  63. def test_unreachable(clip):
  64. with pytest.raises(deepinfra.EngineError, match="Could not reach"):
  65. deepinfra.transcribe(clip, "t", api_url="http://127.0.0.1:9/x", timeout=2)