personainsight / app.py
dindizz's picture
Create app.py
ccf39c6 verified
raw
history blame
2.12 kB
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()