Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ 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["
|
| 9 |
|
| 10 |
st.title("Children's Story and Image Panel Generator")
|
| 11 |
|
|
@@ -16,40 +16,42 @@ details = st.text_area("Additional Details", placeholder="Any specific details t
|
|
| 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.ChatCompletion.create(
|
| 21 |
model="gpt-4",
|
| 22 |
messages=[
|
| 23 |
{"role": "system", "content": "You are a creative writer."},
|
| 24 |
{"role": "user", "content": prompt}
|
| 25 |
-
]
|
|
|
|
| 26 |
)
|
| 27 |
return response.choices[0].message['content']
|
| 28 |
|
| 29 |
-
# Function to generate images
|
| 30 |
def generate_image(panel_text):
|
| 31 |
try:
|
| 32 |
response = openai.Image.create(
|
| 33 |
prompt=panel_text,
|
| 34 |
n=1,
|
| 35 |
-
size="1024x1024" # Adjust the size if needed
|
|
|
|
| 36 |
)
|
| 37 |
image_url = response['data'][0]['url']
|
| 38 |
image_response = requests.get(image_url)
|
| 39 |
image = Image.open(BytesIO(image_response.content))
|
| 40 |
return image
|
| 41 |
except Exception as e:
|
|
|
|
| 42 |
return None
|
| 43 |
|
| 44 |
if st.button('Generate Story and Images'):
|
| 45 |
story = generate_story(age_group, theme, details)
|
| 46 |
-
story_parts = story.split('.')[:10] #
|
| 47 |
|
| 48 |
for i, part in enumerate(story_parts):
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
st.error("Error in generating image for this part of the story.")
|
|
|
|
| 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 |
|
|
|
|
| 16 |
|
| 17 |
# Function to generate the story
|
| 18 |
def generate_story(age_group, theme, details):
|
| 19 |
+
prompt = f"Write a concise children's story suitable for age group {age_group} about {theme}. Include details such as {details}. The story should be short enough to fit within 10 panels."
|
| 20 |
response = openai.ChatCompletion.create(
|
| 21 |
model="gpt-4",
|
| 22 |
messages=[
|
| 23 |
{"role": "system", "content": "You are a creative writer."},
|
| 24 |
{"role": "user", "content": prompt}
|
| 25 |
+
],
|
| 26 |
+
max_tokens=300 # Adjust the token limit as needed
|
| 27 |
)
|
| 28 |
return response.choices[0].message['content']
|
| 29 |
|
| 30 |
+
# Function to generate images with DALL-E 3
|
| 31 |
def generate_image(panel_text):
|
| 32 |
try:
|
| 33 |
response = openai.Image.create(
|
| 34 |
prompt=panel_text,
|
| 35 |
n=1,
|
| 36 |
+
size="1024x1024", # Adjust the size if needed
|
| 37 |
+
model="dall-e-3" # Specify DALL-E 3 model
|
| 38 |
)
|
| 39 |
image_url = response['data'][0]['url']
|
| 40 |
image_response = requests.get(image_url)
|
| 41 |
image = Image.open(BytesIO(image_response.content))
|
| 42 |
return image
|
| 43 |
except Exception as e:
|
| 44 |
+
st.error(f"Error in generating image: {e}")
|
| 45 |
return None
|
| 46 |
|
| 47 |
if st.button('Generate Story and Images'):
|
| 48 |
story = generate_story(age_group, theme, details)
|
| 49 |
+
story_parts = story.split('.')[:10] # Use the first 10 sentences for panels
|
| 50 |
|
| 51 |
for i, part in enumerate(story_parts):
|
| 52 |
+
if part.strip(): # Ensure part is not empty
|
| 53 |
+
st.subheader(f'Panel {i+1}')
|
| 54 |
+
st.write(part.strip())
|
| 55 |
+
image = generate_image(part)
|
| 56 |
+
if image:
|
| 57 |
+
st.image(image, caption=f'Image for Panel {i+1}')
|
|
|