tugot17 commited on
Commit
a949e9d
1 Parent(s): 378dcb1

Upload 3 files

Browse files
Files changed (3) hide show
  1. prompt_generation.py +110 -0
  2. requirements.txt +5 -0
  3. streamlit_app.py +82 -0
prompt_generation.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import concurrent.futures
2
+ import re
3
+ from typing import List
4
+
5
+ from dotenv import load_dotenv
6
+ from langchain import LLMChain
7
+ from langchain.chat_models import ChatOpenAI
8
+ from langchain.prompts.chat import (
9
+ ChatPromptTemplate,
10
+ SystemMessagePromptTemplate,
11
+ HumanMessagePromptTemplate,
12
+ )
13
+
14
+ load_dotenv()
15
+
16
+ # chat = ChatAnthropic()
17
+ chat = ChatOpenAI(temperature=0, model="gpt-3.5-turbo")
18
+
19
+
20
+ def generate_the_original_story(user_description: str) -> str:
21
+ system_template = "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions " \
22
+ "carefully. Respond using markdown. "
23
+ prompt_1 = SystemMessagePromptTemplate.from_template(system_template)
24
+
25
+ human_template = "You are a story teller. You generate a story according to users description. The user " \
26
+ "description is {user_description}. Generate a story according to description "
27
+ prompt_2 = HumanMessagePromptTemplate.from_template(human_template)
28
+
29
+ chat_prompt = ChatPromptTemplate.from_messages([prompt_1, prompt_2])
30
+
31
+ chain = LLMChain(llm=chat, prompt=chat_prompt)
32
+ return chain.run(user_description=user_description)
33
+
34
+
35
+ def generate_the_steps_in_the_story(story: str, n_steps=10) -> List[str]:
36
+ system_template = "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions " \
37
+ "carefully. Respond using markdown. "
38
+ prompt_1 = SystemMessagePromptTemplate.from_template(system_template)
39
+
40
+ human_template = """{story}\n Given the story split split it into {n_steps} steps. Every step should consist of
41
+ concise summary of the narrative.
42
+
43
+ Use the following format:
44
+ 1. The first element of the story
45
+ 2. The second element ...
46
+ ...
47
+ N. The last n'th element of the story"""
48
+ prompt_2 = HumanMessagePromptTemplate.from_template(human_template)
49
+
50
+ chat_prompt = ChatPromptTemplate.from_messages([prompt_1, prompt_2])
51
+
52
+ chain = LLMChain(llm=chat, prompt=chat_prompt)
53
+ return re.findall(r"\d+\..+", chain.run(story=story, n_steps=n_steps))
54
+
55
+
56
+ def generate_image_prompts_based_on_a_step(step: str) -> str:
57
+ system_template = "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions " \
58
+ "carefully. Respond using markdown. "
59
+ prompt_1 = SystemMessagePromptTemplate.from_template(system_template)
60
+
61
+ human_template = """Write the nice image prompts for image generation model for the step in the story. ,
62
+
63
+ If the story speaks about some specific human, just say Men or Woman. Give a detailed artistic expressions that
64
+ are useful for an image generation model.
65
+
66
+ Here are some examples:
67
+ - hyper close up photography of a majestic long ear kitsune spirit, ethereal ghost veil, japanese forest scenery, adorable, summer background, dynamic focus, round eyes, Hyperrealist, photography, 8k, ultra high quality, insanely detailed , perfect shading, intricate design, beautiful composition, soft lighting, many particles, Sony a7 III
68
+
69
+ - Iron Man, (Arnold Tsang, Toru Nakayama), Masterpiece, Studio Quality, 6k , toa, toaair, 1boy, glowing, axe, mecha, science_fiction, solo, weapon, jungle , green_background, nature, outdoors, solo, tree, weapon, mask, dynamic lighting, detailed shading, digital texture painting
70
+
71
+ - professional portrait photograph of a gorgeous Norwegian girl in winter clothing with long wavy blonde hair, ((sultry flirty look)), freckles, beautiful symmetrical face, cute natural makeup, ((standing outside in snowy city street)), stunning modern urban upscale environment, ultra realistic, concept art, elegant, highly detailed, intricate, sharp focus, depth of field, f/1.8, 85mm, medium shot, mid shot, (centered image composition), (professionally color graded), ((bright soft diffused light)), volumetric fog, trending on instagram, trending on tumblr, hdr 4k, 8k
72
+
73
+ - detailed and realistic portrait of a woman with a few freckles, round eyes and short messy hair shot outside, wearing a white t shirt, staring at camera, chapped lips, soft natural lighting, portrait photography, magical photography, dramatic lighting, photo realism, ultra-detailed, intimate portrait composition, Leica 50mm, f1. 4
74
+
75
+
76
+ Write a prompt for following step in the story: {step}
77
+
78
+ """
79
+ prompt_2 = HumanMessagePromptTemplate.from_template(human_template)
80
+
81
+ chat_prompt = ChatPromptTemplate.from_messages([prompt_1, prompt_2])
82
+
83
+ chain = LLMChain(llm=chat, prompt=chat_prompt)
84
+
85
+ return chain.run(step=step)
86
+
87
+
88
+ def pipeline(user_description: str, n_steps: int = 10) -> dict:
89
+ story = generate_the_original_story(user_description)
90
+ steps = generate_the_steps_in_the_story(story, n_steps)
91
+
92
+ with concurrent.futures.ThreadPoolExecutor() as executor:
93
+ image_prompts_futures = [
94
+ executor.submit(generate_image_prompts_based_on_a_step, step)
95
+ for step in steps
96
+ ]
97
+
98
+ image_prompts = [fut.result() for fut in image_prompts_futures]
99
+
100
+ return {"story": story, "steps": steps, "image_prompts": image_prompts}
101
+
102
+
103
+ if __name__ == '__main__':
104
+ user_description = "A story about a brave frog."
105
+ from time import time
106
+
107
+ start = time()
108
+ res = pipeline(user_description)
109
+ finish = time()
110
+ print(f"Time: {finish - start}")
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit==1.22.0
2
+ langchain==0.0.153
3
+ openai==0.27.5
4
+ anthropic==0.2.7
5
+ python-dotenv==1.0.0
streamlit_app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from PIL import Image
4
+ from io import BytesIO
5
+ import json
6
+
7
+ from prompt_generation import pipeline
8
+
9
+
10
+ # Function to create the page navigation
11
+ def page_navigation(current_page):
12
+ col1, col2, col3 = st.columns(3)
13
+
14
+ if current_page > 0:
15
+ with col1:
16
+ if st.button('<< Previous'):
17
+ current_page -= 1
18
+
19
+ with col2:
20
+ st.write(f'Step {current_page} of 10')
21
+
22
+ if current_page < 10:
23
+ with col3:
24
+ if st.button('Next >>'):
25
+ if current_page == 0:
26
+ user_input = st.session_state.user_input
27
+ response = pipeline(user_input, 10)
28
+
29
+ st.session_state.pipeline_response = response
30
+
31
+ current_page += 1
32
+
33
+ return current_page
34
+
35
+
36
+ # Main function to display the pages
37
+ def get_pipeline_data(page_number):
38
+ pipeline_response = st.session_state.pipeline_response
39
+ text_output = pipeline_response.get("steps")[page_number - 1]
40
+
41
+ random_img = f"https://picsum.photos/800/600?random={page_number}"
42
+ response = requests.get(random_img)
43
+ image = Image.open(BytesIO(response.content))
44
+
45
+ return {"text_output": text_output, "image_obj": image}
46
+
47
+
48
+ def main():
49
+ st.set_page_config(page_title="Narrative chat", layout="wide")
50
+ st.title("DreamBot")
51
+
52
+ # Initialize the current page
53
+ current_page = st.session_state.get('current_page', 0)
54
+
55
+ # Display content for each page
56
+ if current_page == 0:
57
+ st.write("Tell me what story you would like me to tell:")
58
+ user_input = st.text_area("")
59
+ st.session_state.user_input = user_input
60
+
61
+ else:
62
+ # Retrieve data from random generators
63
+ data = get_pipeline_data(current_page)
64
+ text_output = data.get('text_output', '')
65
+ image = data.get('image_obj', '')
66
+
67
+ # Display text output
68
+ st.write(text_output)
69
+
70
+ # Display image output
71
+ if image:
72
+ st.image(image, use_column_width=False, width=400)
73
+
74
+ # Display page navigation
75
+ current_page = page_navigation(current_page)
76
+
77
+ st.write('current_page:', current_page)
78
+ st.session_state.current_page = current_page
79
+
80
+
81
+ if __name__ == "__main__":
82
+ main()