zahidpichen commited on
Commit
85c6c13
·
verified ·
1 Parent(s): d3840d7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain import PromptTemplate, LLMChain
3
+ from langchain_cohere import ChatCohere
4
+ from transformers import BlipProcessor, BlipForConditionalGeneration
5
+ from PIL import Image
6
+ from diffusers import DiffusionPipeline
7
+ import os
8
+
9
+ # Load models
10
+ model_name = "Salesforce/blip-image-captioning-base"
11
+ processor = BlipProcessor.from_pretrained(model_name)
12
+ model = BlipForConditionalGeneration.from_pretrained(model_name)
13
+
14
+ chat = ChatCohere(model="command", cohere_api_key="Pkl2kMU326tZ4aJhgE2up6iD9qJeuVDgS6EdWgaJ")
15
+
16
+ # Define templates and chains
17
+ story_template = PromptTemplate(
18
+ input_variables=["prompt"],
19
+ template="""
20
+ Create a Chapter wise detailed and sequential story based on the following prompt: {prompt}. It must only have 5 chapters. here's an example of the output:
21
+ Chapter 0:
22
+ Chapter 1:
23
+ Chapter 2:
24
+ Chapter 3:
25
+ Chapter 4:
26
+ Chapter 5:
27
+
28
+ after chapter 5, there should not be any other text.
29
+ """,
30
+ )
31
+ story_chain = LLMChain(llm=chat, prompt=story_template)
32
+
33
+ def generate_story(prompt):
34
+ response = story_chain.invoke(prompt)
35
+ return response['text']
36
+
37
+ def split_story(story):
38
+ chapters = story.split("\n\nChapter ")
39
+ chapters = [chapters[0]] + [f"Chapter {chap}" for chap in chapters[1:]]
40
+ return chapters
41
+
42
+
43
+ def generate_images(story_parts, save_dir="story_images"):
44
+ pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo")
45
+ pipe = pipe.to("cuda")
46
+ os.makedirs(save_dir, exist_ok=True)
47
+ image_paths = []
48
+ for i, part in enumerate(story_parts):
49
+ image = pipe(part).images[0]
50
+ image_path = os.path.join(save_dir, f"story_part_{i+1}.png")
51
+ image.save(image_path)
52
+ image_paths.append(image_path)
53
+ return image_paths
54
+
55
+ def generate_captions(image_paths):
56
+ captions_dict = {}
57
+ for image_path in image_paths:
58
+ image = Image.open(image_path)
59
+ inputs = processor(images=image, return_tensors="pt")
60
+ out = model.generate(**inputs)
61
+ caption = processor.decode(out[0], skip_special_tokens=True)
62
+ captions_dict[image_path] = caption
63
+ return captions_dict
64
+
65
+ story_prompt = PromptTemplate(
66
+ input_variables=[
67
+ "context",
68
+ "image_caption_1",
69
+ "image_caption_2",
70
+ "image_caption_3",
71
+ "image_caption_4",
72
+ "image_caption_5",
73
+ "image_caption_6"
74
+ ],
75
+ template="""
76
+ You are given a sequence of 6 images. Your task is to create a coherent and engaging story based on these images. Use the provided context to set the scene and ensure that each part of the story corresponds to the captions of the images in the order they are provided.
77
+
78
+ Instructions:
79
+ 1. Read the context to understand the background and setting of the story.
80
+ 2. Carefully look at the image captions to capture the essence of each image.
81
+ 3. Create a story that weaves together the elements from the context and the image captions.
82
+ 4. Make sure the transitions between the images are smooth and the story maintains a logical flow.
83
+
84
+ Begin the story below:
85
+
86
+ {context}
87
+
88
+ 1. Image 1: {image_caption_1}
89
+ 2. Image 2: {image_caption_2}
90
+ 3. Image 3: {image_caption_3}
91
+ 4. Image 4: {image_caption_4}
92
+ 5. Image 5: {image_caption_5}
93
+ 6. Image 6: {image_caption_6}
94
+
95
+ Story:
96
+ """
97
+ )
98
+ llm_chain = LLMChain(
99
+ llm=chat,
100
+ prompt=story_prompt
101
+ )
102
+
103
+ def final_story(image_captions, context):
104
+ result = llm_chain.run({
105
+ "context": context,
106
+ "image_caption_1": image_captions[0],
107
+ "image_caption_2": image_captions[1],
108
+ "image_caption_3": image_captions[2],
109
+ "image_caption_4": image_captions[3],
110
+ "image_caption_5": image_captions[4],
111
+ "image_caption_6": image_captions[5]
112
+ })
113
+ return result
114
+
115
+
116
+ # Streamlit UI
117
+ st.title("Story Board")
118
+ prompt = st.text_input("Enter your story prompt:", "A heroic journey of a young adventurer in a mystical land.")
119
+ if st.button("Generate Story"):
120
+ with st.spinner("Generating story..."):
121
+ story_context = generate_story(prompt)
122
+ story_parts = split_story(story_context)
123
+ image_paths = generate_images(story_parts)
124
+ captions_dict = generate_captions(image_paths)
125
+ captions_list = list(captions_dict.values())
126
+ final_story_text = final_story(image_captions=captions_list, context=story_context)
127
+ for i in range(6):
128
+ st.image(image_paths[i])
129
+ st.write(story_parts[i + 1]) # Display chapters below corresponding images