PromptStory / app.py
vjchamp's picture
Update app.py
1a9d2ca verified
Raw History Blame Contribute Delete
7.12 kB
import random
import gradio as gr
def generate_story(character, setting, mood, genre, length):
# Normalize genre input
genre = genre.strip().lower()
genre = genre.replace(" d", "-")
genre = genre.replace(".", "")
# Genre aliases
genre_aliases = {
"romantic": "romance",
"rom-com": "romance",
"scifi": "sci-fi",
"sci fi": "sci-fi",
"science-fiction": "sci-fi",
"mistery": "mystery",
"fantacy": "fantasy",
}
genre = genre_aliases.get(genre, genre)
# Banks
sensory_details = [
"the air carried a faint smell of rain",
"soft footsteps echoed nearby",
"a warm breeze brushed past",
"shadows shifted quietly",
"the light flickered in a strange rhythm",
"an unfamiliar stillness filled the space",
"distant voices blended into the wind"
]
emotional_beats = [
"a strange pull tugged at their chest",
"their heartbeat stuttered for a moment",
"a quiet tension wrapped around them",
"their breath caught in their throat",
"something inside them softened unexpectedly",
"a spark of curiosity lit up their expression"
]
dialogue_starters = [
"\"I didn’t expect to see you here,\" someone murmured.",
"\"Are you lost?\" a voice asked from behind.",
"\"Strange day, isn’t it?\" a stranger said softly.",
"\"You felt it too, didn’t you?\" came the whisper."
]
# Genre banks
genre_hooks = {
"romance": [
f"{character} hadn’t meant to linger in {setting}, but something in the {mood} atmosphere slowed their steps.",
f"There was a softness in {setting} that {character} couldn’t explain—not today, not with their heart feeling so {mood}."
],
"horror": [
f"The silence in {setting} felt wrong, the kind that presses against your skin until you shiver.",
f"{character} felt a {mood} heaviness in the air—thick, watching, waiting."
],
"mystery": [
f"The day began normally in {setting}, until {character} noticed something that didn’t belong.",
f"There was a puzzle hidden in the {mood} quiet of {setting}, and {character} could sense it shifting."
],
"fantasy": [
f"In {setting}, the air shimmered faintly—as if the world remembered a secret.",
f"{character} sensed ancient {mood} magic waking beneath the ground."
],
"sci-fi": [
f"The neon lights of {setting} flickered, glitching like corrupted code.",
f"{character} felt a {mood} pulse ripple through the tech-laced air."
],
"adventure": [
f"{setting} stretched before {character}, alive with {mood} possibility.",
f"The path ahead glittered with dust and sunlight, calling {character} forward."
],
}
genre_middles = {
"romance": [
f"They noticed someone watching them with a smile that warmed the space between them.",
f"A gentle laugh reached {character}, pulling their attention toward a stranger whose presence softened everything."
],
"horror": [
f"A soft scraping noise followed them—too close, too deliberate.",
f"Shadows seemed to breathe, shifting just beyond their line of sight."
],
"mystery": [
f"A detail others ignored caught {character}'s eye—small, but wrong.",
f"Someone slipped an object into their hand before vanishing into the crowd."
],
"fantasy": [
f"A creature of light emerged, watching {character} with knowing eyes.",
f"A map etched in floating runes appeared before them."
],
"sci-fi": [
f"A rogue signal pinged through their neural implant.",
f"The ground trembled as a holographic distortion whispered data into their mind."
],
"adventure": [
f"A hidden trail revealed itself after a gust of wind swept the dust aside.",
f"A stranger handed them a weathered map, eyes gleaming with challenge."
],
}
genre_endings = {
"romance": [
"The moment settled softly between them—warm, promising, beautifully uncertain.",
"Something new began blooming quietly in their heart, a feeling too real to ignore."
],
"horror": [
"The darkness didn’t answer, but it breathed—waiting for the next step.",
"The unease lingered long after the echoes faded, refusing to let go."
],
"mystery": [
"The final clue sat heavy in their palm, raising more questions than answers.",
"A quiet realization crept in: the real mystery was only beginning."
],
"fantasy": [
"Magic pulsed around them, whispering that their destiny had only just awakened.",
"The world shifted slightly, as if bowing to the path they were now bound to follow."
],
"sci-fi": [
"The system hummed with new data, hinting at a future they had yet to uncover.",
"Reality flickered once—an invitation to step beyond what they knew."
],
"adventure": [
"With a steady breath, they stepped forward, ready for whatever came next.",
"The horizon stretched open, full of promise, full of the stories waiting to be lived."
],
}
if genre not in genre_hooks:
genre = "mystery"
hook = random.choice(genre_hooks[genre])
middle = random.choice(genre_middles[genre])
ending = random.choice(genre_endings[genre])
sensory = random.choice(sensory_details)
emotion = random.choice(emotional_beats)
dialogue = random.choice(dialogue_starters)
if length == "short":
story = f"{hook} {sensory}. {middle} {emotion}. {ending}"
elif length == "medium":
story = (
f"{hook} {sensory}. "
f"{middle} {dialogue} "
f"{emotion}. "
f"{ending}"
)
else: # long
scene1 = f"{hook} {sensory}. {emotion}."
scene2 = f"{middle} {dialogue}"
scene3 = f"{ending}"
story = f"{scene1}\n\n{scene2}\n\n{scene3}"
return story
# --------------------------
# GRADIO APP
# --------------------------
def gradio_story(character, setting, mood, genre, length):
return generate_story(character, setting, mood, genre, length)
ui = gr.Interface(
fn=gradio_story,
inputs=[
gr.Textbox(label="Character Name"),
gr.Textbox(label="Setting"),
gr.Textbox(label="Mood"),
gr.Radio(["romance", "fantasy", "mystery", "adventure", "sci-fi", "horror"], label="Genre"),
gr.Radio(["short", "medium", "long"], label="Story Length")
],
outputs=gr.Textbox(label="Generated Story", lines=20),
title="AI Story Generator",
description="Generate stories with emotions, sensory detail, and genre styling.",
)
ui.launch()