Spaces:
Sleeping
Sleeping
| # app.py | |
| import re | |
| import gradio as gr | |
| from PIL import Image | |
| from transformers import pipeline | |
| # Single multi-modal pipeline: BLIP2 + Flan-T5-XL | |
| pipe = pipeline( | |
| task="image-text-to-text", | |
| model="Salesforce/blip2-flan-t5-xl", | |
| tokenizer="Salesforce/blip2-flan-t5-xl", | |
| max_new_tokens=500, | |
| do_sample=True, | |
| temperature=1.0, | |
| top_k=50, | |
| top_p=0.9, | |
| ) | |
| # Hard-coded gallery URLs | |
| def get_recommendations(): | |
| return [ | |
| "https://i.imgur.com/InC88PP.jpeg", | |
| "https://i.imgur.com/7BHfv4T.png", | |
| "https://i.imgur.com/wp3Wzc4.jpeg", | |
| "https://i.imgur.com/5e2xOA4.jpeg", | |
| "https://i.imgur.com/txjRk98.jpeg", | |
| "https://i.imgur.com/rQ4AYl0.jpeg", | |
| "https://i.imgur.com/bDzwD04.jpeg", | |
| "https://i.imgur.com/fLMngXI.jpeg", | |
| "https://i.imgur.com/nYEJzxt.png", | |
| "https://i.imgur.com/Xj92Cjv.jpeg", | |
| ] | |
| def process(image: Image): | |
| prompt = ( | |
| "You are an expert ad critic. Given the image below, output exactly three sections:\n\n" | |
| "Category: <one concise label>\n\n" | |
| "Analysis: <exactly five sentences explaining what the ad communicates and its emotional impact>\n\n" | |
| "Suggestions:\n" | |
| "- <bullet 1>\n" | |
| "- <bullet 2>\n" | |
| "- <bullet 3>\n" | |
| "- <bullet 4>\n" | |
| "- <bullet 5>\n" | |
| ) | |
| # run the multi-modal pipeline | |
| result = pipe(image, prompt=prompt)[0]["generated_text"] | |
| # extract the three parts via regex | |
| cat = re.search(r"Category:(.*?)Analysis:", result, re.S) | |
| ana = re.search(r"Analysis:(.*?)Suggestions:", result, re.S) | |
| sug = re.search(r"Suggestions:(.*)", result, re.S) | |
| category = cat.group(1).strip() if cat else "" | |
| analysis = ana.group(1).strip() if ana else "" | |
| suggestions = sug.group(1).strip() if sug else "" | |
| # ensure exactly five bullets | |
| bullets = [line for line in suggestions.splitlines() if line.startswith("-")] | |
| if len(bullets) < 5: | |
| bullets += ["- (no suggestion)"] * (5 - len(bullets)) | |
| suggestions = "\n".join(bullets[:5]) | |
| return category, analysis, suggestions, get_recommendations() | |
| # build the Gradio interface | |
| with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo: | |
| gr.Markdown("## 📢 Smart Ad Analyzer") | |
| gr.Markdown( | |
| "Upload an image ad to see: an **Ad Category**, a **five-sentence Analysis**, " | |
| "**five bullet-point Suggestions**, and **Example Ads**." | |
| ) | |
| with gr.Row(): | |
| image_input = gr.Image(type="pil", label="Upload Ad Image") | |
| with gr.Column(): | |
| cat_out = gr.Textbox(label="Ad Category", interactive=False) | |
| ana_out = gr.Textbox(label="Ad Analysis", lines=5, interactive=False) | |
| sug_out = gr.Textbox(label="Improvement Suggestions", lines=5, interactive=False) | |
| btn = gr.Button("Analyze Ad", size="sm", variant="primary") | |
| gallery = gr.Gallery(label="Recommended Example Ads", show_label=True) | |
| btn.click( | |
| fn=process, | |
| inputs=[image_input], | |
| outputs=[cat_out, ana_out, sug_out, gallery] | |
| ) | |
| gr.Markdown("Made by Simon Thalmay") | |
| if __name__ == "__main__": | |
| demo.launch() | |