-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStoryBook.py
69 lines (58 loc) · 1.79 KB
/
StoryBook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#import
from openai import OpenAI
import streamlit as st
# import dotenv
# dotenv.load_dotenv()
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
def story_gen(prompt):
response=client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system",
"content": """
You are an accomplished storyteller. You have published young adult short stories for 5 years.
Given a topic, you can write quality fantasy stories with a closed ending.
The story must be 100-150 words long.
"""},
{"role": "user",
"content": prompt}
],
temperature=1.3,
max_tokens=1000,
)
return response.choices[0].message.content
def cover_prompt(prompt):
response= client.chat.completions.create(
model='gpt-4o-mini',
messages=[
{'role':'system','content':"""
You are tasked with generating a prompt for an AI image generator.
A story will be given and you have to analyse and digest the contents, and extract the main elements or essence of the story.
Write a short prompt to produce an interesting and relevant cover art for the story.
"""},
{'role':'user', 'content':prompt}],
temperature= 1,
max_tokens=1000
)
return response.choices[0].message.content
def cover_art(prompt):
response= client.images.generate(
model='dall-e-3',
prompt=prompt,
size='1024x1024',
quality='standard',
n=1,
style='vivid'
)
return response.data[0].url
def storybook(prompt):
story= story_gen(prompt)
cover= cover_prompt(story)
image= cover_art(cover)
st.image(image)
st.caption(cover)
st.divider()
st.write(story)
prompt = st.text_input('What is the Title?')
if st.button('Generate Story'):
storybook(prompt)