AICopyCC's picture
Update app.py
81985aa verified
import openai
import streamlit as st
import os
from tenacity import retry, stop_after_attempt, wait_fixed
# Set the OpenAI API key
openai.api_key = os.environ["sk-proj-39WvclbTNo8aFwOukQXET3BlbkFJn4sNgi7G3tXMNb8OyS9t"]
initial_messages = [{
"role": "system",
"content": """You are an AI assistant that suggests weekend activities in Chelmsford, Essex, and surrounding areas up to 10 miles away. You'll receive information about the user's preferences for the weekend. Use this information to suggest activities in the area. Always add the following text to the end of every response you give: 'Enjoy your weekend!' """
}]
@retry(stop=stop_after_attempt(3), wait=wait_fixed(1))
def call_openai_api(messages):
return openai.ChatCompletion.create(
model="gpt-4",
messages=messages
)
def CustomChatGPT(activities, location_proximity, location_type, group_type, messages):
selected_activities = ', '.join(activities)
messages.append({
"role": "user",
"content": f"I'm looking for weekend activities in Chelmsford, Essex, and surrounding areas up to 10 miles away. I'm interested in {selected_activities}. I prefer to do activities {location_proximity} and in {location_type}. My group type is {group_type}."
})
response = call_openai_api(messages)
ChatGPT_reply = response["choices"][0]["message"]["content"]
messages.append({"role": "assistant", "content": ChatGPT_reply})
return ChatGPT_reply, messages
# Streamlit Interface
st.set_page_config(layout="wide") # Set the layout to wide
st.title("Weekend Activity Matchmaker")
st.write("This tool suggests weekend activities in Chelmsford, Essex, and surrounding areas based on your preferences.")
# Using columns to organize the layout
col1, col2 = st.columns(2)
with col1:
st.subheader("Your Preferences")
activities_list = ["Outdoor activities", "Indoor activities", "Free activities", "On a budget", "Restaurants", "Live music", "Museums", "Sports", "Games", "Animals", "Shopping", "Parks", "Cultural attractions", "Spas", "Swimming pools"]
# Splitting activities into two columns
half = len(activities_list) // 2
activities_col1, activities_col2 = st.columns(2)
with activities_col1:
activities1 = [activity for activity in activities_list[:half] if st.checkbox(activity)]
with activities_col2:
activities2 = [activity for activity in activities_list[half:] if st.checkbox(activity)]
activities = activities1 + activities2
location_proximity = st.selectbox("Proximity to Activities", ["Nearby", "A short drive away", "I don't mind travelling"])
location_type = st.selectbox("Location Type", ["City centre", "Out of town", "Any"])
group_type = st.selectbox("Group Type", ["Individual", "Small group", "Family", "Pet-friendly", "Accessible"])
submit_button = st.button('Find Activities')
with col2:
# Placeholder for the result
st.subheader("Recommended Activities")
result_placeholder = st.empty()
if submit_button:
messages = initial_messages.copy()
reply, _ = CustomChatGPT(activities, location_proximity, location_type, group_type, messages)
result_placeholder.write(reply + "\nEnjoy your weekend!")
else:
result_placeholder.write("**Results will appear here after you submit your preferences**")