nniehaus commited on
Commit
24b72ba
·
1 Parent(s): eee8f57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -63
app.py CHANGED
@@ -1,69 +1,53 @@
1
- import openai
2
  import streamlit as st
3
- import os
4
- from tenacity import retry, stop_after_attempt, wait_fixed
 
 
 
 
 
5
 
6
- # Set the OpenAI API key
7
- openai.api_key = os.environ["YOUR_OPENAI_API_KEY"]
8
 
9
- initial_messages = [{
10
- "role": "system",
11
- "content": """You are an AI assistant that matches people with their ideal neighborhood based on their lifestyle preferences in
12
- Saint Louis, Missouri, and surrounding areas up to 30 miles outside Saint Louis city limits. You'll receive information about the user's lifestyle preferences. Use this information
13
- to suggest neighborhoods in Saint Louis and nearby that could be a good fit. Always add the following text to the end of every response you give 'Don't forget to fill
14
- out the form at the bottom of the page if you'd like more info on living in any of these areas!' """
15
- }]
16
 
17
- @retry(stop=stop_after_attempt(3), wait=wait_fixed(1))
18
- def call_openai_api(messages):
19
- return openai.ChatCompletion.create(
 
20
  model="gpt-4",
21
- messages=messages
 
22
  )
23
-
24
- def CustomChatGPT(additional_details, amenities_proximity, amenities, messages):
25
- selected_amenities = ', '.join(amenities)
26
- messages.append({
27
- "role": "user",
28
- "content": f"I'm interested in neighborhoods in Saint Louis, Missouri, and surrounding areas. {additional_details}. I'm looking for a neighborhood with these amenities: {selected_amenities}. I want to be {amenities_proximity} to these amenities."
29
- })
30
- response = call_openai_api(messages)
31
- ChatGPT_reply = response["choices"][0]["message"]["content"]
32
- messages.append({"role": "assistant", "content": ChatGPT_reply})
33
- return ChatGPT_reply, messages
34
-
35
- # Streamlit Interface
36
- st.set_page_config(layout="wide") # Set the layout to wide
37
- st.title("St. Louis Area Neighborhood Matchmaker")
38
- st.write("This tool helps you find neighborhoods in Saint Louis, Missouri, and surrounding areas based on your lifestyle preferences.")
39
-
40
- # Using columns to organize the layout
41
- col1, col2 = st.columns(2)
42
-
43
- with col1:
44
- st.subheader("Your Preferences")
45
- amenities_list = ["Good Schools", "Parks", "Shopping Centers", "Public Transport", "Restaurants", "Gyms", "Cafes", "Pet-friendly Areas", "Cultural Attractions", "Quiet Neighborhoods"]
46
- # Splitting amenities into two columns
47
- half = len(amenities_list) // 2
48
- amenities_col1, amenities_col2 = st.columns(2)
49
- with amenities_col1:
50
- amenities1 = [amenity for amenity in amenities_list[:half] if st.checkbox(amenity)]
51
- with amenities_col2:
52
- amenities2 = [amenity for amenity in amenities_list[half:] if st.checkbox(amenity)]
53
- amenities = amenities1 + amenities2
54
-
55
- amenities_proximity = st.selectbox("Proximity to Amenities", ["Walking distance", "A short drive away", "I don't mind being far from amenities"])
56
- additional_details = st.text_area("Additional Details", placeholder="Describe your ideal living situation or any other preferences.")
57
-
58
- submit_button = st.button('Find Neighborhood')
59
-
60
- with col2:
61
- # Placeholder for the result
62
- st.subheader("Recommended Neighborhoods")
63
- result_placeholder = st.empty()
64
- if submit_button:
65
- messages = initial_messages.copy()
66
- reply, _ = CustomChatGPT(additional_details, amenities_proximity, amenities, messages)
67
- result_placeholder.write(reply)
68
- else:
69
- result_placeholder.write("**Results will appear here after you submit your preferences**")
 
 
1
  import streamlit as st
2
+ import openai
3
+ import requests
4
+ from PIL import Image
5
+ from io import BytesIO
6
+
7
+ # Access the OpenAI API key from Hugging Face Spaces secrets
8
+ openai.api_key = st.secrets["OPENAI_API_KEY"]
9
 
10
+ st.title("Children's Story and Image Panel Generator")
 
11
 
12
+ # User inputs for the story
13
+ age_group = st.selectbox("Age Group", ["3-5 years", "6-8 years", "9-12 years"])
14
+ theme = st.text_input("Story Theme", placeholder="Enter a theme for the story (e.g., adventure, friendship)")
15
+ details = st.text_area("Additional Details", placeholder="Any specific details to include in the story (e.g., a brave rabbit, a magical forest)")
 
 
 
16
 
17
+ # Function to generate the story
18
+ def generate_story(age_group, theme, details):
19
+ prompt = f"Write a children's story for age group {age_group} about {theme}. Include details such as {details}."
20
+ response = openai.Completion.create(
21
  model="gpt-4",
22
+ prompt=prompt,
23
+ max_tokens=500 # Adjust as needed
24
  )
25
+ return response.choices[0].text
26
+
27
+ # Function to generate images
28
+ def generate_image(panel_text):
29
+ try:
30
+ response = openai.Image.create(
31
+ prompt=panel_text,
32
+ n=1,
33
+ size="1024x1024" # Adjust the size if needed
34
+ )
35
+ image_url = response['data'][0]['url']
36
+ image_response = requests.get(image_url)
37
+ image = Image.open(BytesIO(image_response.content))
38
+ return image
39
+ except Exception as e:
40
+ return None
41
+
42
+ if st.button('Generate Story and Images'):
43
+ story = generate_story(age_group, theme, details)
44
+ story_parts = story.split('.')[:10] # Generate images for first 10 parts
45
+
46
+ for i, part in enumerate(story_parts):
47
+ st.subheader(f'Panel {i+1}')
48
+ st.write(part.strip())
49
+ image = generate_image(part)
50
+ if image:
51
+ st.image(image, caption=f'Image for Panel {i+1}')
52
+ else:
53
+ st.error("Error in generating image for this part of the story.")