File size: 31,237 Bytes
3e8a1f9 6985bb5 3e8a1f9 0142009 e1ffe38 799b050 0142009 5816824 6985bb5 5816824 3e8a1f9 5816824 799b050 101908c 799b050 6985bb5 84b7c6c 0142009 799b050 101908c 0142009 101908c 0142009 6985bb5 0142009 101908c 5816824 6985bb5 b7ad107 6985bb5 0142009 5816824 6985bb5 0142009 e1ffe38 101908c b7ad107 84b7c6c b7ad107 e1ffe38 b7ad107 e1ffe38 b7ad107 e1ffe38 b7ad107 e1ffe38 b7ad107 e1ffe38 b7ad107 84b7c6c e1ffe38 84b7c6c b7ad107 84b7c6c b7ad107 0142009 b7ad107 e1ffe38 b7ad107 5816824 b7ad107 0142009 b7ad107 0142009 b7ad107 0142009 b7ad107 5816824 0142009 b7ad107 5816824 b7ad107 e1ffe38 0142009 3e8a1f9 0142009 e1ffe38 84b7c6c e1ffe38 6985bb5 b7ad107 e1ffe38 b7ad107 e1ffe38 b7ad107 e1ffe38 3e8a1f9 0142009 e1ffe38 0142009 e1ffe38 84b7c6c e1ffe38 b7ad107 3e8a1f9 6985bb5 0142009 3e8a1f9 6985bb5 b7ad107 e1ffe38 b7ad107 6985bb5 b7ad107 6985bb5 e1ffe38 6985bb5 b7ad107 6985bb5 b7ad107 6985bb5 84b7c6c 6985bb5 e1ffe38 6985bb5 84b7c6c e1ffe38 b7ad107 3e8a1f9 e1ffe38 84b7c6c e1ffe38 101908c e1ffe38 3e8a1f9 101908c 0142009 799b050 3e8a1f9 0142009 b7ad107 6985bb5 b7ad107 6985bb5 84b7c6c 0142009 b7ad107 3e8a1f9 b7ad107 e1ffe38 6985bb5 b7ad107 6985bb5 b7ad107 6985bb5 e1ffe38 b7ad107 6985bb5 e1ffe38 101908c |
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 |
# app.py
from fastapi import FastAPI, Body, Query
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
from typing import List, Optional, Tuple, Literal
from transformers import pipeline
from datetime import datetime, timedelta
import random, re, itertools
# --- pytrends optional ---
try:
from pytrends.request import TrendReq
HAS_PYTRENDS = True
except Exception:
HAS_PYTRENDS = False
app = FastAPI(title="NocoAI Title & Prompt Forge (Shorts Optimized)")
# ===== ๋ชจ๋ธ =====
MODEL_ID = "google/mt5-small"
pipe = pipeline("text2text-generation", model=MODEL_ID)
# ===== ์ต์ ํธ๋ ๋ํ ์๋ (AI ์ํผ ์นํ 80+๊ฐ) =====
SEED_TOPICS = [
# ==== ๋์ค / ๋ฐ / ์ฒผ๋ฆฐ์ง ====
"AI ๋ฏธ๋
๊ฐ K-ํ ์๋ฌด๋ฅผ ์๋ฒฝํ๊ฒ ์ํํ๋ค๋ฉด",
"AI ๊ฑธ๊ทธ๋ฃน์ด ๋ด์ง์ค ๋์ค๋ฅผ ์ปค๋ฒํ๋ค๋ฉด",
"AI ๋ชจ๋ธ์ด ํฑํก ๋ฐ ๋์ค์ ๋์ ํ๋ค",
"AI ์บ๋ฆญํฐ๊ฐ โํ์
๋ณด์ดโ์ ๋ง์ถฐ ์ถค์ถ๋ค๋ฉด",
"AI๊ฐ ์์ด๋ ์ค๋์
์ ์ฐธ๊ฐํ๋ค๋ฉด",
"AI์ ์ธ๊ฐ์ด ๋์์ ์ถค์ถ ๋ ์๊ธฐ๋ ํ์",
"AI ์์ด๋์ ๋ฌด๋ ๋ฐฑ์
๋์๊ฐ ์ธ๊ฐ์ด๋ผ๋ฉด",
"AI ์ธํ๋ฃจ์ธ์๊ฐ ๋ฆด์ค ์ฒผ๋ฆฐ์ง์ ์ฐธ์ฌํ๋ค๋ฉด",
"AI๊ฐ ํ์ ํ๋๋ก ๋ฐ์ ๋ง๋ ๋ค๋ฉด",
"AI๊ฐ ๋๋ค ๋
ธ๋ ์ค์์น ๋์ค์ ๋์ ํ๋ค๋ฉด",
"AI๊ฐ ์ ๋ช
๋์ค ์๋ฌด๋ฅผ ์ด์ฌ๋ก๋ก ๋ณด์ฌ์ค๋ค๋ฉด",
"AI ์บ๋ฆญํฐ๊ฐ ๊ตญ๊ฐ๋ณ ์ ํต์ถค์ ์ปค๋ฒํ๋ค๋ฉด",
# ==== ํจ์
/ ๋ทฐํฐ / ๋ผ์ดํ ====
"AI ๋ชจ๋ธ์ด 2025 ํจ์
์ํฌ๋ฅผ ์ฅ์
ํ ์ด์ ",
"AI ๋ฏธ๋
์ ํ๋ฃจ: ์ถ๊ทผ๋ถํฐ ๋ฐ์ดํธ๊น์ง",
"AI ์ธํ๋ฃจ์ธ์์ ์
์นด ๋ฃจํด (ํ์ค๊ฐ 200%)",
"AI๊ฐ ์ง์ ๊ณ ๋ฅธ ์ค๋์ ๋ฐ์ผ๋ฆฌ๋ฃฉ",
"AI ์บ๋ฆญํฐ๊ฐ ๊พธ๋ฏผ ๋ฏธ๋๋ฉ ์ธํ
๋ฆฌ์ด",
"AI ์ฌ์น์ VLOG: ๋๋ ํ๋ฃจ ์ด์๋ณด๊ธฐ",
"AI๊ฐ ์ฝ๋ํ ๋จ์ ํจ์
, ํ์ค๋ณด๋ค ๋ซ๋ค",
"AI ํค์ด์คํ์ผ ์ถ์ฒ TOP5",
"AI๊ฐ ์๋ ค์ฃผ๋ ์ฌ๋ฆ ๋ฐ์ดํธ๋ฃฉ ๊ฟ์กฐํฉ",
"AI๋ก ๋ง๋ ์ด์ํ ์ผ๊ตด, ์ค์ ๋ก ๋ณด๋ฉด ์ด๋ฐ ๋๋",
"AI ๋ชจ๋ธ์ด ํจ์
์ผ๋ฅผ ์ฅ์
ํ ๋ ",
"AI๊ฐ ๊ณ์ ๋ณ ์ฝ๋ 10์ด ์
ํ์ ๋ณด์ฌ์ค๋ค๋ฉด",
# ==== ์ผ์ / ๋๋ผ๋งํฑ / ๋ฆฌ์ผ๋ฃจํด ====
"AI๋ก ๋ฐ๋ ๋ด ํ๋ฃจ, ์ถ๊ทผ๋ถํฐ ์ ๋ค๊ธฐ๊น์ง",
"AI๊ฐ ์ธ๊ฐ์ ํ๋ฃจ๋ฅผ ์๋ฒฝํ๊ฒ ๋ณต์ ํ๋ค๋ฉด",
"AI๊ฐ ๋ ๋์ ์ถ๊ทผํ๋ฉด ์๊ธฐ๋ ์ผ",
"AI ์ฌ์์น๊ตฌ์ ํ์ค ๋จ์์ ํ๋ฃจ ๋ธ์ด๋ก๊ทธ",
"AI๊ฐ ์ง์์ผ์ ๋งก์ผ๋ฉด ์ง์ง ํธํ ๊น",
"AI๊ฐ ๋ฐ๋ ค๊ฒฌ ์ฐ์ฑ
์ ๋์ ํ๋ฉด ๋ฒ์ด์ง๋ ์ผ",
"AI ์ธํ๋ฃจ์ธ์์ ๋ชจ๋ ๋ฃจํด์ด ์ค์ ๋ก ์กด์ฌํ๋ค๋ฉด",
"AI๊ฐ ์ง์ ๋ง๋ ์๋ฆฌ, ๋ง์์๊น?",
"AI๊ฐ ์ฃผ์ ํฌ์ํ๋ฉด ์ผ๋ง๋ ๋ฒ๊น?",
"AI ๋น์์ 24์๊ฐ ํจ๊ป ์ด์๋ณด๊ธฐ",
"AI๊ฐ ๋ ๋์ ํ์์ ์ฐธ์ํ๋ค๋ฉด",
"AI๊ฐ ์ฌํ ์ฝ์ค๋ฅผ ํ๋ฃจ ๋ง์ ์ง์ค๋ค๋ฉด",
# ==== ์ฐ์ / ๊ฐ์ / ๋ฆฌ์ก์
====
"AI ์ฌ์์น๊ตฌ๊ฐ ์งํฌ๋ฅผ ๋๋๋ค๋ฉด",
"AI ๋จ์น์ด ์นดํกๆข่ชญ็ก่ฆ ํ๋ค๋ฉด",
"AI ์บ๋ฆญํฐ๊ฐ ๊ณ ๋ฐฑ์ ๋ฐ์์ค๊น?",
"AI์ ์ธ๊ฐ์ ์ฒซ ๋ฐ์ดํธ (์๋ฎฌ๋ ์ด์
)",
"AI๊ฐ ์ฐ์ ์กฐ์ธ์ ํด์ค๋ค๋ฉด ์ง์ง ํตํ ๊น?",
"AI๊ฐ ์ธ๋ฉด ์ง์ง ๊ฐ์ ์ผ๊น?",
"AI๊ฐ ์ฌ๋์ ๋ฐฐ์ด๋ค๋ฉด ์๊ธฐ๋ ๋ณํ",
"AI๊ฐ ํค์ด์ง ์ฐ์ธ์ ๋ค์ ๋ง๋ฌ์ ๋",
"AI๊ฐ ๋ด ์ฌ์ง์ผ๋ก ๋๋ฅผ ์ฌ๋ํ๊ฒ ๋๋ค๋ฉด",
"AI์ ์ธ๊ฐ์ด ์ธ์ฐ๋ฉด ๋๊ฐ ๋จผ์ ์ฌ๊ณผํ ๊น?",
"AI๊ฐ ์๊ฐํ
์์ ๋ง์ค์ํ๋ค๋ฉด",
"AI๊ฐ ์ปคํ ์ฌ์ง์ ์ฌํด์ํด ์ค๋ค๋ฉด",
# ==== ์์ / ์ฑ๋ฆฐ์ง / ๊ฟ์กฐํฉ ====
"ํธ์์ ๊ฟ์กฐํฉ TOP5, ์ง์ง ๋ฐ์น๊ธ ์กฐํฉ ๋ชจ์",
"AI ์
ฐํ๊ฐ ๋ง๋ ๋ผ๋ฉด ๊ฟ์กฐํฉ",
"AI๊ฐ ์์์ฌ์ง์ ๋ณด๊ณ ๋ ์ํผ๋ฅผ ๋ง๋ ๋ค๋ฉด",
"AI ๋จน๋ฐฉ: ์ธ๊ฐ๋ณด๋ค ๋ ๋ฆฌ์ผํ๊ฒ ๋จน๋๋ค",
"AI ์บ๋ฆญํฐ๊ฐ ์นดํ ์๋ฃ ์ถ์ฒํด์ค๋ค",
"AI๊ฐ ๊ฐ์ฅ ์ข์ํ๋ ๊ฐ์ TOP3",
"AI ๋ค์ด์ดํธ ๋ฃจํด, ํ์ค๋ณด๋ค ์ ํํ๋ค",
"AI๊ฐ ์๋ฆฌํ๋ฉด ์ง์ง ๋ง์์๊น?",
"AI๊ฐ ๊ณ ๋ฅธ ํธ์์ ๋์๋ฝ 1๋ฑ ๋ฉ๋ด",
"AI๊ฐ ํ๊ตญ ์์ ์ฒ์ ๋จน์์ ๋ ๋ฐ์",
"AI๊ฐ 10๋ถ ๋ง์ ํ์นดํ๋ฅผ ๊ตฌํํ๋ค๋ฉด",
"AI๊ฐ ์ธ๊ณ ๊ธธ๊ฑฐ๋ฆฌ ์์์ ์ฌํํ๋ค๋ฉด",
# ==== ํ์ต / ์๊ธฐ๊ณ๋ฐ / ์ง์ ====
"ํ ๋ฌ ๋ง์ ์์ด ๋ฆฌ์ค๋ ํญ๋ฐ์ ์ผ๋ก ์ฌ๋ฆฌ๋ ๋ฒ",
"AI๊ฐ ์๋ ค์ฃผ๋ ๊ณต๋ถ ์ง์ค๋ ฅ ๋ฃจํด",
"AI๊ฐ ์ง์ ์ค๊ณํ โ๊ธฐ์ต๋ ฅ ํ๋ จ๋ฒโ",
"AI๊ฐ ์ฝ๋ฉ ๋ฐฐ์์ ๋ง๋ ์ฒซ ํ๋ก์ ํธ",
"AI ํํฐ์ ํจ๊ป ์์ดํํ ์ฐ์ตํ๊ธฐ",
"AI๊ฐ ์ค๋งํธํฐ ์ค๋
์ ๊ณ ์น๋ ๋ฒ",
"AI๊ฐ ์ธ๊ฐ๋ณด๋ค ๋น ๋ฅด๊ฒ ์ฑ
์์ฝํ๋ ๋ฒ",
"AI๊ฐ ๋ง๋ ์ํ ๋๋น ๊ณต๋ถ ๋ฃจํด",
"AI๊ฐ ๋ ๊ณผํ์ผ๋ก ์ง์ค๋ ฅ์ ๋์ธ๋ค",
"AI ์ ์๋์ด ์๋ ค์ฃผ๋ ํ ์ต ๋ฆฌ์ค๋ ๋น๋ฒ",
"AI๊ฐ ํ๋ฃจ 10๋ถ ์คํผํน ํ๋ จ ๋ฃจํด์ ์ง์ค๋ค๋ฉด",
# ==== ์ฌํ / ๋ด์ค / ๋ฐ์ ====
"AI๊ฐ ๋ง๋ ๊ฐ์ง ๋ด์ค, ๋น์ ์ ์์๊น",
"AI ์บ๋ฆญํฐ๊ฐ ๋ํต๋ น์ ์ถ๋งํ๋ค๋ฉด",
"AI ๊ธฐ์๊ฐ ์ง์ ๋ด์ค ์งํ์ ๋งก๋๋ค๋ฉด",
"AI๊ฐ ์ธํ๋ฃจ์ธ์๋ฅผ ๋์ฒดํ ์ธ์",
"AI๊ฐ ๋ง๋ ๊ด๊ณ , ์ง์ง๋ณด๋ค ์ค๋๋ ฅ ์๋ค",
"AI ์บ๋ฆญํฐ๊ฐ ํ ๋ก ํ๋ก๊ทธ๋จ์ ๋์จ๋ค๋ฉด",
"AI ๋ชจ๋ธ์ด SNS๋ฅผ ์ ๋ นํ๋ ์ด์ ",
"AI๊ฐ ์ ๊ฑฐ์ ๊ฐ์
ํ๋ค๋ฉด ์๊ธธ ์ผ",
"AI์ ๊ฐ์ ํํ์ด ์ง์ง์ฒ๋ผ ๋ณด์ผ ๋",
"ํน์ด์ ์ด ์จ AI ์ธ์, ์๊ธฐ์ง๋ง ๋ฌด์ญ๋ค",
"AI๋ก ๋ง๋ ์ํ ์๊ณ ํธ์ด ์ง์ง ์ํ๋ณด๋ค ๋ฉ์ง๋ค",
"๊ฐค๋ญ์ vs ์์ดํฐ ์นด๋ฉ๋ผ, ์ง์ง ์ฐจ์ด 1์ด ๋น๊ต",
"AI ์์ด๋์ด ๋ถ๋ฅด๋ K-POP ์ปค๋ฒ ๋ฌด๋",
"AI๋ก ๋ง๋ ๊ฐ์ ์์ด๋, ์ง์ง๋ณด๋ค ์์ ์ด์ ",
]
_seed_iter = itertools.cycle(SEED_TOPICS)
# ===== ์์ฒญ/์๋ต =====
class GenerateReq(BaseModel):
mode: Optional[str] = Field(None, description="auto | topic")
topic: Optional[str] = None
count: int = Field(30, ge=1, le=80, description="์์ฑํ ์ ๋ชฉ ์(์ต๋ 80)")
# ์ธ์ด/๊ตญ๊ฐ (ํ์งํ)
lang: str = Field("ํ๊ตญ์ด", description="์: ํ๊ตญ์ด/Korean/ko/ko-KR/English/en/zh ๋ฑ")
country: Optional[str] = Field(None, description="์: KR, US, JP, ES, MX, CN ๋ฑ. ์์ผ๋ฉด ์ธ์ด์ ๋ง์ถฐ ์ถ๋ก ")
length: str = Field("medium", description="short | medium | long")
style: str = Field("meme-shock", description="meme-shock | wholesome | informative | clickbait")
emoji: int = Field(1, ge=0, le=2)
numberless: bool = Field(True)
extra_hint: Optional[str] = None
trend_source: str = Field("pytrends", description="pytrends | seed")
passes: int = Field(3, ge=1, le=6, description="๋ค์์ฑ ํ๋ณด์ฉ ์ํ๋ง ํ์")
class GenerateResp(BaseModel):
topic: str
titles: List[str]
used_prompt: str
trend_source: str
class TopicResp(BaseModel):
topics: List[str]
lang: str
country: str
category: Optional[str] = None
class ShortsPromptReq(BaseModel):
topic: str = Field(..., description="์ฃผ์ (์: ํธ์์ ๊ฟ์กฐํฉ TOP5)")
title: str = Field(..., description="์ ์ ๋ ์ ๋ชฉ(ํ ์ค)")
lang: str = Field("ko")
style: str = Field("meme-shock")
tone: Optional[str] = Field(None, description="energetic | wholesome | informative | edgy ๋ฑ")
duration_sec: int = Field(30, ge=15, le=60)
cuts: int = Field(5, ge=5, le=8)
emoji: int = Field(1, ge=0, le=2)
class ShortsPromptResp(BaseModel):
topic: str
title: str
prompt: str
lang: str
style: str
cuts: int
duration_sec: int
# ===== ์ธ์ด/๊ตญ๊ฐ ์ ๊ทํ =====
LANG_MAP = {
"ko": ("ko", "ํ๊ตญ์ด"), "ko-kr": ("ko", "ํ๊ตญ์ด"), "korean": ("ko", "ํ๊ตญ์ด"), "ํ๊ตญ์ด": ("ko", "ํ๊ตญ์ด"),
"en": ("en", "English"), "en-us": ("en", "English"), "english": ("en", "English"), "์์ด": ("en", "English"),
"ja": ("ja", "ๆฅๆฌ่ช"), "ja-jp": ("ja", "ๆฅๆฌ่ช"), "japanese": ("ja", "ๆฅๆฌ่ช"), "์ผ๋ณธ์ด": ("ja", "ๆฅๆฌ่ช"),
"es": ("es", "Espaรฑol"), "es-es": ("es", "Espaรฑol"), "es-mx": ("es", "Espaรฑol"), "spanish": ("es", "Espaรฑol"), "์คํ์ธ์ด": ("es", "Espaรฑol"),
"zh": ("zh", "ไธญๆ"), "zh-cn": ("zh", "ไธญๆ"), "chinese": ("zh", "ไธญๆ"), "์ค๊ตญ์ด": ("zh", "ไธญๆ"), "ไธญๆ": ("zh", "ไธญๆ"),
}
PNT_MAP = {
"KR": ("south_korea", "ko-KR"),
"US": ("united_states", "en-US"),
"JP": ("japan", "ja-JP"),
"ES": ("spain", "es-ES"),
"MX": ("mexico", "es-419"),
"CN": ("china", "zh-CN"), # pytrends ๋ฏธ์ง์/๋ถ์์ ๊ฐ๋ฅ์ฑ ์์
}
LANG_DEFAULT_COUNTRY = {"ko": "KR", "en": "US", "ja": "JP", "es": "ES", "zh": "CN"}
def normalize_lang_and_country(lang_in: str, country_in: Optional[str]) -> Tuple[str, str, str]:
key = (lang_in or "").strip().lower().replace("_", "-")
if key in LANG_MAP: lang_code, pretty = LANG_MAP[key]
else: lang_code, pretty = ("ko", "ํ๊ตญ์ด")
country = (country_in or "").upper().strip() or LANG_DEFAULT_COUNTRY.get(lang_code, "US")
if country not in PNT_MAP: country = LANG_DEFAULT_COUNTRY.get(lang_code, "US")
return lang_code, pretty, country
# ===== ํ์งํ ๋ฆฌ์์ค =====
EXAMPLES = {
"ko": ("์์(ํ์๋ง ์ฐธ๊ณ ):\n"
"์์ดํฐ๋ ํ๋ค๋ฆฐ ๊ฐค๋ญ์ ๊ธฐ๋ฅ, ์ด๊ฑด ๋ชฐ๋์ฃ ?\n"
"AI๋ก 1๋ถ ์ปท ์์ ๋ง๋ค๊ธฐโฆ ์คํ๋ ๐คฏ\n"
"ํธ์์ ์กฐํฉ TOP5, ๋ง์ง๋ง์ด ๋ฏธ์ณค๋ค ใทใท\n"),
"en": ("Examples (format only):\n"
"iPhone users switching? This Galaxy feature is wild ๐คฏ\n"
"Make a viral short in 60sโฆ actually works\n"
"Top 5 convenience-store hacks (the last one is insane)\n"),
"ja": ("ไพ๏ผๅฝขๅผใฎใฟๅ่๏ผ:\n"
"iPhoneๆดพใๆบใใGalaxyๆฉ่ฝใ็ฅใฃใฆใ๏ผ\n"
"AIใง60็งใทใงใผใไฝๆโฆใใธใ ๐คฏ\n"
"ใณใณใใ็ฅใขใฌใณใธTOP5ใๆๅพใใคใใ\n"),
"es": ("Ejemplos (solo formato):\n"
"ยฟUsuarios de iPhone dudando? Esta funciรณn de Galaxy es una locura ๐คฏ\n"
"Crea un short viral en 60sโฆ funciona de verdad\n"
"Top 5 trucos de tienda, el รบltimo te vuela la cabeza\n"),
"zh": ("็คบไพ๏ผไป
ไพๆ ผๅผๅ่๏ผ๏ผ\n"
"iPhone็จๆท้ฝ้ๆไบ๏ผ่ฟ้กนGalaxyๅ่ฝๅคช็ฆป่ฐฑไบ ๐คฏ\n"
"AIไธๅ้ๅ็ๆฌพ็ญ่ง้ข๏ผ็็ๅ็๏ผ\n"
"ไพฟๅฉๅบ็ฅ็ปๅTOP5๏ผๆๅไธไธช็ดๆฅ็ธ่ฃ๏ผ\n"),
}
HOOKS = {
"ko": ["์ด๊ฑด ๋ชฐ๋์ฃ ?", "์๋ฆ", "์ถฉ๊ฒฉ", "์คํ๋", "TOP5", "ในใ
", "๋๋ฐ", "๋ฏธ์ณค๋ค", "๋ฐ์ ์์", "ํ์ค ์์ฝ", "์ง๊ธ ์ ๋ณด๋ฉด ์ํด", "30์ด ์ปท", "๋๊น์ง ๋ณด์ธ์", "์๊ณ ๋ณด๋ฉด ๋ ์๋ฆ", "์ด๊ฐ๋จ"],
"en": ["did you know?", "shocking", "insane", "is this real?", "TOP 5", "no way", "viral", "mind-blown", "plot twist", "one-line tip", "donโt miss this", "30s hack", "watch till the end", "low-key genius"],
"ja": ["็ฅใฃใฆใ๏ผ", "้ณฅ่", "่กๆ", "ใใธ๏ผ", "TOP5", "ใใฐใ", "ใใบใ", "็ฅ", "ใพใใใฎๅฑ้", "่ฆ็นใ ใ", "่ฆ้ใๅณ็ฆ", "30็งใงOK", "ๆๅพใพใง่ฆใฆ", "ๅฎใฏใณใฌ"],
"es": ["ยฟlo sabรญas?", "impactante", "una locura", "ยฟes real?", "TOP 5", "no puede ser", "viral", "mente en blanco", "giro inesperado", "tip en una lรญnea", "no te lo pierdas", "truco de 30s", "hasta el final", "dato clave"],
"zh": ["้ๆ", "ๅคช็ฆป่ฐฑไบๅง", "ไฝ ็ปๅฏนๆณไธๅฐ", "TOP5", "็ฝๅ้ฝๆๅไบ", "ๅคช็ธไบ", "็ฌๆญป", "็ ด้ฒไบ", "่ฟๆไฝ้ๅคฉ", "็ๆขๆ", "ไธ็ๅๆ", "็ปๆๆๆ", "็ปไบ", "ๅคช็ ไบ"],
}
TAILS = {
"ko": ["๋ฐฉ๋ฒ ๊ณต๊ฐ", "์์ฒญ์ ํ๊ธฐ ํฐ์ง", "์ /ํ ๋น๊ต", "์ด๋ณด๋ ๊ฐ๋ฅ", "์จ์ ๊ธฐ๋ฅ", "๋ฐ์ ์ฃผ์", "์ค์ ๊ฟํ", "๋ชจ๋ฅด๋ฉด ์ํด", "ํ ๋ฒ์ ๋", "๋ฐ๋ก ์จ๋จน๋ ์คํฌ", "ํต์ฌ๋ง ์์ฝ", "์ ๋ฆฌํด๋๋ฆผ"],
"en": ["method revealed", "viewers went crazy", "before vs after", "beginner friendly", "hidden feature", "twist ahead", "pro tips", "donโt miss out", "done in one go", "instant skill", "key points only", "quick recap"],
"ja": ["ใใๆนๅ
ฌ้", "่ฆ่ด่
ใฎๅๅฟใ็ฅ", "ใใใฉใผ/ใขใใฟใผ", "ๅๅฟ่
OK", "้ ใๆฉ่ฝ", "ใใฟใใฌๆณจๆ", "ๅฎ็จใใฏ", "็ฅใใชใใๆ", "ไธ็บใงๅฎไบ", "ใใไฝฟใใใฏใถ", "่ฆ็นใ ใ", "ใพใจใใฆ่งฃ่ชฌ"],
"es": ["mรฉtodo revelado", "los viewers enloquecen", "antes vs despuรฉs", "para principiantes", "funciรณn oculta", "plot twist", "tips pro", "no te lo pierdas", "todo de una", "skill al instante", "puntos clave", "resumen rรกpido"],
"zh": ["ๅฎๆด็ๆฅไบ", "็ฝๅ็ญ่ฎฎไธญ", "็ๅฎๆๆฒ้ปไบ", "ๅๅๅฏนๆฏ็ฆป่ฐฑ", "ๆฐๆไน่ฝๅ", "้่ๅ่ฝๆๅ
", "่ๅๅๅ ้ๆผ", "ไธ็็็ไบ", "ๆ็จๆฅไบ", "ๅ
จ็ฝ็็ซ", "ไบฒๆตๆๆ", "ๅนฒ่ดงๆป็ป"],
}
PROMPT_HEADER = {
"ko": "์ญํ : ๋น์ ์ ํด๋น ๊ตญ๊ฐ์ ์ ํ๋ธ ์ผ์ธ ์นดํผ๋ผ์ดํฐ๋ค.\n๊ณผ์
: ์๋ โ์ฃผ์ โ๋ก ํ์ง ์์ฒญ์๊ฐ ๋ง์ด ๋ณด๊ฒ๋ ๋ฐ์ด๋ด ์ ๋ชฉ์ ์์ฑํ๋ผ.\n",
"en": "Role: You are a YouTube Shorts copywriter for the target country.\nTask: Generate viral titles so local viewers want to watch.\n",
"ja": "ๅฝนๅฒ: ใใชใใฏๅฏพ่ฑกๅฝๅใใฎYouTubeใทใงใผใใณใใผใฉใคใฟใผใงใใ\n่ชฒ้ก: ็พๅฐ่ฆ่ด่
ใ่ฆใใใชใใใคใฉใซใฟใคใใซใไฝๆใใฆใใ ใใใ\n",
"es": "Rol: Eres copywriter de YouTube Shorts para el paรญs objetivo.\nTarea: Genera tรญtulos virales para atraer a los espectadores locales.\n",
"zh": "่ง่ฒ: ไฝ ๆฏไธๅ้ขๅไธญๅฝ่งไผ็YouTube Shortsๆๆกๅไฝ่
ใ\nไปปๅก: ๆ นๆฎไธป้ข็ๆ่ฝ่ฎฉๅฝๅฐ่งไผๅไธๆปๅจใ็นๅผ่ง็็็ๆฌพๆ ้ขใ\n",
}
def length_rule(length: str, lang_code: str) -> str:
if lang_code in ("ko", "ja", "zh"):
return {"short": "๊ฐ ์ ๋ชฉ์ 10~18์ (์ํฉํธ)",
"medium": "๊ฐ ์ ๋ชฉ์ 16~28์ (์งง๊ณ ๊ฐ๋ ฌ)",
"long": "๊ฐ ์ ๋ชฉ์ 24~38์ (๋ฐ์ /์คํ ๋ฆฌ)"}\
.get(length, "๊ฐ ์ ๋ชฉ์ 16~28์")
else:
return {"short": "Each title 3โ6 words (punchy)",
"medium": "Each title 5โ9 words (concise, strong hook)",
"long": "Each title 8โ14 words (with twist/story)"}\
.get(length, "Each title 5โ9 words")
def style_block(style: str, lang_code: str) -> str:
blocks = {
"ko": {"meme-shock": "- ๋ฐ/๋ฐ์ /๊ฐ์ ์๊ทน ์ ๊ทน ํ์ฉ\n- ใ
ใ
ใ
, ใทใท, ใ
ใ
, ๐คฏ ํ์ฉ\n- ํธ๊ธฐ์ฌ/์ถฉ๊ฒฉ ๋จ์ด ์ ๊ทน ์ฌ์ฉ",
"wholesome": "- ๊ณต๊ฐ/ํ๋ง ์ค์ฌ, ๋ฐ๋ปํ ์ดํ",
"informative": "- ํต์ฌ ํค์๋ ์ค์ฌ, ์ ๋ขฐ/๊ฐ๊ฒฐ",
"clickbait": "- ๊ณผ์ฅ/๋ฏธ์คํฐ๋ฆฌ ํค, ๊ถ๊ธ์ฆ ์ ๋ฐ(์ฌ์ค ์๊ณก ๊ธ์ง)"},
"en": {"meme-shock": "- Use memes/twist/emotion\n- Allow ๐คฏ/omg vibe\n- Strong curiosity/shock words",
"wholesome": "- Relatable/warm tone",
"informative": "- Key facts first, concise & credible",
"clickbait": "- Tease mystery, no deception"},
"ja": {"meme-shock": "- ใใฟ/ใฎใฃใใ/ๆๆ
ๅบๆฟใๆดป็จ\n- ๐คฏ/ใใฐใ ็ญOK\n- ๅฅฝๅฅๅฟใป้ฉใใฏใผใใๅผทใใซ",
"wholesome": "- ๅ
ฑๆใปใปใฃใใ็ณปใฎ่ชๅฝ",
"informative": "- ่ฆ็น้่ฆใ็ฐกๆฝ",
"clickbait": "- ใในใใชใผ่ชฟใงๅผใใ่ชๅผตใฏใใคใคใ่ชคๆ
ๅ ฑ็ฆๆญข"},
"es": {"meme-shock": "- Usa memes/giros/emociรณn\n- Se permite ๐คฏ vibes\n- Palabras de curiosidad/sorpresa fuertes",
"wholesome": "- Tono cercano y cรกlido",
"informative": "- Datos clave primero, conciso y fiable",
"clickbait": "- Genera misterio sin engaรฑo"},
"zh": {"meme-shock": "- ๅผบๅๆข/ๅ่ฝฌ/ๆ
็ปชๅผ ๅ\n- ๅ
่ฎธ ๐คฏ/็ฆป่ฐฑ/ๆ ่ฏญ ็ญ่ฏญๆฐ\n- ไฝฟ็จๅผบ็ๅฅฝๅฅ/้ๆ่ฏ",
"wholesome": "- ๅ
ฑๆ
/ๆฒปๆ้ฃ๏ผ่ฏญๆฐๆธฉๆ",
"informative": "- ๅ
ณ้ฎไฟกๆฏไผๅ
๏ผ็ฎๆดๅฏไฟก",
"clickbait": "- ๅ่ถณ่ๅฃ๏ผ้ฟๅ
่ๅๅคธๅคง"},
}
return blocks.get(lang_code, {}).get(style, "")
def build_prompt(lang_code: str, pretty_lang: str, topic: str, count: int, length: str,
style: str, emoji: int, numberless: bool, extra_hint: Optional[str]) -> str:
example = EXAMPLES.get(lang_code, EXAMPLES["ko"])
header = PROMPT_HEADER.get(lang_code, PROMPT_HEADER["ko"])
rules = [
f"- ์ฃผ์ /Topic: {topic}",
"- ์ ํ๋ธ ์ผ์ธ ์ฉ ๋ฐ์ด๋ด ์ ๋ชฉ๋ง ์์ฑ / Generate only viral titles for Shorts",
"- ๊ฐ๋ ฅํ ํ
(์ฒซ 5์)ยทํธ๊ธฐ์ฌ ๊ฐญยท๋ฐ์ /๊ฐํ ํฌํจ / Strong hook, curiosity gap, twist",
length_rule(length, lang_code),
f"- ์ด๋ชจ์ง๋ ์ต๋ {emoji}๊ฐ / Up to {emoji} emoji(s)",
"- ๋ฒํธยท๋ถ๋ฆฟยท๋ฐ์ดํยท์ค๋ช
์์ด, ์ ๋ชฉ๋ง ํ ์ค์ฉ / No numbers/quotes/explanations",
"- ์๋ก ๋ค๋ฅธ ์์ด๋์ด๋ก ๋ค์ํ๊ฒ(์ค๋ณต ๊ธ์ง) / Ensure diversity, no duplicates",
"- ๊ณผ๋ํ ์์ค/์ ์ ์ฑยท์ ์๊ถ/๋ช
์ํผ์ ๊ธ์ง / No slurs/adult content/defamation",
style_block(style, lang_code),
(f"- ์ถ๊ฐ ํํธ / Extra hint: {extra_hint}" if extra_hint else ""),
f"- ๋ฐ๋์ ์ด {count}์ค ์ถ๋ ฅ / Exactly {count} lines",
]
rule_text = "\n".join([r for r in rules if r])
return f"{header}{rule_text}\n\n{example}\n์ถ๋ ฅ/Output: ์ ๋ชฉ๋ง ์ค๋ฐ๊ฟ์ผ๋ก ๋์ด / One title per line.\n"
# ===== ํธ๋ ๋ ์บ์/์กฐํ =====
_cache = {"topics": [], "ts": datetime.min, "key": ""}
def fetch_trends_pytrends(n: int, country: str) -> List[str]:
if not HAS_PYTRENDS:
return []
pn, hl = PNT_MAP.get(country, ("south_korea", "ko-KR"))
cache_key = f"{pn}:{hl}"
global _cache
if _cache["topics"] and _cache["key"] == cache_key and datetime.utcnow() - _cache["ts"] < timedelta(minutes=10):
return _cache["topics"][:n]
try:
pt = TrendReq(hl=hl, tz=540)
df = pt.trending_searches(pn=pn)
topics = [x for x in df[0].tolist() if isinstance(x, str)]
_cache = {"topics": topics, "ts": datetime.utcnow(), "key": cache_key}
return topics[:n]
except Exception:
return []
def pick_auto_topic(source: str, country: str) -> str:
if source == "pytrends" and HAS_PYTRENDS:
topics = fetch_trends_pytrends(8, country)
if topics: return random.choice(topics)
base = next(_seed_iter)
pool = [base] + random.sample(SEED_TOPICS, k=min(3, len(SEED_TOPICS)))
return random.choice(pool)
# ===== ์ ๋ชฉ ํ์ฒ๋ฆฌ(๊ฐํ) =====
_LEAK_PATTERNS = [
r'^\s*(์ฃผ์ |topic)\s*[:/]',
r'^\s*(์ถ๋ ฅ|output)\s*[:/]',
r'^\s*์์',
r'์์ฑํ๋ผ[.!?]?\s*$',
r'^\s*[-โข]\s*$',
r'^\s*๊ฐ ์ ๋ชฉ์',
]
def _fingerprint(s: str) -> str:
return re.sub(r"[\s\W]+", "", s.lower())
def _valid_len(s: str, lang_code: str) -> bool:
if lang_code in ("en", "es"):
words = re.findall(r"\w+", s)
return 3 <= len(words) <= 14
return 6 <= len(s) <= 36 # ko/ja/zh
def _leak(s: str) -> bool:
for p in _LEAK_PATTERNS:
if re.search(p, s, flags=re.I):
return True
return False
def postprocess(raw: str, lang_code: str, numberless: bool = True) -> List[str]:
if not raw: return []
raw = re.sub(r"<extra_id_\d+>", "", raw)
out = []
for line in raw.splitlines():
s = line.strip()
if not s: continue
if numberless:
s = re.sub(r'^[\-\*\d]+\s*[\.\)]?\s*', '', s)
s = re.sub(r'^[\"\':โโโโ]+', '', s).strip()
if _leak(s):
continue
if _valid_len(s, lang_code):
out.append(s)
seen, dedup = set(), []
for s in out:
key = _fingerprint(s)
if key not in seen:
seen.add(key)
dedup.append(s)
return dedup
def generate_titles(topic: str, count: int, req, lang_code: str, pretty_lang: str) -> Tuple[List[str], str]:
base_params = [(0.9,0.9),(1.0,0.92),(0.85,0.95),(1.05,0.88),(0.95,0.97),(1.1,0.9)]
param_grid = base_params[:max(1, min(req.passes, len(base_params)))]
collected, used_prompts = [], []
for (temperature, top_p) in param_grid:
prompt = build_prompt(lang_code, pretty_lang, topic, count, req.length, req.style, req.emoji, req.numberless, req.extra_hint)
gen = pipe(
prompt,
max_new_tokens=180 if req.length in ("medium","long") else 120,
temperature=float(temperature),
top_p=float(top_p),
do_sample=True,
repetition_penalty=1.12,
no_repeat_ngram_size=3,
)
text = gen[0].get("generated_text", "").strip()
used_prompts.append(prompt)
batch = postprocess(text, lang_code, numberless=req.numberless)
collected.extend(batch)
seen, dedup = set(), []
for s in collected:
key = _fingerprint(s)
if key not in seen:
seen.add(key)
dedup.append(s)
if len(dedup) < count:
hooks = HOOKS.get(lang_code, HOOKS["ko"])
tails = TAILS.get(lang_code, TAILS["ko"])
templates = []
for _ in range(count * 2):
h = random.choice(hooks); t = random.choice(tails)
if lang_code in ("en", "es"): templates.append(f"{topic}: {h}, {t}")
else: templates.append(f"{topic} {h} {t}")
for s in templates:
if not _valid_len(s, lang_code): continue
key = _fingerprint(s)
if key not in seen:
seen.add(key); dedup.append(s)
if len(dedup) >= count: break
return dedup[:count], (used_prompts[-1] if used_prompts else "")
# ===== ํ ํฝ ํฌ์ง(์์ํ ๊ฐ๋ฅํ ์ฃผ์ ์๋ ์์ฑ) =====
TOPIC_CATEGORIES = ("ai-slice","ai-teach","ai-transform","ai-battle","ai-micro","mixed")
OBJ = {
"ko": {
"fruit": ["์๋ฐ","๋ง๊ณ ","ํ์ธ์ ํ","์ฌ๊ณผ","ํค์","์ฉ๊ณผ","๋ณต์ญ์","๋ฐ๋๋","์ฝ์ฝ๋","ํฌ๋"],
"planet": ["์ง๊ตฌ","ํ์ฑ","ํ ์ฑ","๋ชฉ์ฑ","๋ฌ","๊ธ์ฑ","ํด์์ฑ","์ฒ์์ฑ","ํ์(๋ฏธ๋๋ฒ์ )"],
"animal": ["๊ณ ์์ด","๊ฐ์์ง","ํ๋ค","์๋ฌ","์ต๋ฌด์","๊ณ ์ด๋์น","์นดํผ๋ฐ๋ผ"],
"item": ["๋ฃจ๋น
์คํ๋ธ","์ด์ฝ๋ฆฟ๋ฐ","๋ผ๋ฉด","๋ก๋ณถ์ด","์ค๋์ปค์ฆ","์ถ๊ตฌ๊ณต","๋๋ก "],
"subject": ["์์ด","ํ๊ตญ์ด","์ํ","์ถค","์๋ฆฌ","๊ฒ์","๋๋ก์"],
"elder": ["ํ ๋จธ๋","ํ ์๋ฒ์ง"],
"persona": ["์ธ๊ตญ์ธ","๋ก๋ด","๋ง๋ฒ์ฌ","์ํผํ์ด๋ก","AI ๋น์","์ ํ๋ฒ"],
"micro": ["๋ฏธ์ธ๋จผ์ง","์ธํฌ","๋ฐ์ด๋ฌ์ค","๋ฐํ
๋ฆฌ์","๋ฌผ๋ฐฉ์ธ","์คํ ๊ฒฐ์ "],
},
"en": {
"fruit": ["watermelon","mango","pineapple","apple","kiwi","dragon fruit","peach","banana","coconut","grapes"],
"planet": ["Earth","Mars","Saturn","Jupiter","Moon","Venus","Neptune","Uranus","mini Sun"],
"animal": ["cat","dog","panda","otter","parrot","hedgehog","capybara"],
"item": ["Rubikโs cube","chocolate bar","ramen","spicy rice cake","sneakers","soccer ball","drone"],
"subject": ["English","Korean","Math","Dance","Cooking","Gaming","Drawing"],
"elder": ["grandma","grandpa"],
"persona": ["foreigner","robot","wizard","superhero","AI assistant","YouTuber"],
"micro": ["fine dust","cells","virus","bacteria","droplet","sugar crystal"],
},
}
def _obj(lang: str) -> dict:
return OBJ.get(lang, OBJ["ko"])
def forge_topic(lang_code: str, category: str) -> str:
o = _obj(lang_code)
if category == "ai-slice":
thing = random.choice(o["fruit"] + o["planet"] + o["item"])
return f"AI๊ฐ {thing}๋ฅผ ๋ ์ด์ ์นผ๋ก ์๋ฅด๋ ์์"
if category == "ai-teach":
who = random.choice(o["persona"] + o["elder"])
subj = random.choice(o["subject"])
return f"{who}๊ฐ ์ ๋จธ๋ฌ์คํ๊ฒ {subj}๋ฅผ ๊ฐ๋ฅด์น๋ 15์ด ์ผ์ธ "
if category == "ai-transform":
a = random.choice(o["animal"] + o["item"])
b = random.choice(o["fruit"] + o["planet"])
return f"AI๊ฐ {a}๋ฅผ {b}(์ผ)๋ก ๋ณ์ ์ํค๋ ํ์๋ฉ์ค"
if category == "ai-battle":
a = random.choice(o["animal"] + o["item"])
b = random.choice(o["planet"] + o["fruit"])
return f"AI๊ฐ {a} vs {b} ๋๊ฒฐ์ ์๋ฎฌ๋ ์ด์
ํ๋ ์์"
if category == "ai-micro":
m = random.choice(o["micro"])
t = random.choice(o["fruit"] + o["item"])
return f"AI ๋ง์ดํฌ๋ก ์ธ๊ณ ํํ: {t} ์ {m} ํ๋ ๊ด์ฐฐ"
return forge_topic(lang_code, random.choice(TOPIC_CATEGORIES[:-1])) # mixed
# ===== ์ผ์ธ ํ๋กฌํํธ(์ท๋ฆฌ์คํธ) ์์ฑ =====
def build_shorts_prompt(topic: str, title: str, lang_code: str, style: str, tone: Optional[str], cuts: int, duration_sec: int, emoji: int) -> str:
# ์ปท ๊ฐ์ ๋ณด์
cuts = max(5, min(8, cuts))
per_cut = max(4, min(7, duration_sec // cuts)) # ์ปท๋น ์ด
style_note = {
"meme-shock": "๊ฐํ ํ
+ ๋ฐ์ + ๋ฆฌ๋ฌ. ์๋ง ์นํ. ์งง์ ๊ตฌ/๋ช
๋ น๋ฌธ.",
"wholesome": "๊ณต๊ฐ/ํ๋ง ํค. ์น์ ํ ๋งํฌ. ํฌ๋ง/์์.",
"informative": "ํต์ฌ ๋จผ์ , ํฉํธ ์ค์ฌ. ๊ฐ๊ฒฐ/์ ๋ขฐ.",
"clickbait": "๊ถ๊ธ์ฆ ์ ๋ฐ, ๊ณผ์ฅ ํ์ฉ(์ฌ์ค ์๊ณก ๊ธ์ง).",
}.get(style, "๋ฆฌ๋ฌยทํธ๊ธฐ์ฌ ์ค์ฌ.")
tone_note = f"์ถ๊ฐ ํค: {tone}." if tone else ""
emj = " ์ด๋ชจ์ง ์ ๋นํ ์ฌ์ฉ" if emoji > 0 else " ์ด๋ชจ์ง ์ฌ์ฉ ๊ธ์ง"
header = (
f"์ญํ : ์ ํ๋ธ ์ผ์ธ ์ฝํ
์ธ ํ๋๋ & ์นดํผ๋ผ์ดํฐ\n"
f"๋ชฉํ: ์๋ '์ ๋ชฉ'์ ๊ธฐ๋ฐ์ผ๋ก {duration_sec}์ด ์ด๋ด ์์ ํ๋กฌํํธ(๋๋ณธ ์ง์๋ฌธ)๋ฅผ ์์ฑํ๋ค.\n"
f"๊ท์น:\n"
f"- ์ด๋ฐ 3์ด ๊ฐํ ํ
, 3~4๊ฐ์ ํต์ฌ ํฌ์ธํธ, ๋ง์ง๋ง์ ์ก์
์ฝ(์ข์์/๋๊ธ ์ ๋)\n"
f"- ์งง๊ณ ๋ฆฌ๋ฌ๊ฐ ์๊ฒ, ์๋ง์ฉ ๋ฌธ์ฅ\n"
f"- ์ปท(์ฅ๋ฉด)์ ๋ฒํธ๋ก ๊ตฌ๋ถ, ๊ฐ ์ปท 1~2๋ฌธ์ฅ\n"
f"- ์ดฌ์ ์์ค ์์ด๋ ์์ฑํ ์์ํด์ ๋ฐ๋ก ๋ฃ์ ์ ์๋ ๋ฌ์ฌ ํฌํจ\n"
f"- ์คํ์ผ: {style} ({style_note}), ์ธ์ด: {lang_code}, {tone_note}{emj}\n"
f"- ์ปท ์: {cuts}๊ฐ, ์ปท๋น {per_cut}์ด ๋ด์ธ\n\n"
f"๋ฉํ:\n"
f"โข ์ฃผ์ (Topic): {topic}\n"
f"โข ์ ๋ชฉ(Title): {title}\n\n"
f"์ถ๋ ฅ ํฌ๋งท ์์(ํ์๋ง ์ฐธ๊ณ ):\n"
f"1) ํ
: ์์ฒญ์ ํธ๊ธฐ์ฌ ํญ๋ฐ ํ ์ค\n"
f"2) ํฌ์ธํธ1: ๊ตฌ์ฒด ์์/๋ธ๋๋/๊ฐ๊ฒฉ ๋ฑ\n"
f"3) ํฌ์ธํธ2: ๋น๊ต/๋ฐ์ /์ฃผ์\n"
f"4) ํฌ์ธํธ3: ์ค์ ํ/๋ฐ๋ก ์จ๋จน๊ธฐ\n"
f"5) ๋ง๋ฌด๋ฆฌ: ํ ์ค ์์ฝ + ์ก์
์ฝ(๋๊ธ/์ข์์ ์ ๋)\n\n"
f"์ด์ ์ ๊ท์น์ ์ฒ ์ ํ ์ง์ผ {cuts}๊ฐ์ ์ปท์ผ๋ก ํ๊ตญ์ด ํ๋กฌํํธ๋ฅผ ์ถ๋ ฅํ๋ผ."
)
return header
# ===== ์๋ํฌ์ธํธ =====
@app.get("/topic/auto", response_model=TopicResp)
def topic_auto(
count: int = Query(10, ge=1, le=100),
lang: str = Query("ko"),
country: Optional[str] = Query(None),
category: Optional[Literal["ai-slice","ai-teach","ai-transform","ai-battle","ai-micro","mixed"]] = Query("mixed")
):
lang_code, pretty_lang, country_code = normalize_lang_and_country(lang, country)
cats = [category] if category and category != "mixed" else None
topics = []
if HAS_PYTRENDS:
trendy = fetch_trends_pytrends(min(5, count), country_code)
topics.extend(trendy)
while len(topics) < count:
cat = random.choice(cats) if cats else random.choice(TOPIC_CATEGORIES[:-1])
topics.append(forge_topic(lang_code, cat))
seen, uniq = set(), []
for t in topics:
k = re.sub(r"\s+", "", t.lower())
if k not in seen:
seen.add(k)
uniq.append(t)
if len(uniq) >= count: break
return JSONResponse({"topics": uniq, "lang": pretty_lang, "country": country_code, "category": category}, media_type="application/json; charset=utf-8")
@app.get("/")
def root():
return JSONResponse({"message": "NocoAI Title & Prompt Forge. Try GET /topic/auto, POST /generate, GET /generate/auto, POST /prompt/shorts"}, media_type="application/json; charset=utf-8")
@app.get("/health")
def health():
return JSONResponse({"status": "ok", "model": MODEL_ID, "cache_size": len(_cache.get("topics", []))}, media_type="application/json; charset=utf-8")
@app.post("/generate", response_model=GenerateResp)
def generate(req: Optional[GenerateReq] = Body(default=None)):
if req is None:
req = GenerateReq()
lang_code, pretty_lang, country = normalize_lang_and_country(req.lang, req.country)
if req.topic and (req.mode is None or req.mode == "auto"):
mode = "topic"
else:
mode = req.mode or ("topic" if req.topic else "auto")
if mode == "topic" and req.topic:
topic = req.topic; used_source = "custom"
else:
topic = pick_auto_topic(req.trend_source, country)
used_source = req.trend_source if (req.trend_source == "pytrends" and HAS_PYTRENDS) else "seed"
titles, used_prompt = generate_titles(topic, req.count, req, lang_code, pretty_lang)
return JSONResponse({"topic": topic, "titles": titles, "used_prompt": used_prompt, "trend_source": used_source}, media_type="application/json; charset=utf-8")
@app.get("/generate/auto", response_model=GenerateResp)
def generate_auto(
count: int = Query(30, ge=1, le=80),
length: str = Query("medium"),
style: str = Query("meme-shock"),
emoji: int = Query(1, ge=0, le=2),
numberless: bool = Query(True),
trend_source: str = Query("pytrends"),
passes: int = Query(3, ge=1, le=6),
lang: str = Query("ko"),
country: Optional[str] = Query(None),
):
lang_code, pretty_lang, country_code = normalize_lang_and_country(lang, country)
class _Req: pass
req = _Req()
req.lang = pretty_lang; req.length = length; req.style = style; req.emoji = emoji
req.numberless = numberless; req.extra_hint = None; req.trend_source = trend_source; req.passes = passes
topic = pick_auto_topic(trend_source, country_code)
titles, used_prompt = generate_titles(topic, count, req, lang_code, pretty_lang)
used_source = trend_source if (trend_source == "pytrends" and HAS_PYTRENDS) else "seed"
return JSONResponse({"topic": topic, "titles": titles, "used_prompt": used_prompt, "trend_source": used_source}, media_type="application/json; charset=utf-8")
@app.post("/prompt/shorts", response_model=ShortsPromptResp)
def prompt_shorts(body: ShortsPromptReq):
lang_code, _, _ = normalize_lang_and_country(body.lang, None)
prompt = build_shorts_prompt(
topic=body.topic,
title=body.title,
lang_code=lang_code,
style=body.style,
tone=body.tone,
cuts=body.cuts,
duration_sec=body.duration_sec,
emoji=body.emoji
)
return JSONResponse({
"topic": body.topic,
"title": body.title,
"prompt": prompt,
"lang": lang_code,
"style": body.style,
"cuts": body.cuts,
"duration_sec": body.duration_sec
}, media_type="application/json; charset=utf-8")
|