Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,50 +8,117 @@ model_name = "openai/clip-vit-base-patch32"
|
|
| 8 |
model = CLIPModel.from_pretrained(model_name)
|
| 9 |
processor = CLIPProcessor.from_pretrained(model_name)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
""
|
| 14 |
-
|
| 15 |
-
with torch.no_grad():
|
| 16 |
-
features = model.get_image_features(**inputs)
|
| 17 |
-
return features
|
| 18 |
-
|
| 19 |
-
# Generate a basic persona based on the features
|
| 20 |
-
def generate_persona(image):
|
| 21 |
-
"""Generates a persona based on the extracted features."""
|
| 22 |
-
features = process_image(image)
|
| 23 |
-
|
| 24 |
-
# Example: Generating hardcoded persona traits for now, could be improved with actual inference logic
|
| 25 |
-
persona = {
|
| 26 |
-
"Age": "25-35 years",
|
| 27 |
"Gender": "Likely Female",
|
| 28 |
-
"Interests": ["Fashion", "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
"Income Level": "Medium to High",
|
| 30 |
-
"Psychographics": ["
|
| 31 |
-
"Behavioral Traits": ["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
return
|
| 41 |
|
| 42 |
-
# Gradio interface
|
| 43 |
def persona_analysis(image):
|
| 44 |
-
"""
|
| 45 |
-
persona =
|
| 46 |
-
return
|
| 47 |
|
| 48 |
# Build the Gradio interface
|
| 49 |
iface = gr.Interface(
|
| 50 |
-
fn=persona_analysis,
|
| 51 |
inputs=gr.Image(type="pil"), # Accept image input in PIL format
|
| 52 |
outputs=gr.Textbox(), # Display persona as text output
|
| 53 |
title="Marketing Persona Generator",
|
| 54 |
-
description="Upload an image to generate a marketing persona."
|
| 55 |
)
|
| 56 |
|
| 57 |
# Launch the Gradio interface
|
|
|
|
| 8 |
model = CLIPModel.from_pretrained(model_name)
|
| 9 |
processor = CLIPProcessor.from_pretrained(model_name)
|
| 10 |
|
| 11 |
+
# Define possible personas mapped to interests
|
| 12 |
+
predefined_personas = {
|
| 13 |
+
"Fashion": {
|
| 14 |
+
"Age": "18-30 years",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
"Gender": "Likely Female",
|
| 16 |
+
"Interests": ["Fashion", "Trendy Clothing", "Social Media"],
|
| 17 |
+
"Income Level": "Medium",
|
| 18 |
+
"Psychographics": ["Fashion-conscious", "Socially active", "Trend-seeker"],
|
| 19 |
+
"Behavioral Traits": ["Follows fashion influencers", "Buys seasonal items", "Active on Instagram"]
|
| 20 |
+
},
|
| 21 |
+
"Technology": {
|
| 22 |
+
"Age": "20-40 years",
|
| 23 |
+
"Gender": "Likely Male",
|
| 24 |
+
"Interests": ["Technology", "Gadgets", "Software"],
|
| 25 |
+
"Income Level": "High",
|
| 26 |
+
"Psychographics": ["Tech-savvy", "Innovative", "Early adopter"],
|
| 27 |
+
"Behavioral Traits": ["Follows tech blogs", "Interested in product launches", "Buys online frequently"]
|
| 28 |
+
},
|
| 29 |
+
"Sports": {
|
| 30 |
+
"Age": "15-40 years",
|
| 31 |
+
"Gender": "Likely Male",
|
| 32 |
+
"Interests": ["Sports", "Fitness", "Outdoor Activities"],
|
| 33 |
+
"Income Level": "Medium",
|
| 34 |
+
"Psychographics": ["Active lifestyle", "Health-conscious", "Competitive"],
|
| 35 |
+
"Behavioral Traits": ["Watches sports events", "Goes to the gym", "Buys athletic gear"]
|
| 36 |
+
},
|
| 37 |
+
"Travel": {
|
| 38 |
+
"Age": "25-45 years",
|
| 39 |
+
"Gender": "Likely Female or Male",
|
| 40 |
+
"Interests": ["Travel", "Adventure", "Culture"],
|
| 41 |
"Income Level": "Medium to High",
|
| 42 |
+
"Psychographics": ["Experience-seeker", "Open-minded", "Curious"],
|
| 43 |
+
"Behavioral Traits": ["Follows travel blogs", "Likes outdoor activities", "Prefers unique experiences"]
|
| 44 |
+
},
|
| 45 |
+
"Luxury": {
|
| 46 |
+
"Age": "30-50 years",
|
| 47 |
+
"Gender": "Likely Female or Male",
|
| 48 |
+
"Interests": ["Luxury Goods", "High-end Fashion", "Fine Dining"],
|
| 49 |
+
"Income Level": "High",
|
| 50 |
+
"Psychographics": ["Status-conscious", "Exclusive-taste", "Prestige-oriented"],
|
| 51 |
+
"Behavioral Traits": ["Buys luxury brands", "Frequent traveler", "Invests in high-end products"]
|
| 52 |
+
},
|
| 53 |
+
"Fitness": {
|
| 54 |
+
"Age": "20-40 years",
|
| 55 |
+
"Gender": "Likely Female or Male",
|
| 56 |
+
"Interests": ["Fitness", "Healthy Living", "Nutrition"],
|
| 57 |
+
"Income Level": "Medium",
|
| 58 |
+
"Psychographics": ["Health-conscious", "Goal-oriented", "Disciplined"],
|
| 59 |
+
"Behavioral Traits": ["Goes to the gym regularly", "Buys fitness equipment", "Follows nutrition plans"]
|
| 60 |
+
},
|
| 61 |
+
"Education": {
|
| 62 |
+
"Age": "18-35 years",
|
| 63 |
+
"Gender": "Likely Female or Male",
|
| 64 |
+
"Interests": ["Education", "Learning", "Career Development"],
|
| 65 |
+
"Income Level": "Medium",
|
| 66 |
+
"Psychographics": ["Growth-oriented", "Knowledge-seeker", "Curious"],
|
| 67 |
+
"Behavioral Traits": ["Follows online courses", "Interested in higher education", "Learns new skills"]
|
| 68 |
+
},
|
| 69 |
+
"Entertainment": {
|
| 70 |
+
"Age": "15-35 years",
|
| 71 |
+
"Gender": "Likely Female or Male",
|
| 72 |
+
"Interests": ["Movies", "Music", "Video Games"],
|
| 73 |
+
"Income Level": "Medium",
|
| 74 |
+
"Psychographics": ["Fun-seeking", "Relaxed", "Entertainment-driven"],
|
| 75 |
+
"Behavioral Traits": ["Watches Netflix", "Plays video games", "Follows pop culture trends"]
|
| 76 |
}
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
# List of interest categories to compare against
|
| 80 |
+
interest_categories = list(predefined_personas.keys())
|
| 81 |
+
|
| 82 |
+
# Function to calculate similarity between the image and each interest category
|
| 83 |
+
def get_most_similar_persona(image):
|
| 84 |
+
"""Infers the most similar predefined persona for the given image."""
|
| 85 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 86 |
+
with torch.no_grad():
|
| 87 |
+
image_features = model.get_image_features(**inputs)
|
| 88 |
+
|
| 89 |
+
# Define descriptions of interest categories (texts)
|
| 90 |
+
category_descriptions = [
|
| 91 |
+
f"A photo that represents {category.lower()}." for category in interest_categories
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
# Tokenize the category descriptions and get text features
|
| 95 |
+
text_inputs = processor(text=category_descriptions, return_tensors="pt", padding=True)
|
| 96 |
+
text_features = model.get_text_features(**text_inputs)
|
| 97 |
|
| 98 |
+
# Calculate similarity between image and text features
|
| 99 |
+
image_features /= image_features.norm(dim=-1, keepdim=True)
|
| 100 |
+
text_features /= text_features.norm(dim=-1, keepdim=True)
|
| 101 |
+
similarities = torch.matmul(image_features, text_features.T)
|
| 102 |
|
| 103 |
+
# Find the most similar category
|
| 104 |
+
most_similar_idx = similarities.argmax().item()
|
| 105 |
+
most_similar_category = interest_categories[most_similar_idx]
|
| 106 |
+
|
| 107 |
+
return predefined_personas[most_similar_category]
|
| 108 |
|
| 109 |
+
# Gradio interface function
|
| 110 |
def persona_analysis(image):
|
| 111 |
+
"""Generates a marketing persona based on the image."""
|
| 112 |
+
persona = get_most_similar_persona(image)
|
| 113 |
+
return "\n".join([f"{key}: {value}" for key, value in persona.items()])
|
| 114 |
|
| 115 |
# Build the Gradio interface
|
| 116 |
iface = gr.Interface(
|
| 117 |
+
fn=persona_analysis,
|
| 118 |
inputs=gr.Image(type="pil"), # Accept image input in PIL format
|
| 119 |
outputs=gr.Textbox(), # Display persona as text output
|
| 120 |
title="Marketing Persona Generator",
|
| 121 |
+
description="Upload an image to generate a marketing persona based on the ad subject."
|
| 122 |
)
|
| 123 |
|
| 124 |
# Launch the Gradio interface
|