Update app.py
Browse files❌ Versi lama:
"Buat 3 caption sekaligus" → model bingung, hasil asal-asalan
Ga ada contoh → model tebak-tebak gayanya
max_new_tokens=800 → terlalu panjang, makin ngawur di akhir
✅ Versi baru:
"Buat 1 caption" → fokus, kualitas naik drastis
Few-shot: kasih 2 contoh nyata per gaya → model tiru polanya
max_new_tokens=300 → dipotong sebelum mulai ngawur
temperature=0.7 (dari 0.85) → lebih konsisten
repetition_penalty=1.2 (dari 1.1) → kurangi pengulangan
Klik berkali-kali → setiap klik hasil beda
Riwayat caption → caption sebelumnya ga hilang
app.py
CHANGED
|
@@ -1,23 +1,16 @@
|
|
| 1 |
"""
|
| 2 |
-
✍️ CapGen AI — Caption Generator Indonesia
|
| 3 |
-
|
| 4 |
-
GA PERLU TOKEN. GA PERLU SETUP APAPUN.
|
| 5 |
-
Langsung jalan setelah Space selesai building.
|
| 6 |
"""
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
import torch
|
| 10 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 11 |
|
| 12 |
-
# ══════════════════════════════════════════════════════
|
| 13 |
-
# KONFIGURASI MODEL
|
| 14 |
-
# ══════════════════════════════════════════════════════
|
| 15 |
-
# ✅ GANTI dengan ini:
|
| 16 |
MODEL_NAME = "Qwen/Qwen2.5-3B-Instruct"
|
| 17 |
-
print(f"⏳ Loading model {MODEL_NAME}...")
|
| 18 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
| 19 |
|
| 20 |
-
|
|
|
|
| 21 |
model = AutoModelForCausalLM.from_pretrained(
|
| 22 |
MODEL_NAME,
|
| 23 |
torch_dtype=torch.bfloat16,
|
|
@@ -28,34 +21,151 @@ model.eval()
|
|
| 28 |
print("✅ Model siap!")
|
| 29 |
|
| 30 |
# ══════════════════════════════════════════════════════
|
| 31 |
-
#
|
| 32 |
# ══════════════════════════════════════════════════════
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
PLATFORMS = {
|
| 35 |
"Instagram": "Maks 2200 karakter. Boleh panjang, emoji bebas, hashtag penting.",
|
| 36 |
-
"TikTok": "Maks 150 karakter. SANGAT pendek, catchy
|
| 37 |
-
"Twitter/X": "Maks 280 karakter. Padat
|
| 38 |
-
"LinkedIn": "Maks 3000 karakter. Profesional tapi engaging
|
| 39 |
-
"WhatsApp Status": "Maks 100 karakter. Sangat singkat
|
| 40 |
}
|
| 41 |
|
| 42 |
STYLES = {
|
| 43 |
-
"Aesthetic": "Puitis, minimalis emoji, kata-kata indah
|
| 44 |
-
"Lucu": "
|
| 45 |
-
"Jualan": "Ada harga/CTA, urgency, benefit jelas
|
| 46 |
-
"Motivasi": "Inspiratif, powerful words, emoji api
|
| 47 |
-
"Romantis": "Manis, puitis,
|
| 48 |
-
"Gaul/Jaksel": "
|
| 49 |
-
"Santai": "Casual, kayak ngobrol sama temen
|
| 50 |
-
"Formal": "Bahasa baku, sopan
|
| 51 |
}
|
| 52 |
|
| 53 |
LANGUAGES = {
|
| 54 |
-
"Indonesia": "Bahasa Indonesia standar
|
| 55 |
-
"Indonesia Gaul": "
|
| 56 |
-
"Bahasa Jawa": "Bahasa Jawa (ngoko/krama
|
| 57 |
-
"Bahasa Sunda": "Bahasa Sunda
|
| 58 |
-
"English": "Bahasa Inggris
|
| 59 |
"Bilingual": "Campuran Indonesia dan English",
|
| 60 |
}
|
| 61 |
|
|
@@ -70,129 +180,83 @@ PRESETS = {
|
|
| 70 |
"📚 Belajar": "Foto meja belajar yang rapi, ada buku dan kopi, lampu belajar warm light, vibes produktif",
|
| 71 |
}
|
| 72 |
|
| 73 |
-
SYSTEM_PROMPT = """Kamu adalah CapGen AI, ahli pembuat caption media sosial untuk pengguna Indonesia.
|
| 74 |
-
|
| 75 |
-
ATURAN KETAT (WAJIB DIIKUTI):
|
| 76 |
-
1. HANYA keluarkan caption. TANPA kata "Berikut", "Tentu", "Baik", "Oke", atau penjelasan apapun.
|
| 77 |
-
2. Setiap caption dipisahkan EXACTLY dengan --- (tiga strip, tanpa spasi tambahan).
|
| 78 |
-
3. Setiap caption WAJIB sertakan 3-8 hashtag relevan di AKHIR caption.
|
| 79 |
-
4. Sesuaikan panjang dengan batasan platform yang dipilih.
|
| 80 |
-
5. Gaya bahasa HARUS konsisten sesuai pilihan user.
|
| 81 |
-
6. Jangan beri nomor urut (1., 2., 3. dst).
|
| 82 |
-
7. Jangan pakai tanda kutip atau formatting markdown di caption."""
|
| 83 |
-
|
| 84 |
-
|
| 85 |
# ══════════════════════════════════════════════════════
|
| 86 |
-
# LOGIC
|
| 87 |
# ══════════════════════════════════════════════════════
|
| 88 |
|
| 89 |
-
def generate_text(messages
|
| 90 |
-
"""Generate teks pakai model lokal"""
|
| 91 |
text = tokenizer.apply_chat_template(
|
| 92 |
messages, tokenize=False, add_generation_prompt=True
|
| 93 |
)
|
| 94 |
inputs = tokenizer(text, return_tensors="pt")
|
| 95 |
-
|
| 96 |
with torch.no_grad():
|
| 97 |
outputs = model.generate(
|
| 98 |
**inputs,
|
| 99 |
-
max_new_tokens=
|
| 100 |
-
temperature=
|
| 101 |
-
top_p=
|
|
|
|
| 102 |
do_sample=True,
|
| 103 |
-
repetition_penalty=1.
|
| 104 |
)
|
| 105 |
-
|
| 106 |
-
# Ambil hanya bagian yang di-generate (bukan prompt)
|
| 107 |
new_tokens = outputs[0][inputs["input_ids"].shape[-1]:]
|
| 108 |
-
return tokenizer.decode(new_tokens, skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
-
def
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
"
|
| 116 |
-
"
|
| 117 |
-
"Berikut caption
|
| 118 |
-
"
|
| 119 |
]
|
| 120 |
-
for
|
| 121 |
-
if text.strip().
|
| 122 |
-
|
| 123 |
-
if
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
if 0 < dot_idx < 100:
|
| 127 |
-
text = text[dot_idx + 1:].strip()
|
| 128 |
-
break
|
| 129 |
-
return text
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
def parse_captions(raw_text, jumlah):
|
| 133 |
-
text = clean_text(raw_text)
|
| 134 |
-
parts = [p.strip() for p in text.split("---") if p.strip()]
|
| 135 |
-
|
| 136 |
-
captions = []
|
| 137 |
-
for p in parts:
|
| 138 |
-
p = p.strip()
|
| 139 |
-
if len(p) > 2 and p[0].isdigit() and p[1] in ".)":
|
| 140 |
-
p = p[2:].strip()
|
| 141 |
-
if len(p) > 15:
|
| 142 |
-
captions.append(p)
|
| 143 |
-
|
| 144 |
-
if not captions and len(text) > 15:
|
| 145 |
-
captions = [text]
|
| 146 |
-
|
| 147 |
-
while len(captions) < jumlah:
|
| 148 |
-
captions.append("")
|
| 149 |
-
return captions[:jumlah]
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
def generate_caption(konteks, platform, gaya, bahasa, jumlah):
|
| 153 |
-
if not konteks or not konteks.strip():
|
| 154 |
-
vals = ["⚠️ Tulis dulu deskripsi foto/konten kamu di kolom atas!"] + [""] * 4
|
| 155 |
-
return tuple(gr.update(value=v, visible=True) for v in vals)
|
| 156 |
|
| 157 |
-
user_msg = f"""PLATFORM: {platform}
|
| 158 |
-
{PLATFORMS[platform]}
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
BAHASA: {bahasa}
|
| 164 |
-
{LANGUAGES[bahasa]}
|
| 165 |
-
|
| 166 |
-
KONTEKS FOTO/KONTEN:
|
| 167 |
-
{konteks.strip()}
|
| 168 |
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
messages = [
|
| 172 |
-
{"role": "system", "content":
|
| 173 |
{"role": "user", "content": user_msg}
|
| 174 |
]
|
| 175 |
|
| 176 |
try:
|
| 177 |
result = generate_text(messages)
|
| 178 |
except Exception as e:
|
| 179 |
-
|
| 180 |
-
return tuple(gr.update(value=v, visible=True) for v in vals)
|
| 181 |
-
|
| 182 |
-
captions = parse_captions(result, jumlah)
|
| 183 |
|
| 184 |
-
|
| 185 |
-
for i in range(5):
|
| 186 |
-
if i < jumlah:
|
| 187 |
-
output.append(gr.update(value=captions[i], visible=True))
|
| 188 |
-
else:
|
| 189 |
-
output.append(gr.update(value="", visible=False))
|
| 190 |
-
|
| 191 |
-
return tuple(output)
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
def update_visibility(jumlah):
|
| 195 |
-
return tuple(gr.update(visible=(i < jumlah)) for i in range(5))
|
| 196 |
|
| 197 |
|
| 198 |
def fill_preset(name):
|
|
@@ -200,7 +264,7 @@ def fill_preset(name):
|
|
| 200 |
|
| 201 |
|
| 202 |
# ══════════════════════════════════════════════════════
|
| 203 |
-
# UI
|
| 204 |
# ══════════════════════════════════════════════════════
|
| 205 |
|
| 206 |
CUSTOM_CSS = """
|
|
@@ -210,10 +274,11 @@ CUSTOM_CSS = """
|
|
| 210 |
padding: 14px 32px !important;
|
| 211 |
border-radius: 12px !important;
|
| 212 |
}
|
| 213 |
-
.
|
| 214 |
-
font-size:
|
| 215 |
-
line-height: 1.
|
| 216 |
border-radius: 10px !important;
|
|
|
|
| 217 |
}
|
| 218 |
.preset-row button {
|
| 219 |
min-width: fit-content !important;
|
|
@@ -225,6 +290,11 @@ CUSTOM_CSS = """
|
|
| 225 |
font-size: 12px;
|
| 226 |
margin-top: 16px;
|
| 227 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
"""
|
| 229 |
|
| 230 |
with gr.Blocks() as demo:
|
|
@@ -285,11 +355,6 @@ with gr.Blocks() as demo:
|
|
| 285 |
value="Indonesia",
|
| 286 |
label="🌐 Bahasa",
|
| 287 |
)
|
| 288 |
-
jumlah = gr.Dropdown(
|
| 289 |
-
choices=[1, 2, 3, 5],
|
| 290 |
-
value=3,
|
| 291 |
-
label="🔢 Jumlah",
|
| 292 |
-
)
|
| 293 |
|
| 294 |
btn_generate = gr.Button(
|
| 295 |
"✨ Generate Caption",
|
|
@@ -297,45 +362,59 @@ with gr.Blocks() as demo:
|
|
| 297 |
size="lg",
|
| 298 |
elem_id="btn-generate",
|
| 299 |
)
|
| 300 |
-
|
| 301 |
-
with gr.Column(scale=1):
|
| 302 |
-
gr.Markdown("### 📋 Hasil Caption")
|
| 303 |
gr.Markdown(
|
| 304 |
-
'<
|
| 305 |
-
'Blok teks caption lalu Ctrl+C / Cmd+C untuk copy</span>'
|
| 306 |
)
|
| 307 |
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
for i in range(5):
|
| 311 |
-
with gr.Group(visible=(i < 3)) as group:
|
| 312 |
-
box = gr.Textbox(
|
| 313 |
-
label=f"Caption {i + 1}",
|
| 314 |
-
lines=4,
|
| 315 |
-
max_lines=8,
|
| 316 |
-
interactive=False,
|
| 317 |
-
elem_classes=["caption-output"],
|
| 318 |
-
)
|
| 319 |
-
caption_boxes.append(box)
|
| 320 |
-
caption_groups.append(group)
|
| 321 |
-
|
| 322 |
-
jumlah.change(
|
| 323 |
-
fn=update_visibility,
|
| 324 |
-
inputs=[jumlah],
|
| 325 |
-
outputs=caption_groups,
|
| 326 |
-
)
|
| 327 |
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
)
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
outputs=caption_groups,
|
| 337 |
)
|
| 338 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
gr.Markdown(
|
| 340 |
'<div class="footer-text">'
|
| 341 |
"Dibuat dengan ❤️ untuk komunitas Indonesia · "
|
|
|
|
| 1 |
"""
|
| 2 |
+
✍️ CapGen AI — Caption Generator Indonesia (v2)
|
| 3 |
+
Versi improved: few-shot prompting + generate 1 per 1
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import torch
|
| 8 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
MODEL_NAME = "Qwen/Qwen2.5-3B-Instruct"
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
print(f"⏳ Loading {MODEL_NAME}...")
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
MODEL_NAME,
|
| 16 |
torch_dtype=torch.bfloat16,
|
|
|
|
| 21 |
print("✅ Model siap!")
|
| 22 |
|
| 23 |
# ══════════════════════════════════════════════════════
|
| 24 |
+
# CONTOH CAPTION PER GAYA (ini kunci kualitasnya)
|
| 25 |
# ══════════════════════════════════════════════════════
|
| 26 |
|
| 27 |
+
CONTOH = {
|
| 28 |
+
"Aesthetic": {
|
| 29 |
+
"Instagram": (
|
| 30 |
+
"Contoh caption bagus:\n"
|
| 31 |
+
'"Di antara decitan kerupuk dan aroma rempah, hidup rasanya lengkap 🌾✨\n'
|
| 32 |
+
'#warungcita #hidupsimple #nungguinmatahari"\n'
|
| 33 |
+
'"Senja kali ini bilang, kamu boleh lelah tapi jangan berhenti 🌅\n'
|
| 34 |
+
'#senja #istirahatsejenak #catatanpetang"'
|
| 35 |
+
),
|
| 36 |
+
"TikTok": (
|
| 37 |
+
"Contoh caption bagus:\n"
|
| 38 |
+
'"Senja ngomong kamu cantik hari ini 🧡"\n'
|
| 39 |
+
'"Yang penting bahagia, sisanya urusan nanti 🌿"'
|
| 40 |
+
),
|
| 41 |
+
"default": (
|
| 42 |
+
"Contoh caption bagus:\n"
|
| 43 |
+
'"Hal kecil yang bikin senyum tanpa alasan 🌿✨\n'
|
| 44 |
+
'#simplejoy #catatanhariini"\n'
|
| 45 |
+
'"Beberapa hal indah datang tanpa direncanakan 🌙\n'
|
| 46 |
+
'#unexpected #grateful"'
|
| 47 |
+
),
|
| 48 |
+
},
|
| 49 |
+
"Lucu": {
|
| 50 |
+
"Instagram": (
|
| 51 |
+
"Contoh caption bagus:\n"
|
| 52 |
+
'"Diet mulai Senin... Senin apa ya yang mana 🤔\n'
|
| 53 |
+
'#dietgagal #makanyangpenting #realita"\n'
|
| 54 |
+
'"Hidupku kayak WiFi kantoran, kadang nyambung kadang nggak 📶😅\n'
|
| 55 |
+
'#hidupini #relatable #galau'"
|
| 56 |
+
),
|
| 57 |
+
"default": (
|
| 58 |
+
"Contoh caption bagus:\n"
|
| 59 |
+
'"Udah usaha maksimal, hasilnya tetap minimal 🤡"\n'
|
| 60 |
+
'"Mau kaya tapi males, gimana tuh 😭"'
|
| 61 |
+
),
|
| 62 |
+
},
|
| 63 |
+
"Jualan": {
|
| 64 |
+
"Instagram": (
|
| 65 |
+
"Contoh caption bagus:\n"
|
| 66 |
+
'"PROMO TERBATAS! 🔥 Serum glow yang biasanya 150rb, sekarang CUMA 89rb!\n'
|
| 67 |
+
'Stok terbatas, siapa cepat dia dapat 💕\n'
|
| 68 |
+
'Order via DM / klik link di bio!\n'
|
| 69 |
+
'#skincare #serumglow #promoskincare #diskon"\n'
|
| 70 |
+
'"Best seller kami kembali hadir! ✨ Celana linin premium yang nyaman seharian.\n'
|
| 71 |
+
'Harga normal 199rb → NOW 129rb aja!\n'
|
| 72 |
+
'Tersisa 30 pcs saja, buruan!\n'
|
| 73 |
+
'#celanalinen #fashionpria #diskon"'
|
| 74 |
+
),
|
| 75 |
+
"TikTok": (
|
| 76 |
+
"Contoh caption bagus:\n"
|
| 77 |
+
'"Harga 89rb aja buat kulit glowing! Link di bio 🔗🔥"'
|
| 78 |
+
),
|
| 79 |
+
"default": (
|
| 80 |
+
"Contoh caption bagus:\n"
|
| 81 |
+
'"Promo hari ini! Dari 150rb jadi 89rb aja 🔥\n'
|
| 82 |
+
'Order langsung via DM ya!\n'
|
| 83 |
+
'#promohariini #diskon"'
|
| 84 |
+
),
|
| 85 |
+
},
|
| 86 |
+
"Motivasi": {
|
| 87 |
+
"default": (
|
| 88 |
+
"Contoh caption bagus:\n"
|
| 89 |
+
'"Proses itu berat, tapi hasilnya buat kamu bangun bangun pagi 🔥\n'
|
| 90 |
+
'#motivasipagi #hustle #nevergiveup"\n'
|
| 91 |
+
'"Kamu bukan lambat, kamu lagi build fondasi yang kuat 💪\n'
|
| 92 |
+
'#proses #trusttheprocess #growth"'
|
| 93 |
+
),
|
| 94 |
+
},
|
| 95 |
+
"Romantis": {
|
| 96 |
+
"default": (
|
| 97 |
+
"Contoh caption bagus:\n"
|
| 98 |
+
'"Kamu tuh kayak kopi pagi, tanpa kamu seharian rasanya hambar ☕💕\n'
|
| 99 |
+
'#pacarsayang #romantis #cintalokal"\n'
|
| 100 |
+
'"Senyum kamu itu autofokus, yang lain jadi blur semua 📸💗\n'
|
| 101 |
+
'#jodoh #romance #lovestory"'
|
| 102 |
+
),
|
| 103 |
+
},
|
| 104 |
+
"Gaul/Jaksel": {
|
| 105 |
+
"default": (
|
| 106 |
+
"Contoh caption bagus:\n'
|
| 107 |
+
'"Literally this is the best day ever bangeet bestie 🫶✨\n'
|
| 108 |
+
'#bestie #jaksel #vibeskekinian"\n'
|
| 109 |
+
'"Makanannya literally bikin crying aaah parah banget sih 🍜😭\n'
|
| 110 |
+
'#foodie #makanenak #gaul"'
|
| 111 |
+
),
|
| 112 |
+
},
|
| 113 |
+
"Santai": {
|
| 114 |
+
"default": (
|
| 115 |
+
"Contoh caption bagus:\n"
|
| 116 |
+
'"Hari ini ngga ada rencana spesial, tapi ngga apa-apa kok 🌿\n'
|
| 117 |
+
'#chill #santai #flow"\n'
|
| 118 |
+
'"Nongkrong, ngopi, ngobrol ga jelas. Tapi bahagia ☕😊\n'
|
| 119 |
+
'#weekendvibes #hangout #qualitytime"'
|
| 120 |
+
),
|
| 121 |
+
},
|
| 122 |
+
"Formal": {
|
| 123 |
+
"default": (
|
| 124 |
+
"Contoh caption bagus:\n"
|
| 125 |
+
'"Kami mengucapkan terima kasih atas kehadiran Bapak/Ibu dalam acara seminar nasional ini.\n'
|
| 126 |
+
'Semoga ilmu yang diperoleh dapat bermanfaat.\n'
|
| 127 |
+
'#seminar #profesional #diknas"\n'
|
| 128 |
+
'"Peluncuran program terbaru kami telah berlangsung dengan baik.\n'
|
| 129 |
+
'Kami berharap program ini dapat memberikan dampak positif bagi masyarakat.\n'
|
| 130 |
+
'#programbaru #layananpublik"'
|
| 131 |
+
),
|
| 132 |
+
},
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
# Fallback kalau gaya/platform ga ada di contoh
|
| 136 |
+
def get_contoh(gaya, platform):
|
| 137 |
+
if gaya in CONTOH:
|
| 138 |
+
if platform in CONTOH[gaya]:
|
| 139 |
+
return CONTOH[gaya][platform]
|
| 140 |
+
return CONTOH[gaya]["default"]
|
| 141 |
+
return CONTOH["Aesthetic"]["default"]
|
| 142 |
+
|
| 143 |
+
|
| 144 |
PLATFORMS = {
|
| 145 |
"Instagram": "Maks 2200 karakter. Boleh panjang, emoji bebas, hashtag penting.",
|
| 146 |
+
"TikTok": "Maks 150 karakter. SANGAT pendek, catchy.",
|
| 147 |
+
"Twitter/X": "Maks 280 karakter. Padat dan tajam.",
|
| 148 |
+
"LinkedIn": "Maks 3000 karakter. Profesional tapi engaging.",
|
| 149 |
+
"WhatsApp Status": "Maks 100 karakter. Sangat singkat.",
|
| 150 |
}
|
| 151 |
|
| 152 |
STYLES = {
|
| 153 |
+
"Aesthetic": "Puitis, minimalis emoji, kata-kata indah",
|
| 154 |
+
"Lucu": "Punchline di akhir, bahasa slang, relatable",
|
| 155 |
+
"Jualan": "Ada harga/CTA, urgency, benefit jelas",
|
| 156 |
+
"Motivasi": "Inspiratif, powerful words, emoji api",
|
| 157 |
+
"Romantis": "Manis, puitis, emoji hati",
|
| 158 |
+
"Gaul/Jaksel": "Slang Jakarta: literally, bestie, bangeet",
|
| 159 |
+
"Santai": "Casual, kayak ngobrol sama temen",
|
| 160 |
+
"Formal": "Bahasa baku, sopan",
|
| 161 |
}
|
| 162 |
|
| 163 |
LANGUAGES = {
|
| 164 |
+
"Indonesia": "Bahasa Indonesia standar",
|
| 165 |
+
"Indonesia Gaul": "Slang gaul kekinian",
|
| 166 |
+
"Bahasa Jawa": "Bahasa Jawa (ngoko/krama)",
|
| 167 |
+
"Bahasa Sunda": "Bahasa Sunda gaul",
|
| 168 |
+
"English": "Bahasa Inggris natural",
|
| 169 |
"Bilingual": "Campuran Indonesia dan English",
|
| 170 |
}
|
| 171 |
|
|
|
|
| 180 |
"📚 Belajar": "Foto meja belajar yang rapi, ada buku dan kopi, lampu belajar warm light, vibes produktif",
|
| 181 |
}
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
# ══════════════════════════════════════════════════════
|
| 184 |
+
# LOGIC
|
| 185 |
# ══════════════════════════════════════════════════════
|
| 186 |
|
| 187 |
+
def generate_text(messages):
|
|
|
|
| 188 |
text = tokenizer.apply_chat_template(
|
| 189 |
messages, tokenize=False, add_generation_prompt=True
|
| 190 |
)
|
| 191 |
inputs = tokenizer(text, return_tensors="pt")
|
|
|
|
| 192 |
with torch.no_grad():
|
| 193 |
outputs = model.generate(
|
| 194 |
**inputs,
|
| 195 |
+
max_new_tokens=300,
|
| 196 |
+
temperature=0.7,
|
| 197 |
+
top_p=0.85,
|
| 198 |
+
top_k=50,
|
| 199 |
do_sample=True,
|
| 200 |
+
repetition_penalty=1.2,
|
| 201 |
)
|
|
|
|
|
|
|
| 202 |
new_tokens = outputs[0][inputs["input_ids"].shape[-1]:]
|
| 203 |
+
return tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
def build_prompt(konteks, platform, gaya, bahasa):
|
| 207 |
+
contoh = get_contoh(gaya, platform)
|
| 208 |
+
return (
|
| 209 |
+
f"Buat SATU caption untuk:\n\n"
|
| 210 |
+
f"Platform: {platform} ({PLATFORMS[platform]})\n"
|
| 211 |
+
f"Gaya: {gaya} ({STYLES[gaya]})\n"
|
| 212 |
+
f"Bahasa: {bahasa} ({LANGUAGES[bahasa]})\n"
|
| 213 |
+
f"Konteks: {konteks.strip()}\n\n"
|
| 214 |
+
f"{contoh}\n\n"
|
| 215 |
+
f"Sekarang buat SATU caption baru yang BERBEDA dari contoh di atas, "
|
| 216 |
+
f"untuk konteks yang saya berikan. Langsung tulis caption-nya saja, "
|
| 217 |
+
f"sertakan 3-6 hashtag di akhir."
|
| 218 |
+
)
|
| 219 |
|
| 220 |
|
| 221 |
+
def clean_output(text):
|
| 222 |
+
"""Bersihkan output dari kalimat pembuka yang tidak perlu"""
|
| 223 |
+
lines_to_remove = [
|
| 224 |
+
"Baik, berikut captionnya:", "Berikut captionnya:",
|
| 225 |
+
"Berikut:", "Tentu, berikut:", "Oke, berikut:",
|
| 226 |
+
"Baik,", "Tentu,", "Oke,", "Here is", "Sure, here",
|
| 227 |
+
"Berikut adalah caption yang kamu minta:",
|
| 228 |
+
"Ini captionnya:", "Nih captionnya:",
|
| 229 |
]
|
| 230 |
+
for line in lines_to_remove:
|
| 231 |
+
if text.strip().startswith(line):
|
| 232 |
+
text = text.strip()[len(line):].strip()
|
| 233 |
+
if text.startswith(":"):
|
| 234 |
+
text = text[1:].strip()
|
| 235 |
+
return text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
|
|
|
|
|
|
| 237 |
|
| 238 |
+
def generate_caption(konteks, platform, gaya, bahasa):
|
| 239 |
+
if not konteks or not konteks.strip():
|
| 240 |
+
return "⚠️ Tulis dulu deskripsi foto/konten kamu!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
+
system_msg = (
|
| 243 |
+
"Kamu adalah penulis caption media sosial profesional bahasa Indonesia. "
|
| 244 |
+
"Kamu HANYA mengeluarkan caption. Tidak ada penjelasan, tidak ada pembuka, "
|
| 245 |
+
"tidak ada kata 'Berikut'. Langsung tulis caption saja."
|
| 246 |
+
)
|
| 247 |
+
user_msg = build_prompt(konteks, platform, gaya, bahasa)
|
| 248 |
|
| 249 |
messages = [
|
| 250 |
+
{"role": "system", "content": system_msg},
|
| 251 |
{"role": "user", "content": user_msg}
|
| 252 |
]
|
| 253 |
|
| 254 |
try:
|
| 255 |
result = generate_text(messages)
|
| 256 |
except Exception as e:
|
| 257 |
+
return f"❌ Error: {str(e)[:200]}"
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
+
return clean_output(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
|
| 262 |
def fill_preset(name):
|
|
|
|
| 264 |
|
| 265 |
|
| 266 |
# ══════════════════════════════════════════════════════
|
| 267 |
+
# UI — DIUBAH: Generate 1 caption, bisa klik berkali-kali
|
| 268 |
# ══════════════════════════════════════════════════════
|
| 269 |
|
| 270 |
CUSTOM_CSS = """
|
|
|
|
| 274 |
padding: 14px 32px !important;
|
| 275 |
border-radius: 12px !important;
|
| 276 |
}
|
| 277 |
+
.output-box textarea {
|
| 278 |
+
font-size: 16px !important;
|
| 279 |
+
line-height: 1.8 !important;
|
| 280 |
border-radius: 10px !important;
|
| 281 |
+
min-height: 120px !important;
|
| 282 |
}
|
| 283 |
.preset-row button {
|
| 284 |
min-width: fit-content !important;
|
|
|
|
| 290 |
font-size: 12px;
|
| 291 |
margin-top: 16px;
|
| 292 |
}
|
| 293 |
+
.hint-text {
|
| 294 |
+
font-size: 13px;
|
| 295 |
+
color: #888;
|
| 296 |
+
margin-top: 4px;
|
| 297 |
+
}
|
| 298 |
"""
|
| 299 |
|
| 300 |
with gr.Blocks() as demo:
|
|
|
|
| 355 |
value="Indonesia",
|
| 356 |
label="🌐 Bahasa",
|
| 357 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
btn_generate = gr.Button(
|
| 360 |
"✨ Generate Caption",
|
|
|
|
| 362 |
size="lg",
|
| 363 |
elem_id="btn-generate",
|
| 364 |
)
|
|
|
|
|
|
|
|
|
|
| 365 |
gr.Markdown(
|
| 366 |
+
'<p class="hint-text">💡 Klik berkali-kali untuk hasil berbeda-beda</p>'
|
|
|
|
| 367 |
)
|
| 368 |
|
| 369 |
+
with gr.Column(scale=1):
|
| 370 |
+
gr.Markdown("### 📋 Caption Kamu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
|
| 372 |
+
output_caption = gr.Textbox(
|
| 373 |
+
label="",
|
| 374 |
+
lines=5,
|
| 375 |
+
max_lines=10,
|
| 376 |
+
interactive=False,
|
| 377 |
+
show_label=False,
|
| 378 |
+
placeholder="Hasil caption akan muncul di sini...",
|
| 379 |
+
elem_classes=["output-box"],
|
| 380 |
)
|
| 381 |
+
|
| 382 |
+
gr.Markdown(
|
| 383 |
+
'<p class="hint-text">Blok teks di atas lalu Ctrl+C / Cmd+C untuk copy</p>'
|
|
|
|
| 384 |
)
|
| 385 |
|
| 386 |
+
with gr.Accordion("📜 Riwayat Caption", open=False):
|
| 387 |
+
history = gr.Textbox(
|
| 388 |
+
label="",
|
| 389 |
+
lines=12,
|
| 390 |
+
interactive=False,
|
| 391 |
+
show_label=False,
|
| 392 |
+
placeholder="Caption-caption sebelumnya akan muncul di sini...",
|
| 393 |
+
)
|
| 394 |
+
|
| 395 |
+
# State untuk riwayat
|
| 396 |
+
history_state = gr.State("")
|
| 397 |
+
|
| 398 |
+
def generate_with_history(konteks, platform, gaya, bahasa, prev_history):
|
| 399 |
+
caption = generate_caption(konteks, platform, gaya, bahasa)
|
| 400 |
+
|
| 401 |
+
if not caption.startswith("⚠️") and not caption.startswith("❌"):
|
| 402 |
+
separator = "\n" + "─" * 40 + "\n"
|
| 403 |
+
new_history = f"[{platform} · {gaya} · {bahasa}]\n{caption}{separator}{prev_history}"
|
| 404 |
+
return caption, new_history
|
| 405 |
+
return caption, prev_history
|
| 406 |
+
|
| 407 |
+
btn_generate.click(
|
| 408 |
+
fn=generate_with_history,
|
| 409 |
+
inputs=[konteks, platform, gaya, bahasa, history_state],
|
| 410 |
+
outputs=[output_caption, history_state],
|
| 411 |
+
)
|
| 412 |
+
btn_generate.click(
|
| 413 |
+
fn=lambda h: h,
|
| 414 |
+
inputs=[history_state],
|
| 415 |
+
outputs=[history],
|
| 416 |
+
)
|
| 417 |
+
|
| 418 |
gr.Markdown(
|
| 419 |
'<div class="footer-text">'
|
| 420 |
"Dibuat dengan ❤️ untuk komunitas Indonesia · "
|