from dotenv import load_dotenv import os import gradio as gr import openai from fpdf import FPDF from datetime import datetime from gtts import gTTS # Load environment variables from .env load_dotenv() api_key = os.getenv("OPENAI_API_KEY") if not api_key: raise ValueError("❌ OPENAI_API_KEY not found in environment.") openai.api_key = api_key # Directory to save stories story_dir = "storybot_stories" os.makedirs(story_dir, exist_ok=True) def save_story_as_pdf(story_text, character_name=""): date_str = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") file_name = f"{character_name or 'story'}_{date_str}.pdf" file_path = os.path.join(story_dir, file_name) pdf = FPDF() pdf.add_page() pdf.set_font("Arial", 'B', 16) pdf.cell(200, 10, txt=f"{character_name}'s Adventure", ln=True, align='C') pdf.set_font("Arial", size=12) pdf.ln(10) for line in story_text.split('\n'): pdf.multi_cell(0, 10, line) pdf.add_page() pdf.set_font("Arial", 'I', 14) pdf.cell(200, 10, txt="The End", ln=True, align='C') pdf.ln(10) pdf.set_font("Arial", size=10) pdf.cell(200, 10, txt="Created with Magic StoryBot", ln=True, align='C') pdf.output(file_path) return file_path def text_to_speech(text): audio_path = "story_audio.mp3" tts = gTTS(text) tts.save(audio_path) return audio_path def generate_story(age_group, theme, character_name, tone, length): import openai from openai import OpenAI # Reconstruct the prompt length_instruction = { "Short": "about 100 words", "Medium": "about 200 words", "Long": "about 350 words" }.get(length, "about 150 words") system_prompt = f""" You are a children's storyteller. Create a {length_instruction}, age-appropriate story for a child aged {age_group}. Use the theme "{theme}" and the main character's name is "{character_name}". Make the tone of the story {tone.lower()}. End it with the phrase "The End". """ # ⚠️ NEW SDK STYLE: OpenAI client client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) try: response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": "Please generate the story now."} ], temperature=0.7, max_tokens=1024 ) story = response.choices[0].message.content print("✅ Story generated") except Exception as e: import traceback print("❌ LLM ERROR:\n", traceback.format_exc()) story = "An error occurred while generating the story." audio_path = text_to_speech(story) return story, audio_path with gr.Blocks() as app: gr.Markdown("## 📖 Magic StoryBot for Kids") age_group = gr.Dropdown(["1–3", "3–5", "6–8", "9–12"], label="Select Age Group") theme = gr.Dropdown(["Animals", "Adventure", "Magic", "Mystery", "Bedtime"], label="Choose a Story Theme") character_name = gr.Textbox(label="Main Character's Name", placeholder="e.g., Luna, Max") tone = gr.Dropdown(["Happy", "Funny", "Gentle", "Spooky", "Exciting", "Calm"], label="Story Tone") length = gr.Radio(["Short", "Medium", "Long"], label="Story Length") story_output = gr.Textbox(label="Generated Story", lines=10) story_audio = gr.Audio(label="Listen", autoplay=True) pdf_output = gr.File(label="Download Your Story", file_types=[".pdf"], visible=True) def start_story(age, theme, name, tone, length): story, audio = generate_story(age, theme, name, tone, length) return story, audio def export_pdf(story, name): return save_story_as_pdf(story, name) start_btn = gr.Button("🌟 Generate Story") save_btn = gr.Button("📥 Save Story as PDF") start_btn.click(start_story, inputs=[age_group, theme, character_name, tone, length], outputs=[story_output, story_audio]) save_btn.click(export_pdf, inputs=[story_output, character_name], outputs=pdf_output) app.launch()