AI Flash Fiction Generator

import streamlit as st

from openai import OpenAI

import textwrap


# Initialize API

client = OpenAI(api_key="YOUR_OPENAI_API_KEY")


st.set_page_config(page_title="AI Flash Fiction Generator", page_icon="📖", layout="centered")


st.title("📖 AI Flash Fiction Generator")

st.caption("Enter a theme or mood → Get a 100-word short story instantly!")


theme = st.text_input("✨ Enter a theme (e.g., hope, mystery, space, love):")


if st.button("Generate Story"):

    if theme.strip() == "":

        st.warning("Please enter a theme!")

    else:

        with st.spinner("Generating your story..."):

            prompt = f"Write a 100-word flash fiction story about '{theme}'. The story should be complete, emotional, and end with a twist."


            response = client.chat.completions.create(

                model="gpt-3.5-turbo",

                messages=[

                    {"role": "system", "content": "You are a creative storyteller who writes short fiction."},

                    {"role": "user", "content": prompt}

                ],

                temperature=0.9,

                max_tokens=200

            )


            story = response.choices[0].message.content.strip()

            story = textwrap.fill(story, width=80)


            st.subheader("🪄 Your 100-Word Story:")

            st.write(story)


            st.success("Done! You can regenerate by changing the theme.")


No comments: