File size: 1,565 Bytes
d7686da
971b0c1
0318e0a
d7686da
d7390e2
d7686da
0318e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7686da
0318e0a
ee0e783
971b0c1
d7390e2
0318e0a
c141be1
 
 
0318e0a
971b0c1
ee0e783
c141be1
93b00e7
c141be1
 
0318e0a
d7390e2
0318e0a
 
 
d7390e2
c141be1
 
 
0318e0a
 
 
bd347e1
c141be1
d7686da
 
 
52b4a35
 
a4c6574
0318e0a
d7686da
 
 
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
import gradio as gr
from transformers import pipeline
import random

generator = pipeline("text-generation", model="gpt2")

fallback_captions = [
    "✨ Crafted for timeless elegance",
    "✨ Where minimal meets luxury",
    "✨ Subtle shine, bold presence",
    "✨ Designed to stand out effortlessly",
    "✨ Elevate your everyday style",
    "✨ Luxury in every detail"
]

def clean_caption(text):
    # Remove weird characters and keep short
    text = text.strip().replace("\n", " ")
    words = text.split()
    return " ".join(words[:8])  # keep it short

def generate_caption(prompt):
    base_prompt = f"Elegant caption for {prompt}:"

    result = generator(
        base_prompt,
        max_length=40,
        num_return_sequences=5,
        temperature=0.9,
        top_k=50,
        top_p=0.95
    )

    captions = []

    for r in result:
        text = r["generated_text"].replace(base_prompt, "").strip()
        short = clean_caption(text)

        # filter junk
        if len(short) > 10 and short not in captions:
            captions.append("✨ " + short)

        if len(captions) == 3:
            break

    # fallback if model fails (very important)
    while len(captions) < 3:
        captions.append(random.choice(fallback_captions))

    return "\n".join(captions)

iface = gr.Interface(
    fn=generate_caption,
    inputs=gr.Textbox(placeholder="Enter product (e.g. gold earrings)"),
    outputs=gr.Textbox(label="Captions"),
    title="✨ AI Caption Generator",
    description="Generate clean captions"
)

iface.launch()