Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Initialize pipelines (replace model names with ones available on Hugging Face)
|
5 |
+
# Story Generation Pipeline
|
6 |
+
story_generator = pipeline("text-generation", model="gpt2") # GPT-2 for text generation
|
7 |
+
|
8 |
+
# Image Generation Pipeline (placeholder; use a model like Stable Diffusion if available)
|
9 |
+
# Note: As of now, Hugging Face's pipeline doesn't natively support text-to-image, so you may need diffusers library
|
10 |
+
from diffusers import StableDiffusionPipeline
|
11 |
+
image_generator = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
|
12 |
+
image_generator = image_generator.to("cpu") # Use "cuda" if you have a GPU
|
13 |
+
|
14 |
+
# Text-to-Speech Pipeline
|
15 |
+
tts = pipeline("text-to-speech", model="facebook/tts_transformer-en-ljspeech") # English TTS
|
16 |
+
|
17 |
+
def generate_story_image_audio(prompt):
|
18 |
+
"""
|
19 |
+
Generate a story, an image, and audio based on the user's prompt.
|
20 |
+
Args:
|
21 |
+
prompt (str): The input prompt (e.g., "A brave little dragon").
|
22 |
+
Returns:
|
23 |
+
tuple: (story text, image, audio file path).
|
24 |
+
"""
|
25 |
+
# Step 1: Generate the story
|
26 |
+
story_output = story_generator(prompt, max_length=100, num_return_sequences=1, temperature=0.7)
|
27 |
+
story = story_output[0]["generated_text"].strip()
|
28 |
+
|
29 |
+
# Step 2: Generate an image based on the story
|
30 |
+
image = image_generator(story, num_inference_steps=30).images[0] # Generate one image
|
31 |
+
|
32 |
+
# Step 3: Generate audio from the story
|
33 |
+
audio_output = tts(story) # Assuming the model returns audio data
|
34 |
+
audio_path = "story_audio.wav"
|
35 |
+
with open(audio_path, "wb") as f:
|
36 |
+
f.write(audio_output["audio"]) # Save audio to a file
|
37 |
+
|
38 |
+
return story, image, audio_path
|
39 |
+
|
40 |
+
# Create the Gradio interface
|
41 |
+
interface = gr.Interface(
|
42 |
+
fn=generate_story_image_audio,
|
43 |
+
inputs=gr.Textbox(label="Enter a story prompt (e.g., 'A brave little dragon')"),
|
44 |
+
outputs=[
|
45 |
+
gr.Textbox(label="Generated Story"),
|
46 |
+
gr.Image(label="Story Illustration"),
|
47 |
+
gr.Audio(label="Story Narration")
|
48 |
+
],
|
49 |
+
title="Kids' Story Generator",
|
50 |
+
description="Generate a short story, illustration, and audio narration for kids based on your prompt!"
|
51 |
+
)
|
52 |
+
|
53 |
+
# Launch the interface
|
54 |
+
interface.launch()
|