AI Story Generator using Python

AI Story Generator using Python Thumbnail

AI Story Generator using Python

Creating your own AI-powered story generator is a fun and practical project. In this tutorial, we will build a desktop application that takes a topic as input and generates a creative story instantly.

We will use:

  • Python 🐍
  • Tkinter (for GUI)
  • OpenAI API (or placeholder logic if offline)

Features of the App

  • Simple and clean GUI 🎨
  • Input field for story topic
  • Generate AI story in one click
  • Scrollable output area
  • Copy story option

Requirements

Install dependencies:
Open terminal / command prompt and run:
pip install openai
Install dependencies openai thumbnail
Full Python Source Code
import tkinter as tk
from tkinter import scrolledtext, messagebox
import openai

# 🔐 Add your OpenAI API Key here
openai.api_key = "YOUR_API_KEY_HERE"

# Function to generate story
def generate_story():
    topic = topic_entry.get()

    if not topic.strip():
        messagebox.showwarning("Input Error", "Please enter a topic!")
        return

    try:
        response = openai.ChatCompletion.create(
            model="gpt-4o-mini",
            messages=[
                {"role": "system", "content": "You are a creative story writer."},
                {"role": "user", "content": f"Write a short, engaging story about {topic}."}
            ],
            max_tokens=300
        )

        story = response['choices'][0]['message']['content']

        output_text.delete(1.0, tk.END)
        output_text.insert(tk.END, story)

    except Exception as e:
        messagebox.showerror("Error", str(e))

# Copy story
def copy_story():
    root.clipboard_clear()
    root.clipboard_append(output_text.get(1.0, tk.END))
    messagebox.showinfo("Copied", "Story copied to clipboard!")

# GUI Setup
root = tk.Tk()
root.title("AI Story Generator")
root.geometry("600x500")
root.resizable(False, False)

# Title Label
title_label = tk.Label(root, text="AI Story Generator", font=("Arial", 18, "bold"))
title_label.pack(pady=10)

# Topic Entry
topic_entry = tk.Entry(root, width=50, font=("Arial", 12))
topic_entry.pack(pady=10)
topic_entry.insert(0, "Enter your story topic...")

# Generate Button
generate_btn = tk.Button(root, text="Generate Story", command=generate_story, bg="#4CAF50", fg="white")
generate_btn.pack(pady=5)

# Output Text Area
output_text = scrolledtext.ScrolledText(root, wrap=tk.WORD, width=70, height=15)
output_text.pack(pady=10)

# Copy Button
copy_btn = tk.Button(root, text="Copy Story", command=copy_story, bg="#2196F3", fg="white")
copy_btn.pack(pady=5)

# Run App
root.mainloop()
AI Generate Story Thumbnail

How It Works

  • User enters a topic
  • Clicks Generate Story
  • API sends request to AI model
  • AI generates story
  • Output displayed in text area

🔒 AdSense Safety Tips

  • Avoid auto-generated spam content
  • Add user interaction (like input field ✔️)
  • Include educational explanation (✔️ this tutorial)
  • Do not mass-generate pages automatically

Optional Improvements

You can upgrade this project by adding:
  • 🌙 Dark Mode toggle
  • 💾 Save story as TXT file
  • 🌐 Multi-language support
  • 🎭 Genre selection (Horror, Comedy, Sci-Fi)
  • 🧠 Offline AI using local models

❓ FAQ

1. Is this project beginner-friendly?

Yes, it uses basic Python and Tkinter.

2. Can I run it without internet?

Only if you replace API with offline logic.

3. Is it safe for blogging?

Yes, this is AdSense-safe educational content.

4. Which Python version is required?

Python 3.8+

🎯 Conclusion

You now have a fully working AI Story Generator Desktop App. This project is perfect for:
  • Beginners learning Python GUI
  • Bloggers creating tech content
  • Developers exploring AI apps
Previous Post Next Post

Contact Form