|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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"), |
|
|
} |
|
|
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 |
|
|
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])) |
|
|
|
|
|
|
|
|
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") |
|
|
|