Spaces:
Sleeping
Sleeping
File size: 4,175 Bytes
688b946 d31e1fe 688b946 d31e1fe 688b946 d31e1fe 688b946 d31e1fe a6ea5eb 688b946 d31e1fe 688b946 c88aaf1 688b946 c88aaf1 688b946 c88aaf1 688b946 c88aaf1 688b946 d31e1fe 688b946 d31e1fe 688b946 c88aaf1 d31e1fe 688b946 d31e1fe 688b946 c88aaf1 688b946 d31e1fe 688b946 d31e1fe 688b946 d31e1fe 688b946 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
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()
|