-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_setup.py
More file actions
147 lines (112 loc) · 4.38 KB
/
test_setup.py
File metadata and controls
147 lines (112 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""Utility script to validate the MediaTranscript development environment.
This script checks the availability of FFmpeg, verifies key Python packages,
and (optionally) exercises an OpenAI-compatible text summarization endpoint.
Use it after setting up the environment to confirm everything is wired up.
"""
from __future__ import annotations
import argparse
import os
import subprocess
import sys
from typing import Tuple
def check_ffmpeg() -> Tuple[bool, str]:
"""Return (status, message) indicating FFmpeg availability."""
try:
result = subprocess.run(
["ffmpeg", "-version"],
capture_output=True,
text=True,
check=True,
)
except FileNotFoundError:
return False, "FFmpeg executable not found in PATH."
except subprocess.CalledProcessError as exc:
message = exc.stderr.strip() or str(exc)
return False, f"FFmpeg command failed: {message}"
first_line = result.stdout.strip().splitlines()[0]
return True, first_line
def check_python_packages() -> Tuple[bool, str]:
"""Verify that Whisper, Flask, and the OpenAI client are importable."""
package_map = {
"whisper": "openai-whisper",
"flask": "Flask",
"openai": "openai",
}
missing = []
for module_name, package_name in package_map.items():
try:
__import__(module_name)
except ImportError as exc:
missing.append(f"{package_name} ({exc})")
if missing:
detail = ", ".join(missing)
return False, f"Missing or broken Python packages: {detail}"
return True, "Required Python packages are importable."
def try_openai_summary(text: str, model: str) -> Tuple[bool, str]:
"""Attempt to call an OpenAI-compatible endpoint to summarize text."""
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
return False, "OPENAI_API_KEY not set; skipping API request."
try:
from openai import OpenAI
except ImportError as exc: # pragma: no cover - handled earlier but defensive
return False, f"OpenAI client unavailable: {exc}"
base_url = os.getenv("OPENAI_BASE_URL")
client_kwargs = {"api_key": api_key}
if base_url:
client_kwargs["base_url"] = base_url
client = OpenAI(**client_kwargs)
try:
response = client.responses.create(
model=model,
input=(
"Please provide a concise summary for the following text:\n\n"
f"{text}"
),
max_output_tokens=120,
)
except Exception as exc: # Broad to surface SDK/server issues clearly
return False, f"OpenAI-compatible request failed: {exc}"
summary = getattr(response, "output_text", "").strip()
if not summary:
return False, "Received empty response from the API."
return True, summary
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Validate dependencies and test OpenAI-compatible summarization API."
)
parser.add_argument(
"--text",
default=(
"MediaTranscript extracts audio, transcribes speech with Whisper, "
"summarizes content using GPT, and emits downloadable reports."
),
help="Sample text to summarize via the OpenAI-compatible API.",
)
parser.add_argument(
"--model",
default=os.getenv("OPENAI_MODEL", "gpt-4o-mini"),
help="Model name to use for the API call (default from OPENAI_MODEL or gpt-4o-mini).",
)
parser.add_argument(
"--fail-on-missing-api",
action="store_true",
help="Exit with code 1 if API credentials are missing or the call fails.",
)
return parser.parse_args()
def main() -> int:
args = parse_args()
ffmpeg_ok, ffmpeg_msg = check_ffmpeg()
print(f"FFmpeg status : {'OK' if ffmpeg_ok else 'FAIL'} - {ffmpeg_msg}")
packages_ok, packages_msg = check_python_packages()
print(f"Python packages: {'OK' if packages_ok else 'FAIL'} - {packages_msg}")
api_ok, api_msg = try_openai_summary(args.text, args.model)
status = "OK" if api_ok else "SKIP" if "not set" in api_msg.lower() else "FAIL"
print(f"OpenAI API : {status} - {api_msg}")
if not ffmpeg_ok or not packages_ok:
return 1
if args.fail_on_missing_api and not api_ok:
return 1
return 0
if __name__ == "__main__":
sys.exit(main())