Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import CLIPProcessor, CLIPModel
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load a pre-trained CLIP model and processor from Hugging Face
|
7 |
+
model_name = "openai/clip-vit-base-patch32"
|
8 |
+
model = CLIPModel.from_pretrained(model_name)
|
9 |
+
processor = CLIPProcessor.from_pretrained(model_name)
|
10 |
+
|
11 |
+
# Load the image (Gradio handles this as part of the interface)
|
12 |
+
def process_image(image):
|
13 |
+
"""Prepares the image for the model and extracts features."""
|
14 |
+
inputs = processor(images=image, return_tensors="pt")
|
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", "Modern Lifestyle", "Technology"],
|
29 |
+
"Income Level": "Medium to High",
|
30 |
+
"Psychographics": ["Trend-conscious", "Tech-savvy", "Health-aware"],
|
31 |
+
"Behavioral Traits": ["Likes social media", "Follows influencers", "Shops online frequently"]
|
32 |
+
}
|
33 |
+
|
34 |
+
return persona
|
35 |
+
|
36 |
+
# Function to format the persona output as a string
|
37 |
+
def format_persona(persona):
|
38 |
+
"""Formats the persona for display in Gradio."""
|
39 |
+
result = "\n".join([f"{key}: {value}" for key, value in persona.items()])
|
40 |
+
return result
|
41 |
+
|
42 |
+
# Gradio interface for image input and persona output
|
43 |
+
def persona_analysis(image):
|
44 |
+
"""Takes an image and returns the inferred persona."""
|
45 |
+
persona = generate_persona(image)
|
46 |
+
return format_persona(persona)
|
47 |
+
|
48 |
+
# Build the Gradio interface
|
49 |
+
iface = gr.Interface(
|
50 |
+
fn=persona_analysis,
|
51 |
+
inputs=gr.inputs.Image(type="pil"), # Accept image input in PIL format
|
52 |
+
outputs="text", # 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
|
58 |
+
iface.launch()
|