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("Intuitive Customer Avatar & Content Pillar Generator") # User inputs for avatar generation st.subheader("Tell Us About Your Business") business_type = st.text_input("Business Type", placeholder="e.g., Real Estate, Fitness") primary_service_or_product = st.text_input("Primary Service/Product", placeholder="e.g., Home sales, Personal training sessions") unique_selling_points = st.text_input("Unique Selling Points", placeholder="What sets your business apart?") business_location = st.text_input("Business Location", placeholder="City or region you primarily serve") st.subheader("Your Customer Interactions") most_common_customer_feedback = st.text_area("Common Customer Feedback", placeholder="What feedback do you often receive from customers?") customer_challenges = st.text_input("Customer Challenges", placeholder="What challenges do your customers typically face?") repeat_customer_characteristics = st.text_input("Repeat Customer Characteristics", placeholder="Any common traits among your repeat customers?") if st.button('Generate Avatar and Content Pillars'): # Construct the prompt for text generation prompt_text = ( f"Create a detailed customer avatar and suggest content pillars based on the following business details: " f"Business type: {business_type}, primary service/product: {primary_service_or_product}, " f"unique selling points: {unique_selling_points}, business location: {business_location}. " f"Customer feedback: {most_common_customer_feedback}, customer challenges: {customer_challenges}, " f"repeat customer characteristics: {repeat_customer_characteristics}." ) # Call the OpenAI API for text generation try: response_text = openai.ChatCompletion.create( model="gpt-4", 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}" # Display the avatar description with formatted headers st.markdown("### Customer Avatar Description") st.write(avatar_description.split('\n\n')[0]) # Assuming the avatar description is the first paragraph st.markdown("### Content Pillars Recommendations") st.write('\n'.join(avatar_description.split('\n\n')[1:])) # Assuming the rest is content pillars # Additional prompt for image generation prompt_image = f"An image representing the customer avatar based on the business type: {business_type}, and customer characteristics: {repeat_customer_characteristics}." # Call the OpenAI API for image generation try: response_image = openai.Image.create( model="dall-e-2", # Specify DALL-E model prompt=prompt_image, n=1, size="1024x1024" # Set image dimensions ) # Assuming the response contains a URL to the image image_url = response_image['data'][0]['url'] # Fetch the image from the URL image_response = requests.get(image_url) image = Image.open(BytesIO(image_response.content)) # Display the image with a header st.markdown("### Generated Customer Avatar Image") st.image(image, caption='Generated Customer Avatar Image') except Exception as e: st.error(f"Error in generating image: {e}")