Upload folder using huggingface_hub
Browse files- .DS_Store +0 -0
- README.md +35 -6
- __pycache__/app.cpython-313.pyc +0 -0
- app.py +164 -0
- requirements.txt +3 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
README.md
CHANGED
|
@@ -1,13 +1,42 @@
|
|
| 1 |
---
|
| 2 |
title: Bedtime Story Machine
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
python_version: '3.13'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Bedtime Story Machine
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.29.0
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
short_description: Personalized illustrated bedtime stories for kids
|
| 12 |
+
tags:
|
| 13 |
+
- hackathon
|
| 14 |
+
- thousand-token-wood
|
| 15 |
+
- storytelling
|
| 16 |
+
- children
|
| 17 |
---
|
| 18 |
|
| 19 |
+
# π Bedtime Story Machine
|
| 20 |
+
|
| 21 |
+
A personalized illustrated bedtime story generator, crafted just for your little one.
|
| 22 |
+
|
| 23 |
+
## How it works
|
| 24 |
+
|
| 25 |
+
1. Enter your child's name, age, and pick a theme
|
| 26 |
+
2. AI writes a gentle 4-scene bedtime story with your child as the protagonist
|
| 27 |
+
3. Each scene gets a beautiful watercolor-style illustration
|
| 28 |
+
4. Read it together at bedtime! ποΈ
|
| 29 |
+
|
| 30 |
+
## Models Used (β€32B total)
|
| 31 |
+
|
| 32 |
+
- **Story Generation**: [Qwen/Qwen2.5-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-7B-Instruct) (7B params)
|
| 33 |
+
- **Illustrations**: [black-forest-labs/FLUX.1-schnell](https://huggingface.co/black-forest-labs/FLUX.1-schnell) (12B params)
|
| 34 |
+
- **Total**: ~19B parameters β
|
| 35 |
+
|
| 36 |
+
## Track
|
| 37 |
+
|
| 38 |
+
π **Thousand Token Wood** β Build something delightful that wouldn't exist without AI.
|
| 39 |
+
|
| 40 |
+
## Built for
|
| 41 |
+
|
| 42 |
+
[Build Small Hackathon](https://huggingface.co/build-small-hackathon) by Gradio & Hugging Face
|
__pycache__/app.cpython-313.pyc
ADDED
|
Binary file (8.21 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import json
|
| 4 |
+
import re
|
| 5 |
+
import os
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
# Load .env from parent dir or current dir
|
| 10 |
+
load_dotenv(Path(__file__).resolve().parent.parent / ".env")
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
token = os.environ.get("HF_TOKEN") or os.environ.get("HF_API_KEY")
|
| 14 |
+
|
| 15 |
+
text_client = InferenceClient(provider="together", token=token)
|
| 16 |
+
image_client = InferenceClient(provider="hf-inference", token=token)
|
| 17 |
+
|
| 18 |
+
STORY_MODEL = "Qwen/Qwen2.5-7B-Instruct"
|
| 19 |
+
IMAGE_MODEL = "black-forest-labs/FLUX.1-schnell"
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def generate_story(name, age, theme, extra_details):
|
| 23 |
+
"""Generate a 4-scene bedtime story with image prompts."""
|
| 24 |
+
age = int(age)
|
| 25 |
+
prompt = f"""Write a short bedtime story for a {age}-year-old child named {name}.
|
| 26 |
+
Theme: {theme}
|
| 27 |
+
{f"Extra details: {extra_details}" if extra_details else ""}
|
| 28 |
+
|
| 29 |
+
Requirements:
|
| 30 |
+
- Exactly 4 short scenes (3-4 sentences each)
|
| 31 |
+
- Gentle, calming tone appropriate for bedtime
|
| 32 |
+
- {name} is the main character
|
| 33 |
+
- Happy, peaceful ending where {name} falls asleep or feels safe
|
| 34 |
+
- Age-appropriate vocabulary for a {age}-year-old
|
| 35 |
+
|
| 36 |
+
Return ONLY valid JSON in this exact format, no other text:
|
| 37 |
+
{{"title": "Story Title", "scenes": [{{"text": "Scene text here", "image_prompt": "A children's book illustration of: brief visual description, soft watercolor style, warm colors, whimsical"}}]}}"""
|
| 38 |
+
|
| 39 |
+
response = text_client.chat_completion(
|
| 40 |
+
model=STORY_MODEL,
|
| 41 |
+
messages=[{"role": "user", "content": prompt}],
|
| 42 |
+
max_tokens=1024,
|
| 43 |
+
temperature=0.8,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
content = response.choices[0].message.content.strip()
|
| 47 |
+
# Extract JSON from response
|
| 48 |
+
match = re.search(r'\{.*\}', content, re.DOTALL)
|
| 49 |
+
if match:
|
| 50 |
+
content = match.group()
|
| 51 |
+
return json.loads(content)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def generate_illustration(prompt):
|
| 55 |
+
"""Generate a single illustration."""
|
| 56 |
+
image = image_client.text_to_image(
|
| 57 |
+
prompt=prompt,
|
| 58 |
+
model=IMAGE_MODEL,
|
| 59 |
+
width=768,
|
| 60 |
+
height=512,
|
| 61 |
+
)
|
| 62 |
+
return image
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def create_story(name, age, theme, extra_details, progress=gr.Progress()):
|
| 66 |
+
"""Main function: generate story then illustrations."""
|
| 67 |
+
if not name.strip():
|
| 68 |
+
raise gr.Error("Please enter the child's name!")
|
| 69 |
+
if not theme.strip():
|
| 70 |
+
raise gr.Error("Please choose a theme!")
|
| 71 |
+
|
| 72 |
+
progress(0.1, desc="β¨ Writing your story...")
|
| 73 |
+
story = generate_story(name, age, theme, extra_details)
|
| 74 |
+
|
| 75 |
+
title = story["title"]
|
| 76 |
+
scenes = story["scenes"]
|
| 77 |
+
results = []
|
| 78 |
+
|
| 79 |
+
for i, scene in enumerate(scenes):
|
| 80 |
+
progress((0.2 + i * 0.2), desc=f"π¨ Painting scene {i+1} of {len(scenes)}...")
|
| 81 |
+
image = generate_illustration(scene["image_prompt"])
|
| 82 |
+
results.append((image, scene["text"]))
|
| 83 |
+
|
| 84 |
+
progress(1.0, desc="π Story complete!")
|
| 85 |
+
return title, results
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def build_output(name, age, theme, extra_details):
|
| 89 |
+
"""Build the story and return components for display."""
|
| 90 |
+
title, scenes = create_story(name, age, theme, extra_details)
|
| 91 |
+
|
| 92 |
+
gallery_items = []
|
| 93 |
+
story_text = f"# π {title}\n\n"
|
| 94 |
+
images = []
|
| 95 |
+
|
| 96 |
+
for i, (image, text) in enumerate(scenes):
|
| 97 |
+
story_text += f"**Scene {i+1}:**\n{text}\n\n---\n\n"
|
| 98 |
+
gallery_items.append((image, f"Scene {i+1}"))
|
| 99 |
+
images.append(image)
|
| 100 |
+
|
| 101 |
+
return story_text, images[0] if images else None, images[1] if len(images) > 1 else None, images[2] if len(images) > 2 else None, images[3] if len(images) > 3 else None
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
# --- UI ---
|
| 105 |
+
theme = gr.themes.Soft(
|
| 106 |
+
primary_hue="purple",
|
| 107 |
+
secondary_hue="blue",
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
with gr.Blocks(theme=theme, title="π Bedtime Story Machine") as demo:
|
| 111 |
+
gr.Markdown("""
|
| 112 |
+
# π Bedtime Story Machine
|
| 113 |
+
*A personalized illustrated bedtime story, crafted just for your little one*
|
| 114 |
+
|
| 115 |
+
Enter your child's details and watch as AI weaves a gentle tale with beautiful illustrations.
|
| 116 |
+
""")
|
| 117 |
+
|
| 118 |
+
with gr.Row():
|
| 119 |
+
with gr.Column(scale=1):
|
| 120 |
+
name_input = gr.Textbox(label="Child's Name", placeholder="e.g. Luna")
|
| 121 |
+
age_input = gr.Slider(label="Age", minimum=2, maximum=10, value=5, step=1)
|
| 122 |
+
theme_input = gr.Dropdown(
|
| 123 |
+
label="Story Theme",
|
| 124 |
+
choices=[
|
| 125 |
+
"A magical forest adventure",
|
| 126 |
+
"Friendly dragons and castles",
|
| 127 |
+
"Under the sea with talking fish",
|
| 128 |
+
"A trip to the moon and stars",
|
| 129 |
+
"Friendly animals in a cozy barn",
|
| 130 |
+
"A tiny fairy's garden party",
|
| 131 |
+
"Flying on a cloud to dreamland",
|
| 132 |
+
],
|
| 133 |
+
value="A magical forest adventure",
|
| 134 |
+
allow_custom_value=True,
|
| 135 |
+
)
|
| 136 |
+
extra_input = gr.Textbox(
|
| 137 |
+
label="Extra Details (optional)",
|
| 138 |
+
placeholder="e.g. loves dinosaurs, has a cat named Whiskers",
|
| 139 |
+
)
|
| 140 |
+
generate_btn = gr.Button("β¨ Generate Bedtime Story", variant="primary", size="lg")
|
| 141 |
+
|
| 142 |
+
with gr.Column(scale=2):
|
| 143 |
+
story_output = gr.Markdown(label="Story")
|
| 144 |
+
with gr.Row():
|
| 145 |
+
img1 = gr.Image(label="Scene 1", show_label=True)
|
| 146 |
+
img2 = gr.Image(label="Scene 2", show_label=True)
|
| 147 |
+
with gr.Row():
|
| 148 |
+
img3 = gr.Image(label="Scene 3", show_label=True)
|
| 149 |
+
img4 = gr.Image(label="Scene 4", show_label=True)
|
| 150 |
+
|
| 151 |
+
generate_btn.click(
|
| 152 |
+
fn=build_output,
|
| 153 |
+
inputs=[name_input, age_input, theme_input, extra_input],
|
| 154 |
+
outputs=[story_output, img1, img2, img3, img4],
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
gr.Markdown("""
|
| 158 |
+
---
|
| 159 |
+
*Built with π€ Hugging Face Inference API | Story: Qwen2.5-7B-Instruct | Art: FLUX.1-schnell*
|
| 160 |
+
*For the Build Small Hackathon β Thousand Token Wood track π*
|
| 161 |
+
""")
|
| 162 |
+
|
| 163 |
+
if __name__ == "__main__":
|
| 164 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.29.0
|
| 2 |
+
huggingface_hub==0.30.2
|
| 3 |
+
python-dotenv==1.1.0
|