auto-save 2026-05-26 10:00 (~2)
This commit is contained in:
87
api/main.py
87
api/main.py
@@ -5268,6 +5268,18 @@ class CreativeCopyResp(BaseModel):
|
||||
variants: list[CreativeCopyVariant]
|
||||
|
||||
|
||||
class PromptPolishReq(BaseModel):
|
||||
text: str
|
||||
system_prompt: str = ""
|
||||
mode: Literal["image", "video", "general"] = "image"
|
||||
target_language: Literal["en", "zh", "keep"] = "en"
|
||||
|
||||
|
||||
class PromptPolishResp(BaseModel):
|
||||
model: str
|
||||
text: str
|
||||
|
||||
|
||||
class ScriptRewriteSegmentReq(BaseModel):
|
||||
index: int
|
||||
start: float = 0.0
|
||||
@@ -5400,6 +5412,81 @@ def _parse_creative_copy_response(raw: str, req: CreativeCopyReq) -> CreativeCop
|
||||
return CreativeCopyResp(model=REWRITE_MODEL if LLM_API_KEY else "fallback", variants=variants)
|
||||
|
||||
|
||||
def _prompt_polish_fallback(req: PromptPolishReq) -> PromptPolishResp:
|
||||
text = req.text.strip()
|
||||
base = _ensure_english(text) if req.target_language == "en" else text
|
||||
base = re.sub(r"\s+", " ", base).strip()
|
||||
if req.mode == "video":
|
||||
polished = (
|
||||
f"{base}. Smooth camera movement, clear subject continuity, stable composition, "
|
||||
"natural motion, coherent lighting, no subtitles, no watermark."
|
||||
)
|
||||
elif req.mode == "general":
|
||||
polished = base
|
||||
else:
|
||||
polished = (
|
||||
f"{base}. Detailed visual prompt, clear main subject, coherent composition, "
|
||||
"natural lighting, refined color palette, high-quality details."
|
||||
)
|
||||
return PromptPolishResp(model="fallback", text=polished[:1800])
|
||||
|
||||
|
||||
@app.post("/prompt/polish", response_model=PromptPolishResp)
|
||||
def polish_prompt(req: PromptPolishReq) -> PromptPolishResp:
|
||||
text = req.text.strip()
|
||||
if not text:
|
||||
raise HTTPException(400, "text required")
|
||||
if not LLM_API_KEY:
|
||||
return _prompt_polish_fallback(req)
|
||||
|
||||
target_label = {
|
||||
"en": "English",
|
||||
"zh": "Simplified Chinese",
|
||||
"keep": "the same language as the input",
|
||||
}.get(req.target_language, "English")
|
||||
mode_hint = {
|
||||
"image": "an image-generation prompt",
|
||||
"video": "a video-generation prompt",
|
||||
"general": "a concise generation prompt",
|
||||
}.get(req.mode, "an image-generation prompt")
|
||||
user_system = req.system_prompt.strip()
|
||||
prompt = (
|
||||
f"Rewrite the user's input into {mode_hint} in {target_label}.\n"
|
||||
"Preserve the user's actual subject, brand, product, place, style, and intent.\n"
|
||||
"Do not add SKG, health-tech, massage products, TikTok ad framing, product sales language, hashtags, captions, or any brand/product not explicitly present in the input.\n"
|
||||
"Do not add medical, wellness, or advertising claims unless the user asked for them.\n"
|
||||
"Improve concrete visual details, composition, lighting, camera language, materials, mood, and quality.\n"
|
||||
"Return only the rewritten prompt. No markdown, labels, JSON, quotes, explanation, or alternatives.\n"
|
||||
)
|
||||
if req.mode == "video":
|
||||
prompt += (
|
||||
"For video, describe motion, timing, camera movement, continuity, and what changes over time. "
|
||||
"If people are not essential, prefer no recognizable faces or distant/turned-away background people to reduce video safety risk.\n"
|
||||
)
|
||||
if user_system:
|
||||
prompt += f"\nUser-selected polishing guidance:\n{user_system[:1000]}\n"
|
||||
prompt += f"\nInput:\n{text[:2500]}"
|
||||
|
||||
try:
|
||||
resp = llm().chat.completions.create(
|
||||
model=REWRITE_MODEL,
|
||||
messages=[
|
||||
{"role": "system", "content": "You are a neutral professional prompt editor. You preserve intent and never inject unrelated brands or products."},
|
||||
{"role": "user", "content": prompt},
|
||||
],
|
||||
temperature=0.45,
|
||||
max_tokens=900,
|
||||
)
|
||||
out = (resp.choices[0].message.content or "").strip()
|
||||
out = re.sub(r"^```(?:text)?\s*", "", out, flags=re.I).strip()
|
||||
out = re.sub(r"\s*```$", "", out).strip()
|
||||
out = re.sub(r'^[\'"「『]+|[\'"」』]+$', "", out).strip()
|
||||
return PromptPolishResp(model=REWRITE_MODEL, text=(out or _prompt_polish_fallback(req).text)[:1800])
|
||||
except Exception as e:
|
||||
print(f"[prompt polish fallback] {e}", flush=True)
|
||||
return _prompt_polish_fallback(req)
|
||||
|
||||
|
||||
@app.post("/translate")
|
||||
def translate_text(req: TranslateReq) -> dict:
|
||||
"""单条文本翻译(给生图自定义提取元素 zh→en 用)"""
|
||||
|
||||
Reference in New Issue
Block a user