bizvideoschool's picture
Update app.py
757b271
raw
history blame
2.47 kB
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')