-
Notifications
You must be signed in to change notification settings - Fork 0
/
storyscribe.py
83 lines (60 loc) · 2.92 KB
/
storyscribe.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
from dotenv import load_dotenv
import streamlit as st
from langchain.llms import OpenAI
from langchain.chains import SequentialChain, LLMChain
from langchain.prompts import PromptTemplate
from langchain.utilities.dalle_image_generator import DallEAPIWrapper
st.set_page_config(page_title="StoryScribe", page_icon="📙")
st.header('📙 Welcome to StoryScribe, your story generator and promoter!')
load_dotenv()
openai_api_key = os.environ['OPENAI_API_KEY']
# Create a sidebar for user input
st.sidebar.title("Story teller and promoter")
st.sidebar.markdown("Please enter your details and preferences below:")
llm = OpenAI()
# Ask the user for age, gender and favourite movie genre
topic = st.sidebar.text_input("What is topic?", 'A dog running on the beach')
genre = st.sidebar.text_input("What is the genre?", 'Drama')
audience = st.sidebar.text_input("What is your audience?", 'Young adult')
social = st.sidebar.text_input("What is your social?", 'Instagram')
#story generator
story_template = """You are a storyteller. Given a topic, a genre and a target audience, you generate a story.
Topic: {topic}
Genre: {genre}
Audience: {audience}
Story: This is a story about the above topic, with the above genre and for the above audience:"""
story_prompt_template = PromptTemplate(input_variables=["topic", "genre", "audience"], template=story_template)
story_chain = LLMChain(llm=llm, prompt=story_prompt_template, output_key="story")
#post generator
social_template = """You are an influencer that, given a story, generate a social media post to promote the story.
The style should reflect the type of social media used.
Story:
{story}
Social media: {social}
Review from a New York Times play critic of the above play:"""
social_prompt_template = PromptTemplate(input_variables=["story", "social"], template=social_template)
social_chain = LLMChain(llm=llm, prompt=social_prompt_template, output_key='post')
#image generator
image_template = """Generate a detailed prompt to generate an image based on the following social media post:
Social media post:
{post}
The style of the image should be oil-painted.
"""
prompt = PromptTemplate(
input_variables=["post"],
template=image_template,
)
image_chain = LLMChain(llm=llm, prompt=prompt, output_key='image')
#overall chain
overall_chain = SequentialChain(input_variables = ['topic', 'genre', 'audience', 'social'],
chains=[story_chain, social_chain, image_chain],
output_variables = ['story','post', 'image'], verbose=True)
if st.button('Create your post!'):
result = overall_chain({'topic': topic,'genre':genre, 'audience': audience, 'social': social}, return_only_outputs=True)
image_url = DallEAPIWrapper().run(result['image'])
st.subheader('Story')
st.write(result['story'])
st.subheader('Social Media Post')
st.write(result['post'])
st.image(image_url)