Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,53 @@
|
|
1 |
-
import openai
|
2 |
import streamlit as st
|
3 |
-
import
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
openai.api_key = os.environ["YOUR_OPENAI_API_KEY"]
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
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 |
-
|
18 |
-
def
|
19 |
-
|
|
|
20 |
model="gpt-4",
|
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 |
-
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.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|