Spaces:
Sleeping
Sleeping
Download app.py from Multiverse26/caption-generator: direct link, hf CLI and curl.
- Browser
- Download file 1.57 kB
-
https://huggingface.co/spaces/Multiverse26/caption-generator/resolve/main/app.py
- Command line
-
hf download hf://spaces/Multiverse26/caption-generator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Multiverse26/caption-generator/resolve/main/app.py
1.57 kB
| 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() |