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()