Skip to content

Instantly share code, notes, and snippets.

@fearnworks
Created June 3, 2023 14:34
Show Gist options
  • Save fearnworks/df509cd669dcda6effe9b93fd144e353 to your computer and use it in GitHub Desktop.
Save fearnworks/df509cd669dcda6effe9b93fd144e353 to your computer and use it in GitHub Desktop.
OUTPUT :
0.0:
Summary:
{
"subject": "a man",
"characters": ["a man", "a silhouetted figure", "an old friend"],
"locations": ["a dark alley in the heart of the city"],
"events": ["a man walks down a dark alley", "a silhouetted figure appears and blocks his path", "the man recognizes the figure as an old friend", "the two
catch up on old times and walk out of the alley"],
"summary": "A man walks down a dark alley and encounters a silhouetted figure who he initially fears is a foe. The figure turns out to be an old friend, and
the two catch up on old times before leaving the alley.",
"quality": 80
}
Story:
In the heart of the city, a man walked down a dark alley, the low light casting ominous shadows on the rough textures of the walls. The moody color scheme added
to the mysterious atmosphere, making him feel like he was being watched. Suddenly, a silhouetted figure appeared at the end of the alley, blocking his path.
The man's heart raced as he tried to make out the figure's features. Was it a friend or foe? He couldn't tell. The figure lurked in the shadows, making it
difficult to discern any details. The man felt a sense of danger, but he couldn't quite put his finger on why.
As he approached the figure, he noticed a glint of light reflecting off a sharp object. The man's instincts kicked in, and he prepared for a surprise attack.
But before he could react, the figure stepped forward, revealing a face he recognized.
It was an old friend, someone he hadn't seen in years. The man breathed a sigh of relief, feeling foolish for his earlier fear. The two embraced, catching up on
old times. As they walked out of the alley, the man couldn't help but feel grateful for the mysterious atmosphere that had brought him back in touch with an old
friend.
1.1:
Summary:
{
"subject": "a lone man",
"characters": ["the man", "the silhouetted figure"],
"locations": ["a desolate city", "a dark alley"],
"events": ["the man is walking down a dark alley", "he encounters a silhouetted figure and fights with them", "he gets attacked and loses consciousness"],
"summary": "A man is walking down a dark alley in a desolate city where he encounters a silhouetted figure, the two get into a fight resulting in the man
losing consciousness after being attacked.",
"quality": 70
}
Story:
In a desolate city, a lone man walks down a dark, foreboding alley. The low light casts ominous shadows on the rough textures of the walls, creating a moody
color scheme. The silence is palpable, coupled with a mysterious atmosphere.
Suddenly, a silhouetted figure appears, lurking in the shadows. The man grits his teeth, recognizing the lurking danger. He reaches for a weapon hidden beneath
his coat but hesitates, not wanting to give himself away. The figure moves closer, and the man's heart races as he strains his eyes to make out what the person
wants.
The figure steps forward, revealing a dangerous glint in their eyes. The man's heart clenches in fear as he readies himself for a surprise attack. The figure
lunges, moving with uncanny stealth and speed. The man jumps aside, narrowly avoiding the attack.
He regains his balance and holds his ground, but the figure seems to have vanished. The man scans the darkness, his heart pounding with adrenaline. Suddenly, he
feels a sharp pain in his back. He falls, his vision blurring, unable to see who or what had attacked him.
As his consciousness fades, the man regrets being lured into the alleyway. He feels helpless and vulnerable, succumbing to the hidden dangers that lie within
the shadows.
CODE:
import fwutil.utils as Utils
import requests
import textwrap
SD_PROMPT_TO_STORY = """
You: "Assistant, can you elaborate on your narrative generation capabilities? I have a list of phrases and I'm hoping to transform them into a cohesive story.\n"
Assistant: "Certainly! My narrative generation capabilities enable me to interconnect separate phrases into a coherent and engaging narrative. I apply contextual understanding and creative interpretation to ensure a seamless and logical flow, while making the story intriguing to read.\n"
You: "Can you clarify the significance of triple parentheses like in '(((Human Paladin's armor becomes magnetic)))'? How would that be interpreted?\n"
Assistant: "In this context, an item enclosed in triple parentheses signifies its prominence and likely role as the central event for the narrative. For example, 'Human Paladin's armor becomes magnetic' would be a key plot point around which the story revolves. This would involve describing the challenges faced due to this sudden change, linking it with other phrases, and incorporating it into the story's overall direction.\n"
You: "Could you provide an example? Assume my list of phrases includes '((a dark storm))', 'a lost key', and 'a mysterious stranger'.\n"
Assistant: "Certainly. Using those phrases, emphasizing 'a dark storm', a possible story could be:\n\n 'A dark storm raged over the town, its relentless onslaught compelling the citizens to seek shelter. Amid the chaos, a key of significant importance was lost, a fact known only to the town's wise sage. As despair started to take hold, a mysterious stranger arrived, offering not only hope of finding the key but the possibility of a new dawn for the town.'\n"
You: "'(a scorching desert)', 'a hidden treasure', 'a devious scorpion'?\n"
Assistant: "In the depths of a scorching desert, a hidden treasure awaited discovery, its legends buried beneath golden sands. One fateful day, a cunning scorpion unlike any other desert creature uncovered the treasure, revealing secrets that would forever change the landscape and its inhabitants.\n"
You: "How about 'a snowy mountain', 'a broken compass', and 'a brave explorer'?\n"
Assistant: "Certainly. Here's a narrative using those elements:\n\n 'Perched on a snowy mountain peak, a brave explorer was faced with the daunting task of navigating home with a broken compass. Through courage and determination, he overcame this perilous journey, transforming it into an epic tale of human resilience.'\n"
You: "Consider this phrase: '(((Anyone nearby with a drawn blade can't use it against the human paladin)))', and these words: 'Human paladin, sword drawn, glowing aura, radiant light, divine power, protective shield, impenetrable, unbreakable, invincible, immune to attacks, holy warrior, champion of justice, hero, savior, protector'.\n"
You: "That's not correct. You simply listed out the words. Remember to use the given context to form a narrative. Try again.\n"
Assistant: "Under the setting sun, a group of bandits emerged, weapons drawn, demanding valuables from the passersby. Among the threatened was a human paladin, clutching his sword and ready to protect those unable to defend themselves. As the bandits lunged forward to strike, they found their movements stifled. A vibrant light engulfed the paladin, his raised sword emitting a divine radiance.\n\nAn invisible, impenetrable force pushed the bandits back, their weapons unable to harm the shielded paladin. They tried again and again, but each strike bounced harmlessly off his radiant shield. As the last bandit retreated into the dark, the townsfolk recounted the tale of this holy warrior, a true champion of justice, for weeks to come.\n"
You: "Much better. Here's the next scenario: '(((Next enemy wounded by elf ranger becomes invisible to elf ranger)))', with these words: 'Elf ranger, invisible enemy, Dungeons and Dragons effect, magical, mysterious, sneaky, tactical advantage, surprise attack, stealth, camouflage, hidden'.\n"
Assistant: "In a mystical forest, an elf ranger named Lirael stood guard. Her sharp eyes missed nothing in her surroundings. An unexpected confrontation unfolded one evening as a malevolent entity invaded their sacred ground. Lirael retaliated swiftly, wounding the invader. However, as the arrow struck, the beast vanished from Lirael's sight, invisible to her but still a threat in the enchanted forest.\n"
You: "You're now going to transform a Stable Diffusion prompt into a short story. Remember to form a narrative from the prompt, interpreting visual details into a sequence of events. Ensure you maintain consistency in your story.
Prompt: {sd_prompt}
Now assistant generate a story from that last prompt. Use proper spacing of paragraphs as needed for clarity and always insert 2 newlines between paragraphs. Let's work through this step by step to make sure we have a good story
"
"""
SD_PROMPT_CONTEXT_RPG_SUMMARY = """\
For the following text, extract the following information:
subject: Who is the main subject of the story
characters: Who are the characters in the story
locations: Where does the story take place
events: What are the main events in the story
summary: What is the summary of the story
quality: How good is the story on a scale of 1-100
Format the output as JSON with the following keys:
subject
characters
locations
events
summary
quality
text: {text}
"""
class OpenAISDPromptToStoryAgent(OpenAISDAgent):
def __init__(self, temperature):
super().__init__(temperature = temperature)
self.template = ChatPromptTemplate.from_template(SD_PROMPT_TO_STORY)
self.summary_template = ChatPromptTemplate.from_template(SD_PROMPT_CONTEXT_RPG_SUMMARY)
def wrap_text(text, width=160):
lines = text.split('\n')
wrapped_lines = [textwrap.fill(line, width) for line in lines]
return '\n'.join(wrapped_lines)
def generate(self, prompt):
story_request = self.template.format_messages(sd_prompt=prompt)
response = self.chat(story_request)
return response.content
def summarize(self,prompt):
summary_request = self.summary_template.format_messages(text=prompt)
response = self.chat(summary_request)
return response.content
def generate_and_summarize(self, prompt):
story = self.generate(prompt)
summary = self.summarize(story)
return story, summary
def print_generate_and_summarize(self, prompt):
story, summary = self.generate_and_summarize(prompt)
print("Summary:\n")
print(wrap_text(summary))
print("Story:\n")
print(wrap_text(story))
sd_gen_ouput = OpenAISDAgent(temperature=1.3).generate("A man in a dark alley")
print("0.0:\n")
OpenAISDPromptToStoryAgent(0.0).print_generate_and_summarize(sd_gen_ouput)
print("1.1:\n")
OpenAISDPromptToStoryAgent(1.1).print_generate_and_summarize(sd_gen_ouput)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment