File size: 55,169 Bytes
0cf03f6 | 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 | """
DarkMedia-X Studio — Backend API Server
Deployed as Hugging Face Space (Docker)
Serves REST API for the Vercel frontend dashboard.
"""
import os
import json
import time
import re
import hashlib
import psutil
import requests
import threading
import io
from pathlib import Path
from functools import wraps
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, JSONResponse
from dotenv import load_dotenv
from PIL import Image, ImageFilter, ImageOps, ImageEnhance
load_dotenv()
# Custom Swagger UI CSS for DarkMedia-X theme
DARKMEDIA_SWAGGER_CSS = """
<style>
/* DarkMedia-X Theme Overrides */
:root {
--dmx-bg: #0a0a0a;
--dmx-bg-secondary: #111111;
--dmx-border: #1a1a1a;
--dmx-text: #e0e0e0;
--dmx-text-muted: #888888;
--dmx-accent: #e94560;
--dmx-accent-hover: #ff6b81;
--dmx-success: #4caf50;
--dmx-warning: #ff9800;
--dmx-error: #f44336;
}
body {
background: var(--dmx-bg) !important;
color: var(--dmx-text) !important;
font-family: 'Courier New', monospace !important;
}
/* Header */
.swagger-ui .topbar {
background: linear-gradient(135deg, #0a0a0a 0%, #1a0a0a 100%) !important;
border-bottom: 2px solid var(--dmx-accent) !important;
}
.swagger-ui .topbar .download-url-wrapper input[type=text] {
background: var(--dmx-bg-secondary) !important;
color: var(--dmx-text) !important;
border: 1px solid var(--dmx-border) !important;
}
.swagger-ui .topbar .download-url-wrapper .download-url-button {
background: var(--dmx-accent) !important;
color: #fff !important;
}
/* Title */
.swagger-ui .info .title {
color: var(--dmx-accent) !important;
font-family: 'Courier New', monospace !important;
letter-spacing: 2px !important;
}
.swagger-ui .info .base-url {
color: var(--dmx-text-muted) !important;
}
.swagger-ui .info a {
color: var(--dmx-accent) !important;
}
/* Tags */
.swagger-ui .opblock-tag {
background: var(--dmx-bg-secondary) !important;
border-bottom: 1px solid var(--dmx-border) !important;
color: var(--dmx-accent) !important;
font-family: 'Courier New', monospace !important;
letter-spacing: 1px !important;
}
.swagger-ui .opblock-tag:hover {
background: #1a0a0a !important;
}
.swagger-ui .opblock-tag small {
color: var(--dmx-text-muted) !important;
}
/* Operations */
.swagger-ui .opblock {
background: var(--dmx-bg-secondary) !important;
border: 1px solid var(--dmx-border) !important;
border-radius: 4px !important;
margin-bottom: 10px !important;
}
.swagger-ui .opblock:hover {
border-color: var(--dmx-accent) !important;
box-shadow: 0 0 10px rgba(233, 69, 96, 0.2) !important;
}
.swagger-ui .opblock .opblock-summary {
border: none !important;
}
.swagger-ui .opblock .opblock-summary-method {
background: var(--dmx-accent) !important;
color: #fff !important;
font-weight: bold !important;
border-radius: 3px !important;
}
.swagger-ui .opblock .opblock-summary-path {
color: var(--dmx-text) !important;
font-family: 'Courier New', monospace !important;
}
.swagger-ui .opblock .opblock-summary-description {
color: var(--dmx-text-muted) !important;
}
/* Method colors */
.swagger-ui .opblock.opblock-get {
border-left: 4px solid #4caf50 !important;
}
.swagger-ui .opblock.opblock-get .opblock-summary-method {
background: #4caf50 !important;
}
.swagger-ui .opblock.opblock-post {
border-left: 4px solid #e94560 !important;
}
.swagger-ui .opblock.opblock-post .opblock-summary-method {
background: #e94560 !important;
}
.swagger-ui .opblock.opblock-delete {
border-left: 4px solid #f44336 !important;
}
.swagger-ui .opblock.opblock-delete .opblock-summary-method {
background: #f44336 !important;
}
.swagger-ui .opblock.opblock-put {
border-left: 4px solid #ff9800 !important;
}
.swagger-ui .opblock.opblock-put .opblock-summary-method {
background: #ff9800 !important;
}
/* Parameters */
.swagger-ui .parameters-col_description input[type=text],
.swagger-ui .parameters-col_description textarea {
background: var(--dmx-bg) !important;
color: var(--dmx-text) !important;
border: 1px solid var(--dmx-border) !important;
border-radius: 3px !important;
}
.swagger-ui .parameters-col_description input[type=text]:focus,
.swagger-ui .parameters-col_description textarea:focus {
border-color: var(--dmx-accent) !important;
box-shadow: 0 0 5px rgba(233, 69, 96, 0.3) !important;
}
.swagger-ui table thead tr td,
.swagger-ui table thead tr th {
color: var(--dmx-accent) !important;
border-bottom: 1px solid var(--dmx-border) !important;
}
.swagger-ui table tbody tr td {
border-bottom: 1px solid var(--dmx-border) !important;
}
/* Execute button */
.swagger-ui .btn.execute {
background: var(--dmx-accent) !important;
color: #fff !important;
border: none !important;
border-radius: 3px !important;
font-weight: bold !important;
letter-spacing: 1px !important;
}
.swagger-ui .btn.execute:hover {
background: var(--dmx-accent-hover) !important;
box-shadow: 0 0 15px rgba(233, 69, 96, 0.4) !important;
}
.swagger-ui .btn.cancel {
background: transparent !important;
color: var(--dmx-text-muted) !important;
border: 1px solid var(--dmx-border) !important;
}
/* Response */
.swagger-ui .responses-inner {
background: var(--dmx-bg) !important;
border: 1px solid var(--dmx-border) !important;
border-radius: 4px !important;
}
.swagger-ui .response-col_status {
color: var(--dmx-text) !important;
}
.swagger-ui .response-col_status .response-undocumented {
color: var(--dmx-text-muted) !important;
}
.swagger-ui .highlight-code {
background: var(--dmx-bg) !important;
}
.swagger-ui .highlight-code .microlight {
background: var(--dmx-bg) !important;
color: var(--dmx-text) !important;
}
/* Models */
.swagger-ui section.models {
background: var(--dmx-bg-secondary) !important;
border: 1px solid var(--dmx-border) !important;
}
.swagger-ui section.models h4 {
color: var(--dmx-text-muted) !important;
border-bottom: 1px solid var(--dmx-border) !important;
}
.swagger-ui .model-title {
color: var(--dmx-accent) !important;
}
.swagger-ui .model {
background: var(--dmx-bg) !important;
border: 1px solid var(--dmx-border) !important;
}
/* Scrollbar */
::-webkit-scrollbar {
width: 8px !important;
height: 8px !important;
}
::-webkit-scrollbar-track {
background: var(--dmx-bg) !important;
}
::-webkit-scrollbar-thumb {
background: var(--dmx-border) !important;
border-radius: 4px !important;
}
::-webkit-scrollbar-thumb:hover {
background: var(--dmx-accent) !important;
}
/* Links */
.swagger-ui a {
color: var(--dmx-accent) !important;
}
.swagger-ui a:hover {
color: var(--dmx-accent-hover) !important;
}
/* Try it out */
.swagger-ui .try-out__btn {
background: transparent !important;
color: var(--dmx-accent) !important;
border: 1px solid var(--dmx-accent) !important;
}
.swagger-ui .try-out__btn:hover {
background: var(--dmx-accent) !important;
color: #fff !important;
}
/* Auth */
.swagger-ui .auth-wrapper .authorize {
background: transparent !important;
color: var(--dmx-accent) !important;
border: 1px solid var(--dmx-accent) !important;
}
/* Footer */
.swagger-ui .wrapper {
max-width: 1400px !important;
}
</style>
"""
app = FastAPI(
title="DarkMedia-X Studio API",
description="### 🎬 Automated Horror/Anime Video Production System\n\n"
"**Endpoints for the Vercel frontend dashboard.**\n\n"
"All media is stored on Cloudflare R2. Images are proxied through this API to avoid CORS issues.\n\n"
"---\n"
"### Quick Links\n"
"- **Frontend**: https://darkmedia-xstudio.vercel.app\n"
"- **HF Space**: https://huggingface.co/spaces/cybermedia/darkmedia-x-api\n"
"- **R2 Bucket**: darkmedia-x-studio",
version="1.0.0",
docs_url=None, # Disable default docs
redoc_url="/redoc",
)
# Custom Swagger UI with DarkMedia-X theme
@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.openapi.utils import get_openapi
openapi_schema = get_openapi(
title=app.title,
version=app.version,
description=app.description,
routes=app.routes,
)
# Serve OpenAPI JSON
@app.get("/openapi.json", include_in_schema=False)
async def openapi_json():
return openapi_schema
html_content = f"""<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{app.title} - Swagger UI</title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.css">
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Ctext y='.9em' font-size='90'%3E🎬%3C/text%3E%3C/svg%3E">
{DARKMEDIA_SWAGGER_CSS}
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js" crossorigin></script>
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-standalone-preset.js" crossorigin></script>
<script>
window.onload = function() {{
const ui = SwaggerUIBundle({{
url: "/openapi.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
persistAuthorization: true,
displayRequestDuration: true,
filter: true,
tryItOutEnabled: true,
}});
window.ui = ui;
}};
</script>
</body>
</html>"""
return HTMLResponse(content=html_content)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# --- Server-Side Cache ---
class Cache:
"""Simple TTL cache with ETag support."""
def __init__(self):
self._store = {}
def get(self, key):
if key in self._store:
entry = self._store[key]
if time.time() - entry["ts"] < entry["ttl"]:
return entry["data"], entry["etag"]
del self._store[key]
return None, None
def set(self, key, data, ttl=60):
etag = hashlib.md5(json.dumps(data, sort_keys=True).encode()).hexdigest()[:12]
self._store[key] = {"data": data, "etag": etag, "ts": time.time(), "ttl": ttl}
return etag
def invalidate(self, key):
self._store.pop(key, None)
cache = Cache()
def cached_endpoint(key, ttl=60):
"""Decorator that adds server-side caching + ETag + Cache-Control headers."""
def decorator(func):
@wraps(func)
async def wrapper(request: Request, *args, **kwargs):
# Check If-None-Match for ETag
if_none_match = request.headers.get("if-none-match")
cached_data, cached_etag = cache.get(key)
if cached_data and if_none_match and if_none_match.strip('"') == cached_etag:
return JSONResponse(status_code=304, content={}, headers={
"ETag": f'"{cached_etag}"',
"Cache-Control": f"public, max-age={ttl}, stale-while-revalidate={ttl*2}",
})
if cached_data:
return JSONResponse(content=cached_data, headers={
"ETag": f'"{cached_etag}"',
"Cache-Control": f"public, max-age={ttl}, stale-while-revalidate={ttl*2}",
})
result = await func(request, *args, **kwargs)
if isinstance(result, dict):
etag = cache.set(key, result, ttl)
return JSONResponse(content=result, headers={
"ETag": f'"{etag}"',
"Cache-Control": f"public, max-age={ttl}, stale-while-revalidate={ttl*2}",
})
return result
return wrapper
return decorator
def no_cache(func):
"""Decorator for endpoints that must always return fresh data."""
@wraps(func)
async def wrapper(*args, **kwargs):
result = await func(*args, **kwargs)
if isinstance(result, dict):
return JSONResponse(content=result, headers={
"Cache-Control": "no-cache, no-store, must-revalidate",
"Pragma": "no-cache",
"Expires": "0",
})
return result
return wrapper
# Paths
SPACE_DIR = Path(__file__).parent.resolve()
DATA_DIR = SPACE_DIR / "data"
STORIES_DIR = DATA_DIR / "stories"
ASSETS_DIR = DATA_DIR / "assets"
VIDEOS_DIR = DATA_DIR / "videos"
STATE_DIR = DATA_DIR / "state"
for d in [DATA_DIR, STORIES_DIR, ASSETS_DIR, VIDEOS_DIR, STATE_DIR]:
d.mkdir(parents=True, exist_ok=True)
# --- R2 Client ---
def get_r2_client():
endpoint = os.getenv("R2_ENDPOINT", "")
access_key = os.getenv("R2_ACCESS_KEY_ID", "")
secret_key = os.getenv("R2_SECRET_ACCESS_KEY", "")
if not all([endpoint, access_key, secret_key]):
return None
try:
import boto3
from botocore.config import Config
return boto3.client(
"s3",
endpoint_url=endpoint,
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
config=Config(signature_version="s3v4"),
)
except Exception:
return None
def r2_list(prefix=""):
client = get_r2_client()
if client is None:
return []
bucket = os.getenv("R2_BUCKET", "darkmedia-x-studio")
try:
paginator = client.get_paginator("list_objects_v2")
keys = []
for page in paginator.paginate(Bucket=bucket, Prefix=prefix):
for obj in page.get("Contents", []):
keys.append(obj["Key"])
return keys
except Exception:
return []
def r2_read_text(key):
client = get_r2_client()
if client is None:
return None
bucket = os.getenv("R2_BUCKET", "darkmedia-x-studio")
try:
resp = client.get_object(Bucket=bucket, Key=key)
return resp["Body"].read().decode("utf-8")
except Exception:
return None
def r2_presigned_url(key, expires_in=3600):
client = get_r2_client()
if client is None:
return None
bucket = os.getenv("R2_BUCKET", "darkmedia-x-studio")
try:
return client.generate_presigned_url(
"get_object",
Params={"Bucket": bucket, "Key": key},
ExpiresIn=expires_in,
)
except Exception:
return None
@app.get("/api/image/{image_path:path}", tags=["Images"])
async def proxy_image(image_path: str):
"""Proxy image from R2 to avoid CORS issues with direct R2 URLs."""
import urllib.parse
from fastapi.responses import Response
key = urllib.parse.unquote(image_path)
# Remove leading slash if present
if key.startswith("/"):
key = key[1:]
client = get_r2_client()
if client is None:
raise HTTPException(status_code=500, detail="R2 not configured")
bucket = os.getenv("R2_BUCKET", "darkmedia-x-studio")
try:
resp = client.get_object(Bucket=bucket, Key=key)
content = resp["Body"].read()
content_type = resp.get("ContentType", "image/png")
# Determine content type from extension if not set
if not content_type or content_type == "binary/octet-stream":
if key.lower().endswith(".png"):
content_type = "image/png"
elif key.lower().endswith((".jpg", ".jpeg")):
content_type = "image/jpeg"
elif key.lower().endswith(".gif"):
content_type = "image/gif"
elif key.lower().endswith(".webp"):
content_type = "image/webp"
return Response(content=content, media_type=content_type, headers={
"Cache-Control": "public, max-age=3600",
"Access-Control-Allow-Origin": "*",
})
except Exception as e:
raise HTTPException(status_code=404, detail=f"Image not found: {str(e)}")
# --- Stories ---
@app.get("/api/stories", tags=["Stories"])
@cached_endpoint(key="stories", ttl=30)
async def get_stories(request: Request):
stories = []
seen = set()
# Local scan
if STORIES_DIR.exists():
for dirpath, _, filenames in os.walk(str(STORIES_DIR)):
for filename in filenames:
if not filename.endswith(".md") or filename.startswith("README") or filename == "music_prompt.md":
continue
file_path = Path(dirpath) / filename
rel_path = file_path.relative_to(STORIES_DIR)
story_id = str(rel_path.parent).replace("\\", "/")
if story_id in seen:
continue
seen.add(story_id)
title = filename.replace(".md", "").replace("_", " ").strip()
if title.lower() in {"story", "index", "readme"}:
title = story_id.split("/")[-1].replace("_", " ").strip()
img_dir = file_path.parent / "assets" / "images"
if not img_dir.exists():
img_dir = file_path.parent / "images"
image_count = 0
if img_dir.exists():
image_count = len([f for f in os.listdir(str(img_dir)) if f.lower().endswith((".png", ".jpg", ".jpeg"))])
scenes_dir = file_path.parent / "assets" / "scenes"
total_scenes = 0
if scenes_dir.exists():
total_scenes = len([f for f in os.listdir(str(scenes_dir)) if f.startswith("scene_") and f.endswith(".txt")])
content = file_path.read_text(encoding="utf-8")
word_count = len(content.split())
parent = file_path.parent
category = parent.parent.name if parent.parent != STORIES_DIR else "General"
stories.append({
"id": story_id,
"title": title,
"path": str(rel_path).replace("\\", "/"),
"category": category,
"processed": False,
"image_count": image_count,
"total_scenes": total_scenes if total_scenes > 0 else 10,
"ready": image_count >= (total_scenes if total_scenes > 0 else 10),
"word_count": word_count,
"is_empty": word_count < 20,
})
# R2 scan
r2_keys = r2_list(prefix="stories/")
for key in r2_keys:
if not key.endswith(".md") or "README" in key or "music_prompt" in key:
continue
parts = key.split("/")
if len(parts) < 3:
continue
story_id = "/".join(parts[1:-1])
if story_id in seen:
continue
seen.add(story_id)
filename = parts[-1]
title = filename.replace(".md", "").replace("_", " ").strip()
if title.lower() in {"story", "index", "readme"}:
title = story_id.split("/")[-1].replace("_", " ").strip()
category = parts[1]
# Count images on R2
image_prefix = "/".join(parts[:3]) + "/assets/images/"
image_keys = [k for k in r2_keys if k.startswith(image_prefix) and k.endswith((".png", ".jpg", ".jpeg"))]
image_count = len(image_keys)
content = r2_read_text(key) or ""
word_count = len(content.split())
stories.append({
"id": story_id,
"title": title,
"path": key,
"category": category,
"processed": False,
"image_count": image_count,
"total_scenes": 10,
"ready": image_count >= 10,
"word_count": word_count,
"is_empty": word_count < 20,
})
return {"stories": sorted(stories, key=lambda s: s["title"])}
@app.get("/api/stories/content/{story_path:path}", tags=["Stories"])
@cached_endpoint(key="story_content", ttl=300)
async def get_story_content(request: Request, story_path: str):
"""Get story content by path (frontend calls /api/stories/content/{id})."""
# Try local first
base = STORIES_DIR / story_path
for f in ["story.md", "story.txt"]:
if (base / f).exists():
return {"content": (base / f).read_text(encoding="utf-8"), "status": "success", "path": story_path, "filename": f}
# Try R2
r2_key = f"stories/{story_path}/story.md"
content = r2_read_text(r2_key)
if content:
return {"content": content, "status": "success", "path": story_path, "filename": "story.md"}
raise HTTPException(status_code=404, detail="Story not found")
@app.get("/api/stories/{story_path:path}", tags=["Stories"])
@cached_endpoint(key="story_content", ttl=300)
async def get_story(request: Request, story_path: str):
base = STORIES_DIR / story_path
for f in ["story.md", "story.txt"]:
if (base / f).exists():
return {"content": (base / f).read_text(encoding="utf-8")}
# Try R2
r2_key = f"stories/{story_path}/story.md"
content = r2_read_text(r2_key)
if content:
return {"content": content}
raise HTTPException(status_code=404, detail="Story not found")
# --- Assets ---
@app.get("/api/assets/music", tags=["Assets"])
@cached_endpoint(key="music", ttl=60)
async def get_music(request: Request):
music_dir = ASSETS_DIR / "background_music"
files = []
if music_dir.exists():
files = [f.name for f in music_dir.glob("*.mp3") | music_dir.glob("*.wav")]
return {"music": sorted(files)}
@app.get("/api/assets/voice_samples", tags=["Assets"])
@cached_endpoint(key="voice_samples", ttl=60)
async def get_voice_samples(request: Request):
voice_dir = ASSETS_DIR / "voice_samples"
files = []
if voice_dir.exists():
files = [f.name for f in voice_dir.glob("*.wav") | voice_dir.glob("*.mp3")]
return {"samples": sorted(files)}
# --- Videos ---
@app.get("/api/videos", tags=["Videos"])
@cached_endpoint(key="videos", ttl=30)
async def get_videos(request: Request):
"""List videos from R2 storage."""
videos = []
# Scan R2 for videos
r2_keys = r2_list(prefix="videos/")
for key in r2_keys:
if key.lower().endswith(".mp4"):
filename = key.split("/")[-1]
# Extract story info from path if available
parts = key.split("/")
story = parts[1] if len(parts) > 1 else "Unknown"
# Use proxy URL for video playback
proxy_url = f"/api/video/play/{key}"
videos.append({
"filename": filename,
"path": key,
"url": proxy_url,
"story": story,
})
# Also scan local directory (for local development)
if VIDEOS_DIR.exists():
for f in VIDEOS_DIR.glob("*.mp4"):
if not any(v["filename"] == f.name for v in videos):
videos.append({
"filename": f.name,
"path": f"videos/{f.name}",
"url": f"/api/video/play/videos/{f.name}",
"story": "local",
})
return {"videos": sorted(videos, key=lambda x: x["filename"])}
@app.get("/api/video/play/{video_path:path}", tags=["Videos"])
async def proxy_video(video_path: str):
"""Proxy video from R2 for playback."""
import urllib.parse
from fastapi.responses import StreamingResponse
key = urllib.parse.unquote(video_path)
if key.startswith("/"):
key = key[1:]
client = get_r2_client()
if client is None:
raise HTTPException(status_code=500, detail="R2 not configured")
bucket = os.getenv("R2_BUCKET", "darkmedia-x-studio")
try:
resp = client.get_object(Bucket=bucket, Key=key)
content = resp["Body"].read()
return StreamingResponse(
iter([content]),
media_type="video/mp4",
headers={
"Cache-Control": "public, max-age=3600",
"Access-Control-Allow-Origin": "*",
"Accept-Ranges": "bytes",
}
)
except Exception as e:
raise HTTPException(status_code=404, detail=f"Video not found: {str(e)}")
# --- Generated Images (R2) ---
@app.get("/api/generated_images", tags=["Images"])
@cached_endpoint(key="generated_images", ttl=60)
async def get_generated_images(request: Request):
r2_keys = r2_list(prefix="stories/")
images = []
for key in r2_keys:
if not key.endswith((".png", ".jpg", ".jpeg")) or "/images/" not in key:
continue
parts = key.split("/")
category = parts[1] if len(parts) > 1 else "General"
story_id = "/".join(parts[1:3])
scene = parts[-1].rsplit(".", 1)[0]
# Use proxy URL to avoid CORS issues
proxy_url = f"/api/image/{key}"
images.append({
"url": proxy_url,
"path": key,
"filename": parts[-1],
"story_id": story_id,
"category": category,
"scene": scene,
})
return {"images": sorted(images, key=lambda x: x.get("path", ""))}
# --- Library (Gallery view) ---
@app.get("/api/library", tags=["Images"])
@cached_endpoint(key="library", ttl=30)
async def get_library(request: Request):
"""Scan R2 for generated scene images, return gallery-ready list."""
r2_keys = r2_list(prefix="stories/")
library = []
for key in r2_keys:
if not key.endswith(".png"):
continue
# Skip non-scene images (e.g. UI assets)
if "/images/" not in key and "/depths/" not in key and "/blender_test/" not in key:
continue
parts = key.split("/")
if len(parts) < 4:
continue
# story_name is usually at index 2 (stories/{category}/{story}/...)
story_name = parts[2] if len(parts) > 2 else "Unknown"
# Use proxy URL to avoid CORS issues
proxy_url = f"/api/image/{key}"
library.append({
"story": story_name,
"filename": parts[-1],
"url": proxy_url,
"path": key,
})
library.sort(key=lambda x: x["filename"], reverse=True)
return {"library": library[:200]}
@app.delete("/api/library/{image_path:path}", tags=["Images"])
async def delete_library_image(image_path: str):
"""Delete an image from R2 storage."""
client = get_r2_client()
if client is None:
return {"status": "error", "message": "R2 client not configured"}
bucket = os.getenv("R2_BUCKET", "darkmedia-x-studio")
# URL-decode the path
import urllib.parse
key = urllib.parse.unquote(image_path)
try:
client.delete_object(Bucket=bucket, Key=key)
# Invalidate caches
cache.invalidate("library")
cache.invalidate("generated_images")
cache.invalidate("stories")
return {"status": "success", "message": f"Deleted {key}"}
except Exception as e:
return {"status": "error", "message": str(e)}
# --- Current Frame / Preview ---
@app.get("/api/current_frame", tags=["Preview"])
@no_cache
async def get_current_frame():
return {"frame": None, "narration": ""}
# --- Status ---
@app.get("/api/status", tags=["System"])
@no_cache
async def get_status():
try:
task_file = STATE_DIR / "current_task.json"
if task_file.exists():
with open(task_file) as f:
task = json.load(f)
else:
task = {"story": None, "step": "IDLE", "progress": 0}
return {
"status": "ready",
"step": task.get("step", "IDLE"),
"progress": task.get("progress", 0),
"story": task.get("story"),
"error": task.get("error"),
"current_scene": task.get("current_scene", 0),
"total_scenes": task.get("total_scenes", 0),
"timestamp": time.time(),
}
except Exception as e:
return {"status": "error", "message": str(e)}
@app.get("/api/logs", tags=["System"])
async def get_logs():
return {"logs": []}
# --- System Info ---
@app.get("/api/system", tags=["System"])
@no_cache
async def get_system():
try:
ram = psutil.virtual_memory()
return {
"ram_used": ram.used / (1024**3),
"ram_total": ram.total / (1024**3),
"ram_percent": ram.percent,
"cpu_percent": psutil.cpu_percent(interval=0.1),
"python_version": os.sys.version.split()[0],
}
except Exception as e:
return {"error": str(e)}
# --- Engine Health ---
@app.get("/api/engine/health", tags=["System"])
@no_cache
async def get_engine_health():
try:
task_file = STATE_DIR / "current_task.json"
if task_file.exists():
with open(task_file) as f:
task = json.load(f)
story = task.get("story", "UNKNOWN")
step = task.get("step", "IDLE")
progress = task.get("progress", 0)
else:
story = "AUCUN"
step = "IDLE"
progress = 0
scenes_count = 0
total_scenes = 10
if story and story != "AUCUN":
story_dir = STORIES_DIR / story
img_dir = story_dir / "assets" / "images"
if img_dir.exists():
scenes_count = len(list(img_dir.glob("scene_*.png")))
verdict = "HEALTHY" if progress == 0 else "RENDERING"
verdict_color = "green" if progress == 0 else "yellow"
return {
"verdict": verdict,
"verdict_color": verdict_color,
"story": story,
"step": step,
"progress": progress,
"scenes_generated": scenes_count,
"total_scenes": total_scenes,
"logs": [],
}
except Exception as e:
return {"verdict": "ERROR", "verdict_color": "red", "message": str(e)}
# --- UI Settings ---
@app.get("/api/ui/settings", tags=["Settings"])
@cached_endpoint(key="ui_settings", ttl=10)
async def get_ui_settings(request: Request):
settings_file = STATE_DIR / "ui_settings.json"
if settings_file.exists():
with open(settings_file) as f:
return json.load(f)
return {}
@app.post("/api/ui/settings", tags=["Settings"])
async def save_ui_settings(settings: dict):
settings_file = STATE_DIR / "ui_settings.json"
with open(settings_file, "w") as f:
json.dump(settings, f)
cache.invalidate("ui_settings")
return {"status": "ok"}
# --- TTS Preview ---
@app.get("/api/tts/voices", tags=["Audio"])
async def get_tts_voices():
"""List available TTS voices."""
return {
"voices": [
{"id": "fr-FR-DeniseNeural", "name": "Denise (FR)", "gender": "female", "language": "fr-FR"},
{"id": "fr-FR-HenriNeural", "name": "Henri (FR)", "gender": "male", "language": "fr-FR"},
{"id": "fr-CA-SylvieNeural", "name": "Sylvie (CA)", "gender": "female", "language": "fr-CA"},
{"id": "fr-CA-JeanNeural", "name": "Jean (CA)", "gender": "male", "language": "fr-CA"},
{"id": "fr-FR-AlainNeural", "name": "Alain (FR)", "gender": "male", "language": "fr-FR"},
{"id": "fr-FR-BrigitteNeural", "name": "Brigitte (FR)", "gender": "female", "language": "fr-FR"},
{"id": "fr-FR-CelesteNeural", "name": "Celeste (FR)", "gender": "female", "language": "fr-FR"},
{"id": "fr-FR-ClaudeNeural", "name": "Claude (FR)", "gender": "male", "language": "fr-FR"},
{"id": "fr-FR-CoralieNeural", "name": "Coralie (FR)", "gender": "female", "language": "fr-FR"},
{"id": "fr-FR-EloiseNeural", "name": "Eloise (FR)", "gender": "female", "language": "fr-FR"},
{"id": "fr-FR-JacquelineNeural", "name": "Jacqueline (FR)", "gender": "female", "language": "fr-FR"},
{"id": "fr-FR-JeromeNeural", "name": "Jerome (FR)", "gender": "male", "language": "fr-FR"},
{"id": "fr-FR-JosephineNeural", "name": "Josephine (FR)", "gender": "female", "language": "fr-FR"},
{"id": "fr-FR-MauriceNeural", "name": "Maurice (FR)", "gender": "male", "language": "fr-FR"},
{"id": "fr-FR-YvesNeural", "name": "Yves (FR)", "gender": "male", "language": "fr-FR"},
{"id": "fr-FR-YvetteNeural", "name": "Yvette (FR)", "gender": "female", "language": "fr-FR"},
]
}
@app.post("/api/tts/preview", tags=["Audio"])
async def tts_preview(payload: dict):
import tempfile
import edge_tts
text = payload.get("text", "Test audio DarkMedia-X.")
voice = payload.get("voice", "fr-FR-DeniseNeural")
rate = payload.get("rate", "+0%")
pitch = payload.get("pitch", "+0Hz")
# Map edge-tts voice names
voice_map = {
"fr-FR-DeniseNeural": "fr-FR-DeniseNeural",
"fr-FR-HenriNeural": "fr-FR-HenriNeural",
"fr-CA-SylvieNeural": "fr-CA-SylvieNeural",
"fr-CA-JeanNeural": "fr-CA-JeanNeural",
}
tts_voice = voice_map.get(voice, "fr-FR-DeniseNeural")
try:
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp:
comm = edge_tts.Communicate(text, tts_voice, rate=rate, pitch=pitch)
await comm.save(tmp.name)
tmp.seek(0)
audio_data = tmp.read()
import base64
b64 = base64.b64encode(audio_data).decode()
return {"status": "success", "url": f"data:audio/mp3;base64,{b64}"}
except Exception as e:
return {"status": "error", "message": str(e)}
# --- Story Images (R2) ---
@app.get("/api/images", tags=["Images"])
async def get_story_images(story_id: str):
"""List images for a story from R2."""
import urllib.parse
story_id = urllib.parse.unquote(story_id).strip("/")
images = []
r2_keys = r2_list(prefix=f"stories/{story_id}/assets/images/")
for key in r2_keys:
if key.lower().endswith((".png", ".jpg", ".jpeg")):
filename = key.split("/")[-1]
# Use proxy URL to avoid CORS issues
proxy_url = f"/api/image/{key}"
images.append({
"path": key,
"filename": filename,
"url": proxy_url,
"timestamp": 0,
})
images.sort(key=lambda x: x["filename"])
return {"images": images}
@app.delete("/api/images", tags=["Images"])
async def delete_story_image(story_id: str, filename: str):
"""Delete an image from R2."""
import urllib.parse
story_id = urllib.parse.unquote(story_id).strip("/")
filename = urllib.parse.unquote(filename).strip("/")
key = f"stories/{story_id}/assets/images/{filename}"
client = get_r2_client()
if client is None:
return {"status": "error", "message": "R2 not configured"}
bucket = os.getenv("R2_BUCKET", "darkmedia-x-studio")
try:
client.delete_object(Bucket=bucket, Key=key)
cache.invalidate("generated_images")
cache.invalidate("library")
cache.invalidate("stories")
return {"status": "deleted", "filename": filename}
except Exception as e:
return {"status": "error", "message": str(e)}
# --- Story Mutations (R2) ---
@app.delete("/api/stories/{story_path:path}", tags=["Stories"])
async def delete_story(story_path: str):
"""Delete a story and all its assets from R2."""
import urllib.parse
story_path = urllib.parse.unquote(story_path).strip("/")
prefix = f"stories/{story_path}/"
client = get_r2_client()
if client is None:
return {"status": "error", "message": "R2 not configured"}
bucket = os.getenv("R2_BUCKET", "darkmedia-x-studio")
try:
# List and delete all objects under the story prefix
paginator = client.get_paginator("list_objects_v2")
for page in paginator.paginate(Bucket=bucket, Prefix=prefix):
for obj in page.get("Contents", []):
client.delete_object(Bucket=bucket, Key=obj["Key"])
cache.invalidate("stories")
cache.invalidate("generated_images")
cache.invalidate("library")
return {"status": "success", "message": f"Story '{story_path}' deleted"}
except Exception as e:
return {"status": "error", "message": str(e)}
@app.post("/api/stories/save", tags=["Stories"])
async def save_story(payload: dict):
"""Save story content to R2."""
story_id = payload.get("story_id", "")
content = payload.get("content", "")
if not story_id or not content:
return {"status": "error", "message": "story_id and content required"}
client = get_r2_client()
if client is None:
return {"status": "error", "message": "R2 not configured"}
bucket = os.getenv("R2_BUCKET", "darkmedia-x-studio")
key = f"stories/{story_id}/story.md"
try:
client.put_object(Bucket=bucket, Key=key, Body=content.encode("utf-8"), ContentType="text/markdown")
cache.invalidate("stories")
return {"status": "success", "message": "Story saved"}
except Exception as e:
return {"status": "error", "message": str(e)}
@app.post("/api/stories/normalize_ai", tags=["Stories"])
async def normalize_ai(payload: dict = None):
"""Normalize story prompts (stub - returns success)."""
return {"status": "success", "message": "Normalization complete", "fixed_files": 0}
@app.post("/api/gemini/ask", tags=["AI"])
async def gemini_ask(payload: dict):
"""Call Gemini API for AI assistance."""
import requests
api_key = os.getenv("GEMINI_API_KEY", "")
if not api_key:
return {"status": "error", "message": "GEMINI_API_KEY not configured"}
prompt = payload.get("prompt", "")
model = payload.get("model", "gemini-2.0-flash")
try:
url = f"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?key={api_key}"
resp = requests.post(url, json={
"contents": [{"parts": [{"text": prompt}]}]
}, timeout=60)
data = resp.json()
text = data.get("candidates", [{}])[0].get("content", {}).get("parts", [{}])[0].get("text", "")
return {"status": "success", "response": text}
except Exception as e:
return {"status": "error", "message": str(e)}
@app.post("/api/stories/launch", tags=["Pipeline"])
async def launch_pipeline(payload: dict):
"""Trigger image generation pipeline using HF Inference API (FLUX)."""
import json
import threading
import io
import re
story_id = payload.get("story_id", "")
story_path = payload.get("story_path", "")
config = payload.get("config", {})
regenerate = payload.get("regenerate_all", False)
if not story_id:
return {"status": "error", "message": "story_id required"}
# Write initial task status
task_file = STATE_DIR / "current_task.json"
task_data = {
"story": story_id,
"story_path": story_path,
"step": "READING_STORY",
"progress": 0,
"config": config,
"timestamp": time.time()
}
try:
STATE_DIR.mkdir(parents=True, exist_ok=True)
with open(task_file, "w") as f:
json.dump(task_data, f)
except Exception:
pass
cache.invalidate("status")
def run_voice_generation(client, bucket, task_file, task_data, config):
"""Generate TTS audio for all scenes."""
import edge_tts
import tempfile
story_key = f"stories/{story_id}/story.md"
try:
story_content = r2_read_text(story_key)
if not story_content:
with open(task_file, "w") as f:
json.dump({**task_data, "step": "ERROR", "error": "Story not found"}, f)
return
except Exception as e:
with open(task_file, "w") as f:
json.dump({**task_data, "step": "ERROR", "error": str(e)}, f)
return
scenes = []
scene_pattern = re.compile(r'#+\s*[Ss]c[eè]ne\s*(\d+)[\s:]*\n*(.*?)(?=#+\s*[Ss]c[eè]ne|\Z)', re.DOTALL)
matches = scene_pattern.findall(story_content)
if not matches:
blocks = [b.strip() for b in story_content.split('\n\n') if b.strip()]
for i, block in enumerate(blocks[:10], 1):
scenes.append({"id": i, "text": block[:500]})
else:
for num, content in matches:
scenes.append({"id": int(num), "text": content.strip()[:500]})
if not scenes:
with open(task_file, "w") as f:
json.dump({**task_data, "step": "ERROR", "error": "No scenes found"}, f)
return
voice = config.get("voice", "fr-FR-DeniseNeural")
rate = config.get("rate", "+0%")
total_scenes = len(scenes)
for idx, scene in enumerate(scenes):
scene_num = scene["id"]
scene_text = scene["text"]
progress = int((idx / total_scenes) * 100)
with open(task_file, "w") as f:
json.dump({
**task_data,
"step": f"GENERATING_VOICE_{scene_num}",
"progress": progress,
"current_scene": scene_num,
"total_scenes": total_scenes
}, f)
cache.invalidate("status")
audio_key = f"stories/{story_id}/assets/audio/scene_{scene_num}.mp3"
try:
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp:
comm = edge_tts.Communicate(scene_text, voice, rate=rate)
await comm.save(tmp.name)
tmp.seek(0)
with open(tmp.name, "rb") as f:
client.put_object(
Bucket=bucket,
Key=audio_key,
Body=f.read(),
ContentType="audio/mp3"
)
except Exception as e:
import traceback
error_msg = f"TTS error scene {scene_num}: {str(e)}\n{traceback.format_exc()}"
with open(task_file, "w") as f:
json.dump({**task_data, "step": "ERROR", "error": error_msg, "progress": progress}, f)
return
with open(task_file, "w") as f:
json.dump({
**task_data,
"step": "DONE",
"progress": 100,
"voices_generated": total_scenes
}, f)
cache.invalidate("status")
cache.invalidate("stories")
def run_music_generation(client, bucket, task_file, task_data, config):
"""Generate background music for the story."""
music_style = config.get("music_style", "dark_ambient")
with open(task_file, "w") as f:
json.dump({
**task_data,
"step": "GENERATING_MUSIC",
"progress": 50,
"music_style": music_style
}, f)
cache.invalidate("status")
# Placeholder: music generation not yet implemented
# TODO: Integrate with MusicGen or similar
with open(task_file, "w") as f:
json.dump({
**task_data,
"step": "DONE",
"progress": 100,
"music_generated": True
}, f)
cache.invalidate("status")
def run_generation():
"""Background thread for image/voice/music generation."""
hf_token = os.getenv("HF_TOKEN", "")
if not hf_token:
with open(task_file, "w") as f:
json.dump({**task_data, "step": "ERROR", "error": "HF_TOKEN not configured"}, f)
return
client = get_r2_client()
bucket = os.getenv("R2_BUCKET", "darkmedia-x-studio")
# Check if this is a voice-only generation
if config.get("voice_only", False):
run_voice_generation(client, bucket, task_file, task_data, config)
return
# Check if this is a music-only generation
if config.get("music_only", False):
run_music_generation(client, bucket, task_file, task_data, config)
return
# 1. Read story content
story_key = f"stories/{story_id}/story.md"
try:
story_content = r2_read_text(story_key)
if not story_content:
with open(task_file, "w") as f:
json.dump({**task_data, "step": "ERROR", "error": "Story not found in R2"}, f)
return
except Exception as e:
with open(task_file, "w") as f:
json.dump({**task_data, "step": "ERROR", "error": str(e)}, f)
return
# 2. Parse scenes from story markdown
scenes = []
scene_pattern = re.compile(r'#+\s*[Ss]c[eè]ne\s*(\d+)[\s:]*\n*(.*?)(?=#+\s*[Ss]c[eè]ne|\Z)', re.DOTALL)
matches = scene_pattern.findall(story_content)
if not matches:
blocks = [b.strip() for b in story_content.split('\n\n') if b.strip()]
for i, block in enumerate(blocks[:10], 1):
scenes.append({"id": i, "prompt": block[:500]})
else:
for num, content in matches:
scenes.append({"id": int(num), "prompt": content.strip()[:500]})
if not scenes:
with open(task_file, "w") as f:
json.dump({**task_data, "step": "ERROR", "error": "No scenes found in story"}, f)
return
total_scenes = len(scenes)
# 3. Generate images for each scene via Gradio Client (ZeroGPU)
image_model = config.get("image_model", "flux")
model_spaces = {
"flux": os.getenv("HF_IMAGE_SPACE_FLUX", "cybermedia/flux-zerogpu"),
"ssd": os.getenv("HF_IMAGE_SPACE_SSD", "cybermedia/ssd-zerogpu"),
"sdxl": os.getenv("HF_IMAGE_SPACE_SDXL", "cybermedia/sdxl-zerogpu"),
"playground": os.getenv("HF_IMAGE_SPACE_PLAYGROUND", "cybermedia/playground-zerogpu"),
}
gradio_space = model_spaces.get(image_model, model_spaces["flux"])
for idx, scene in enumerate(scenes):
scene_num = scene["id"]
scene_prompt = scene["prompt"]
progress = int((idx / total_scenes) * 100)
with open(task_file, "w") as f:
json.dump({
**task_data,
"step": f"GENERATING_SCENE_{scene_num}",
"progress": progress,
"current_scene": scene_num,
"total_scenes": total_scenes
}, f)
cache.invalidate("status")
img_key = f"stories/{story_id}/assets/images/scene_{scene_num}.png"
if not regenerate:
try:
client.head_object(Bucket=bucket, Key=img_key)
continue
except Exception:
pass
enhanced_prompt = f"horror anime style, dark atmosphere, cinematic, {scene_prompt}"
try:
from gradio_client import Client
gradio_client = Client(gradio_space)
result = gradio_client.predict(
prompt=enhanced_prompt,
steps=20,
seed=42,
api_name="/generate"
)
if isinstance(result, str):
with open(result, "rb") as f:
client.put_object(
Bucket=bucket,
Key=img_key,
Body=f.read(),
ContentType="image/png"
)
else:
error_msg = f"{image_model.upper()} Gradio returned unexpected result: {result}"
with open(task_file, "w") as f:
json.dump({**task_data, "step": "ERROR", "error": error_msg, "progress": progress}, f)
return
except Exception as e:
import traceback
error_msg = f"{image_model.upper()} Gradio error: {str(e)}\n{traceback.format_exc()}"
with open(task_file, "w") as f:
json.dump({**task_data, "step": "ERROR", "error": error_msg, "progress": progress}, f)
return
with open(task_file, "w") as f:
json.dump({
**task_data,
"step": "DONE",
"progress": 100,
"images_generated": total_scenes
}, f)
cache.invalidate("status")
cache.invalidate("stories")
cache.invalidate("generated_images")
# Start generation in background thread
thread = threading.Thread(target=run_generation, daemon=True)
thread.start()
return {"status": "success", "message": f"Generation started for '{story_id}'", "job_id": story_id}
@app.post("/api/stories/remix", tags=["Pipeline"])
async def remix_story(payload: dict):
"""Remix a story with new settings."""
story_id = payload.get("story_id", "")
if not story_id:
return {"status": "error", "message": "story_id required"}
return {"status": "success", "message": f"Remix queued for '{story_id}'"}
@app.post("/api/stories/apply_art_style", tags=["Images"])
async def apply_art_style(payload: dict):
"""Apply an art style filter to all images of a story."""
story_id = payload.get("story_id", "")
style = payload.get("style", "none")
if not story_id or style == "none":
return {"status": "error", "message": "story_id and style required"}
client = get_r2_client()
if client is None:
return {"status": "error", "message": "R2 not configured"}
bucket = os.getenv("R2_BUCKET", "darkmedia-x-studio")
# List images for this story
prefix = f"stories/{story_id}/assets/images/"
r2_keys = r2_list(prefix=prefix)
image_keys = [k for k in r2_keys if k.lower().endswith((".png", ".jpg", ".jpeg"))]
if not image_keys:
return {"status": "error", "message": "No images found for this story"}
processed = 0
errors = []
for key in image_keys:
try:
# Download image from R2
resp = client.get_object(Bucket=bucket, Key=key)
img_data = resp["Body"].read()
img = Image.open(io.BytesIO(img_data))
# Apply style
styled_img = apply_style_filter(img, style)
# Save back to R2 with _styled suffix
output = io.BytesIO()
styled_img.save(output, format="PNG")
output.seek(0)
styled_key = key.replace(".png", "_styled.png").replace(".jpg", "_styled.png").replace(".jpeg", "_styled.png")
client.put_object(Bucket=bucket, Key=styled_key, Body=output.getvalue(), ContentType="image/png")
processed += 1
except Exception as e:
errors.append(f"{key}: {str(e)}")
cache.invalidate("generated_images")
cache.invalidate("library")
cache.invalidate("stories")
return {
"status": "success",
"message": f"Applied '{style}' to {processed} images",
"processed": processed,
"errors": errors[:5] # Limit error output
}
def apply_style_filter(img, style):
"""Apply a style filter to a PIL Image."""
if style == "oil_paint":
return img.filter(ImageFilter.SMOOTH_MORE).filter(ImageFilter.EDGE_ENHANCE_MORE)
elif style == "charcoal":
return ImageOps.grayscale(img).point(lambda x: 255 if x > 128 else 0, mode="1").convert("RGB")
elif style == "sketch":
gray = ImageOps.grayscale(img)
inverted = ImageOps.invert(gray)
blurred = inverted.filter(ImageFilter.GaussianBlur(radius=2))
return ImageOps.grayscale(Image.blend(gray, blurred, 0.5)).convert("RGB")
elif style == "vintage":
enhancer = ImageEnhance.Color(img)
img = enhancer.enhance(0.7)
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(0.9)
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(1.2)
# Warm tint
r, g, b = img.split()
r = r.point(lambda x: min(255, int(x * 1.15 + 15)))
b = b.point(lambda x: max(0, int(x * 0.85)))
return Image.merge("RGB", (r, g, b))
elif style == "night_vision":
gray = ImageOps.grayscale(img)
r = gray.point(lambda x: int(x * 0.2))
g = gray.point(lambda x: min(255, int(x * 1.3)))
b = gray.point(lambda x: int(x * 0.2))
return Image.merge("RGB", (r, g, b))
elif style == "pixel_art":
w, h = img.size
small = img.resize((w // 8, h // 8), Image.NEAREST)
return small.resize((w, h), Image.NEAREST)
elif style == "vhs_static":
import random
pixels = img.load()
w, h = img.size
for y in range(0, h, 3):
for x in range(w):
r, g, b = pixels[x, y][:3]
noise = random.randint(-30, 30)
pixels[x, y] = (
min(255, max(0, r + noise + 10)),
min(255, max(0, g + noise)),
min(255, max(0, b + noise - 10))
)
return img
return img
# --- Config ---
@app.get("/api/config", tags=["System"])
@cached_endpoint(key="config", ttl=3600)
async def get_config(request: Request):
return {
"ai_mode": os.getenv("AI_MODE", "cloud"),
"image_gen_mode": os.getenv("IMAGE_GEN_MODE", "gemini"),
"voice_gen_mode": os.getenv("VOICE_GEN_MODE", "edge-tts"),
}
# --- Health Check ---
@app.get("/health", tags=["System"])
async def health():
return {"status": "ok", "service": "darkmedia-x-api"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)
|