Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,18 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from huggingface_hub import InferenceClient
|
| 4 |
from PIL import Image
|
| 5 |
-
import
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
|
|
|
| 8 |
client = InferenceClient(
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
provider="featherless-ai", # or "huggingface_hub"
|
| 12 |
)
|
| 13 |
|
| 14 |
def get_recommendations():
|
| 15 |
-
#
|
| 16 |
return [
|
| 17 |
"https://i.imgur.com/InC88PP.jpeg",
|
| 18 |
"https://i.imgur.com/7BHfv4T.png",
|
|
@@ -26,89 +26,74 @@ def get_recommendations():
|
|
| 26 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 27 |
]
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
# Upload PIL image to Hugging Face
|
| 31 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 32 |
-
image.save(tmp, format="PNG")
|
| 33 |
-
image_url = client.upload(tmp.name)
|
| 34 |
-
prompt = (
|
| 35 |
-
"You are an expert ad analyst. "
|
| 36 |
-
"Please give a short category for this ad, a detailed analysis of its message, visuals, and emotional impact in five sentences, "
|
| 37 |
-
"and five unique, actionable improvement suggestions (as bullet points), each addressing a different aspect (visuals, message, call-to-action, targeting, or layout). "
|
| 38 |
-
"Output should have clear sections: 'Category', 'Analysis', and 'Suggestions'."
|
| 39 |
-
)
|
| 40 |
-
messages = [
|
| 41 |
-
{
|
| 42 |
-
"role": "system",
|
| 43 |
-
"content": [{"type": "text", "text": "You are a helpful assistant."}]
|
| 44 |
-
},
|
| 45 |
-
{
|
| 46 |
-
"role": "user",
|
| 47 |
-
"content": [
|
| 48 |
-
{"type": "image_url", "image_url": {"url": image_url}},
|
| 49 |
-
{"type": "text", "text": prompt}
|
| 50 |
-
]
|
| 51 |
-
}
|
| 52 |
-
]
|
| 53 |
-
# API call to Gemma
|
| 54 |
-
result = client.chat.completions.create(
|
| 55 |
-
model="google/gemma-3-4b-it",
|
| 56 |
-
messages=messages,
|
| 57 |
-
max_tokens=500,
|
| 58 |
-
)
|
| 59 |
-
return result.choices[0].message["content"]
|
| 60 |
-
|
| 61 |
-
def process(image):
|
| 62 |
if image is None:
|
| 63 |
return "", "", "", get_recommendations()
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
# ---- Gradio UI ----
|
| 99 |
def main():
|
| 100 |
-
with gr.Blocks(title="Smart Ad Analyzer (Gemma-
|
| 101 |
gr.Markdown("## 📢 Smart Ad Analyzer (Gemma-3 Edition)")
|
| 102 |
gr.Markdown(
|
| 103 |
-
"
|
| 104 |
-
|
|
|
|
|
|
|
| 105 |
)
|
| 106 |
with gr.Row():
|
| 107 |
inp = gr.Image(type='pil', label='Upload Ad Image')
|
| 108 |
with gr.Column():
|
| 109 |
cat_out = gr.Textbox(label='📂 Ad Category', interactive=False)
|
| 110 |
-
ana_out = gr.Textbox(label='📊 Ad Analysis', lines=
|
| 111 |
-
sug_out = gr.Textbox(label='🚀 Improvement Suggestions', lines=
|
| 112 |
btn = gr.Button('Analyze Ad', variant='primary')
|
| 113 |
gallery = gr.Gallery(label='Example Ads')
|
| 114 |
btn.click(
|
|
@@ -116,7 +101,7 @@ def main():
|
|
| 116 |
inputs=[inp],
|
| 117 |
outputs=[cat_out, ana_out, sug_out, gallery],
|
| 118 |
)
|
| 119 |
-
gr.Markdown('Made by Simon Thalmay
|
| 120 |
return demo
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
import io
|
| 4 |
+
import os
|
| 5 |
+
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
+
# Authenticate (Space secrets or env variable)
|
| 8 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 9 |
client = InferenceClient(
|
| 10 |
+
repo_id="google/gemma-3-4b-it",
|
| 11 |
+
token=HF_TOKEN
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
def get_recommendations():
|
| 15 |
+
# Example ad image URLs for the gallery
|
| 16 |
return [
|
| 17 |
"https://i.imgur.com/InC88PP.jpeg",
|
| 18 |
"https://i.imgur.com/7BHfv4T.png",
|
|
|
|
| 26 |
"https://i.imgur.com/Xj92Cjv.jpeg",
|
| 27 |
]
|
| 28 |
|
| 29 |
+
def process(image: Image.Image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if image is None:
|
| 31 |
return "", "", "", get_recommendations()
|
| 32 |
+
try:
|
| 33 |
+
# Convert PIL image to bytes
|
| 34 |
+
buf = io.BytesIO()
|
| 35 |
+
image.save(buf, format="PNG")
|
| 36 |
+
image_bytes = buf.getvalue()
|
| 37 |
+
|
| 38 |
+
prompt = (
|
| 39 |
+
"You are an expert ad analyst. "
|
| 40 |
+
"Given the uploaded image, return these sections:\n\n"
|
| 41 |
+
"Category: (a one or two word category, e.g. 'Fitness', 'Food', 'Travel')\n"
|
| 42 |
+
"Analysis: Write exactly five sentences about the ad's message, visuals, and emotional impact.\n"
|
| 43 |
+
"Suggestions: List five actionable and unique improvements for this ad. "
|
| 44 |
+
"Each suggestion must be one sentence and start with '- '. Suggestions must address different aspects: visuals, messaging, call-to-action, targeting, or layout."
|
| 45 |
+
)
|
| 46 |
+
messages = [
|
| 47 |
+
{
|
| 48 |
+
"role": "system",
|
| 49 |
+
"content": [{"type": "text", "text": "You are a helpful assistant."}]
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"role": "user",
|
| 53 |
+
"content": [
|
| 54 |
+
{"type": "image", "image": image_bytes},
|
| 55 |
+
{"type": "text", "text": prompt}
|
| 56 |
+
]
|
| 57 |
+
}
|
| 58 |
+
]
|
| 59 |
+
# Model call
|
| 60 |
+
output = client.chat.completions.create(
|
| 61 |
+
model="google/gemma-3-4b-it",
|
| 62 |
+
messages=messages,
|
| 63 |
+
max_tokens=800
|
| 64 |
+
)
|
| 65 |
+
text = output.choices[0].message["content"]
|
| 66 |
+
|
| 67 |
+
# Simple parsing
|
| 68 |
+
cat, analysis, suggestions = "", "", ""
|
| 69 |
+
if "Category:" in text and "Analysis:" in text and "Suggestions:" in text:
|
| 70 |
+
cat = text.split("Category:")[1].split("Analysis:")[0].strip()
|
| 71 |
+
analysis = text.split("Analysis:")[1].split("Suggestions:")[0].strip()
|
| 72 |
+
suggestions = text.split("Suggestions:")[1].strip()
|
| 73 |
+
else:
|
| 74 |
+
# fallback: show all in analysis, nothing for cat/suggestions
|
| 75 |
+
analysis = text
|
| 76 |
+
|
| 77 |
+
return cat, analysis, suggestions, get_recommendations()
|
| 78 |
+
except Exception as e:
|
| 79 |
+
# For debugging
|
| 80 |
+
return "Error", "Error", "Error", ["Error"]
|
| 81 |
|
|
|
|
| 82 |
def main():
|
| 83 |
+
with gr.Blocks(title="Smart Ad Analyzer (Gemma-3 Edition)") as demo:
|
| 84 |
gr.Markdown("## 📢 Smart Ad Analyzer (Gemma-3 Edition)")
|
| 85 |
gr.Markdown(
|
| 86 |
+
"""
|
| 87 |
+
Upload your ad image below and instantly get expert feedback.<br>
|
| 88 |
+
Category, analysis, improvement suggestions—and example ads for inspiration.
|
| 89 |
+
"""
|
| 90 |
)
|
| 91 |
with gr.Row():
|
| 92 |
inp = gr.Image(type='pil', label='Upload Ad Image')
|
| 93 |
with gr.Column():
|
| 94 |
cat_out = gr.Textbox(label='📂 Ad Category', interactive=False)
|
| 95 |
+
ana_out = gr.Textbox(label='📊 Ad Analysis', lines=7, interactive=False)
|
| 96 |
+
sug_out = gr.Textbox(label='🚀 Improvement Suggestions', lines=8, interactive=False)
|
| 97 |
btn = gr.Button('Analyze Ad', variant='primary')
|
| 98 |
gallery = gr.Gallery(label='Example Ads')
|
| 99 |
btn.click(
|
|
|
|
| 101 |
inputs=[inp],
|
| 102 |
outputs=[cat_out, ana_out, sug_out, gallery],
|
| 103 |
)
|
| 104 |
+
gr.Markdown('Made by Simon Thalmay')
|
| 105 |
return demo
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|