File size: 2,466 Bytes
bf70abc 0a480c8 c91c212 c1fbddd 757b271 bf70abc 59895d9 f23c1ac c91c212 757b271 a3215c8 757b271 a3215c8 757b271 c91c212 757b271 |
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 60 |
import streamlit as st
import openai
import requests
from PIL import Image
from io import BytesIO
# Access the OpenAI API key from Hugging Face Spaces secrets
openai.api_key = st.secrets["OPENAI_API_KEY"]
st.title("Customer Avatar Generator")
# User inputs for avatar generation
business_type = st.text_input("Business Type", placeholder="e.g., Real Estate, Fitness")
target_demographics = st.text_input("Target Demographics", placeholder="e.g., Age range, gender, location")
interests = st.text_input("Interests", placeholder="e.g., Home improvement, health and wellness")
additional_details = st.text_input("Additional Details", placeholder="Any specific characteristics or preferences")
if st.button('Generate Avatar'):
# Construct the prompt for text generation
prompt_text = f"Create a detailed customer avatar for a business in the {business_type} industry. Target demographics: {target_demographics}. Interests: {interests}. Additional details: {additional_details}."
# Call the OpenAI API for text generation
try:
response_text = openai.ChatCompletion.create(
model="gpt-4", # Use GPT-4 model
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt_text}
]
)
avatar_description = response_text.choices[0].message['content']
except Exception as e:
avatar_description = f"Error in generating avatar description: {e}"
# Construct the prompt for image generation
prompt_image = f"A visual representation of a customer avatar for a {business_type} business. Demographics: {target_demographics}. Interests: {interests}."
# Call the DALL-E API for image generation
try:
response_image = openai.Image.create(
model="dall-e-3", # Specify DALL-E 3 model
prompt=prompt_image,
n=1,
size="1024x1024" # Set image dimensions
)
# Fetch the image from the URL
image_url = response_image['data'][0]['url']
image_response = requests.get(image_url)
image = Image.open(BytesIO(image_response.content))
except Exception as e:
image = None
st.error(f"Error in generating image: {e}")
# Display the results
st.write("**Avatar Description:**")
st.write(avatar_description)
if image:
st.image(image, caption='Generated Customer Avatar Image')
|