Spaces:
Sleeping
Sleeping
File size: 2,121 Bytes
ccf39c6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import gradio as gr
from transformers import CLIPProcessor, CLIPModel
from PIL import Image
import torch
# Load a pre-trained CLIP model and processor from Hugging Face
model_name = "openai/clip-vit-base-patch32"
model = CLIPModel.from_pretrained(model_name)
processor = CLIPProcessor.from_pretrained(model_name)
# Load the image (Gradio handles this as part of the interface)
def process_image(image):
"""Prepares the image for the model and extracts features."""
inputs = processor(images=image, return_tensors="pt")
with torch.no_grad():
features = model.get_image_features(**inputs)
return features
# Generate a basic persona based on the features
def generate_persona(image):
"""Generates a persona based on the extracted features."""
features = process_image(image)
# Example: Generating hardcoded persona traits for now, could be improved with actual inference logic
persona = {
"Age": "25-35 years",
"Gender": "Likely Female",
"Interests": ["Fashion", "Modern Lifestyle", "Technology"],
"Income Level": "Medium to High",
"Psychographics": ["Trend-conscious", "Tech-savvy", "Health-aware"],
"Behavioral Traits": ["Likes social media", "Follows influencers", "Shops online frequently"]
}
return persona
# Function to format the persona output as a string
def format_persona(persona):
"""Formats the persona for display in Gradio."""
result = "\n".join([f"{key}: {value}" for key, value in persona.items()])
return result
# Gradio interface for image input and persona output
def persona_analysis(image):
"""Takes an image and returns the inferred persona."""
persona = generate_persona(image)
return format_persona(persona)
# Build the Gradio interface
iface = gr.Interface(
fn=persona_analysis,
inputs=gr.inputs.Image(type="pil"), # Accept image input in PIL format
outputs="text", # Display persona as text output
title="Marketing Persona Generator",
description="Upload an image to generate a marketing persona."
)
# Launch the Gradio interface
iface.launch()
|