Commit ·
041a406
1
Parent(s): 1288aeb
Revert "separate tmp dir scoring for organics (#85)"
Browse files- services/scoring/server.py +10 -19
services/scoring/server.py
CHANGED
|
@@ -144,15 +144,13 @@ def load_quality_model():
|
|
| 144 |
model.eval()
|
| 145 |
return model
|
| 146 |
|
| 147 |
-
async def download_video(video_url: str, verbose: bool
|
| 148 |
"""
|
| 149 |
Download a video from the given URL and save it to a temporary file.
|
| 150 |
|
| 151 |
Args:
|
| 152 |
video_url (str): The URL of the video to download.
|
| 153 |
verbose (bool): Whether to show download progress.
|
| 154 |
-
dir_path (str | None): Optional directory path where the video should be saved.
|
| 155 |
-
If None, a system temporary directory is used.
|
| 156 |
|
| 157 |
Returns:
|
| 158 |
tuple[str, float]: A tuple containing the path to the downloaded video file
|
|
@@ -162,15 +160,8 @@ async def download_video(video_url: str, verbose: bool, dir_path: str | None = N
|
|
| 162 |
Exception: If the download fails or takes longer than the timeout.
|
| 163 |
"""
|
| 164 |
try:
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
os.makedirs(dir_path, exist_ok=True)
|
| 168 |
-
with tempfile.NamedTemporaryFile(suffix=".mp4", dir=dir_path, delete=False) as vid_temp:
|
| 169 |
-
file_path = vid_temp.name # Path to the temporary file
|
| 170 |
-
else:
|
| 171 |
-
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as vid_temp:
|
| 172 |
-
file_path = vid_temp.name # Path to the temporary file
|
| 173 |
-
|
| 174 |
if verbose:
|
| 175 |
logger.info(f"Downloading video from {video_url} to {file_path}")
|
| 176 |
|
|
@@ -187,7 +178,8 @@ async def download_video(video_url: str, verbose: bool, dir_path: str | None = N
|
|
| 187 |
async for chunk in response.content.iter_chunked(2 * 1024 * 1024):
|
| 188 |
f.write(chunk)
|
| 189 |
|
| 190 |
-
|
|
|
|
| 191 |
|
| 192 |
if not os.path.exists(file_path) or os.path.getsize(file_path) == 0:
|
| 193 |
raise Exception(f"Download failed or file is empty: {file_path}")
|
|
@@ -1483,14 +1475,14 @@ async def score_organics_upscaling(request: OrganicsUpscalingScoringRequest) ->
|
|
| 1483 |
length_scores = []
|
| 1484 |
final_scores = []
|
| 1485 |
reasons = []
|
| 1486 |
-
|
| 1487 |
distorted_video_paths = []
|
| 1488 |
for dist_url in request.distorted_urls:
|
| 1489 |
if len(dist_url) < 10:
|
| 1490 |
distorted_video_paths.append(None)
|
| 1491 |
continue
|
| 1492 |
try:
|
| 1493 |
-
path, download_time = await download_video(dist_url, request.verbose
|
| 1494 |
distorted_video_paths.append(path)
|
| 1495 |
except Exception as e:
|
| 1496 |
logger.error(f"failed to download distorted video: {dist_url}, error: {e}")
|
|
@@ -1526,7 +1518,7 @@ async def score_organics_upscaling(request: OrganicsUpscalingScoringRequest) ->
|
|
| 1526 |
final_scores.append(-1) # -1 means skip, don't penalize
|
| 1527 |
continue
|
| 1528 |
|
| 1529 |
-
ref_path, download_time = await download_video(ref_url, request.verbose
|
| 1530 |
|
| 1531 |
# Validate reference video using ffprobe (avoids AV1 warnings)
|
| 1532 |
if not is_valid_video(ref_path):
|
|
@@ -1816,7 +1808,6 @@ async def score_organics_compression(request: OrganicsCompressionScoringRequest)
|
|
| 1816 |
compression_rates = []
|
| 1817 |
final_scores = []
|
| 1818 |
reasons = []
|
| 1819 |
-
tmp_path = "/tmp_comp_organics"
|
| 1820 |
|
| 1821 |
distorted_video_paths = []
|
| 1822 |
for dist_url in request.distorted_urls:
|
|
@@ -1824,7 +1815,7 @@ async def score_organics_compression(request: OrganicsCompressionScoringRequest)
|
|
| 1824 |
distorted_video_paths.append(None)
|
| 1825 |
continue
|
| 1826 |
try:
|
| 1827 |
-
path, download_time = await download_video(dist_url, request.verbose
|
| 1828 |
distorted_video_paths.append(path)
|
| 1829 |
except Exception as e:
|
| 1830 |
logger.error(f"failed to download distorted video: {dist_url}, error: {e}")
|
|
@@ -1853,7 +1844,7 @@ async def score_organics_compression(request: OrganicsCompressionScoringRequest)
|
|
| 1853 |
final_scores.append(-1) # -1 means skip, don't penalize
|
| 1854 |
continue
|
| 1855 |
|
| 1856 |
-
ref_path, download_time = await download_video(ref_url, request.verbose
|
| 1857 |
|
| 1858 |
# Validate reference video using ffprobe (avoids AV1 warnings)
|
| 1859 |
if not is_valid_video(ref_path):
|
|
|
|
| 144 |
model.eval()
|
| 145 |
return model
|
| 146 |
|
| 147 |
+
async def download_video(video_url: str, verbose: bool) -> tuple[str, float]:
|
| 148 |
"""
|
| 149 |
Download a video from the given URL and save it to a temporary file.
|
| 150 |
|
| 151 |
Args:
|
| 152 |
video_url (str): The URL of the video to download.
|
| 153 |
verbose (bool): Whether to show download progress.
|
|
|
|
|
|
|
| 154 |
|
| 155 |
Returns:
|
| 156 |
tuple[str, float]: A tuple containing the path to the downloaded video file
|
|
|
|
| 160 |
Exception: If the download fails or takes longer than the timeout.
|
| 161 |
"""
|
| 162 |
try:
|
| 163 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as vid_temp:
|
| 164 |
+
file_path = vid_temp.name # Path to the temporary file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
if verbose:
|
| 166 |
logger.info(f"Downloading video from {video_url} to {file_path}")
|
| 167 |
|
|
|
|
| 178 |
async for chunk in response.content.iter_chunked(2 * 1024 * 1024):
|
| 179 |
f.write(chunk)
|
| 180 |
|
| 181 |
+
end_time = time.time() # Record end time
|
| 182 |
+
download_time = end_time - start_time # Calculate download duration
|
| 183 |
|
| 184 |
if not os.path.exists(file_path) or os.path.getsize(file_path) == 0:
|
| 185 |
raise Exception(f"Download failed or file is empty: {file_path}")
|
|
|
|
| 1475 |
length_scores = []
|
| 1476 |
final_scores = []
|
| 1477 |
reasons = []
|
| 1478 |
+
|
| 1479 |
distorted_video_paths = []
|
| 1480 |
for dist_url in request.distorted_urls:
|
| 1481 |
if len(dist_url) < 10:
|
| 1482 |
distorted_video_paths.append(None)
|
| 1483 |
continue
|
| 1484 |
try:
|
| 1485 |
+
path, download_time = await download_video(dist_url, request.verbose)
|
| 1486 |
distorted_video_paths.append(path)
|
| 1487 |
except Exception as e:
|
| 1488 |
logger.error(f"failed to download distorted video: {dist_url}, error: {e}")
|
|
|
|
| 1518 |
final_scores.append(-1) # -1 means skip, don't penalize
|
| 1519 |
continue
|
| 1520 |
|
| 1521 |
+
ref_path, download_time = await download_video(ref_url, request.verbose)
|
| 1522 |
|
| 1523 |
# Validate reference video using ffprobe (avoids AV1 warnings)
|
| 1524 |
if not is_valid_video(ref_path):
|
|
|
|
| 1808 |
compression_rates = []
|
| 1809 |
final_scores = []
|
| 1810 |
reasons = []
|
|
|
|
| 1811 |
|
| 1812 |
distorted_video_paths = []
|
| 1813 |
for dist_url in request.distorted_urls:
|
|
|
|
| 1815 |
distorted_video_paths.append(None)
|
| 1816 |
continue
|
| 1817 |
try:
|
| 1818 |
+
path, download_time = await download_video(dist_url, request.verbose)
|
| 1819 |
distorted_video_paths.append(path)
|
| 1820 |
except Exception as e:
|
| 1821 |
logger.error(f"failed to download distorted video: {dist_url}, error: {e}")
|
|
|
|
| 1844 |
final_scores.append(-1) # -1 means skip, don't penalize
|
| 1845 |
continue
|
| 1846 |
|
| 1847 |
+
ref_path, download_time = await download_video(ref_url, request.verbose)
|
| 1848 |
|
| 1849 |
# Validate reference video using ffprobe (avoids AV1 warnings)
|
| 1850 |
if not is_valid_video(ref_path):
|