Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,14 +5,19 @@ from langchain.chains import LLMChain
|
|
5 |
from openai import OpenAI
|
6 |
import os
|
7 |
import re
|
|
|
|
|
|
|
|
|
8 |
|
|
|
9 |
# Initialize OpenAI client
|
10 |
client = OpenAI()
|
11 |
|
12 |
# Create a prompt template
|
13 |
template = """
|
14 |
-
You are a creative
|
15 |
-
Divide the
|
16 |
|
17 |
Topic: {topic}
|
18 |
|
@@ -30,7 +35,7 @@ def generate_image(prompt):
|
|
30 |
response = client.images.generate(
|
31 |
model="dall-e-3",
|
32 |
prompt=prompt,
|
33 |
-
size="
|
34 |
quality="standard",
|
35 |
n=1,
|
36 |
)
|
@@ -39,7 +44,43 @@ def generate_image(prompt):
|
|
39 |
print(f"Error generating image: {e}")
|
40 |
return None
|
41 |
|
42 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
# Generate the story
|
44 |
story = story_chain.run(topic)
|
45 |
|
@@ -51,40 +92,50 @@ def generate_story_with_images(topic):
|
|
51 |
while len(paragraphs) < 3:
|
52 |
paragraphs.append("...")
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
return
|
61 |
|
62 |
# Create the Gradio interface
|
63 |
def gradio_interface(topic):
|
64 |
try:
|
65 |
-
|
66 |
-
return
|
67 |
-
paragraphs[0], image_urls[0],
|
68 |
-
paragraphs[1], image_urls[1],
|
69 |
-
paragraphs[2], image_urls[2]
|
70 |
-
]
|
71 |
except Exception as e:
|
72 |
print(f"Error in gradio_interface: {e}")
|
73 |
-
return
|
74 |
|
75 |
iface = gr.Interface(
|
76 |
fn=gradio_interface,
|
77 |
inputs=gr.Textbox(lines=2, placeholder="Enter a topic for the story..."),
|
78 |
outputs=[
|
79 |
-
gr.Textbox(label="
|
80 |
-
gr.
|
81 |
-
gr.Textbox(label="Paragraph 2"),
|
82 |
-
gr.Image(label="Image 2", type="filepath"),
|
83 |
-
gr.Textbox(label="Paragraph 3"),
|
84 |
-
gr.Image(label="Image 3", type="filepath"),
|
85 |
],
|
86 |
-
title="Story Generator with
|
87 |
-
description="Enter a topic, and the AI will generate a short story with
|
88 |
)
|
89 |
|
90 |
# Launch the Gradio app
|
|
|
5 |
from openai import OpenAI
|
6 |
import os
|
7 |
import re
|
8 |
+
from pathlib import Path
|
9 |
+
from moviepy.editor import *
|
10 |
+
import requests
|
11 |
+
import tempfile
|
12 |
|
13 |
+
|
14 |
# Initialize OpenAI client
|
15 |
client = OpenAI()
|
16 |
|
17 |
# Create a prompt template
|
18 |
template = """
|
19 |
+
You are a creative teacher exclusively for kids. Given a topic, write a tutorial-based story of about 150 words teaching the child about the topic.
|
20 |
+
Divide the tutorial into 3 paragraphs. Each paragraph should be a distinct part of the tutorial.
|
21 |
|
22 |
Topic: {topic}
|
23 |
|
|
|
35 |
response = client.images.generate(
|
36 |
model="dall-e-3",
|
37 |
prompt=prompt,
|
38 |
+
size="256x256",
|
39 |
quality="standard",
|
40 |
n=1,
|
41 |
)
|
|
|
44 |
print(f"Error generating image: {e}")
|
45 |
return None
|
46 |
|
47 |
+
def generate_speech(text, filename):
|
48 |
+
try:
|
49 |
+
response = client.audio.speech.create(
|
50 |
+
model="tts-1",
|
51 |
+
voice="alloy",
|
52 |
+
input=text
|
53 |
+
)
|
54 |
+
response.stream_to_file(filename)
|
55 |
+
return filename
|
56 |
+
except Exception as e:
|
57 |
+
print(f"Error generating speech: {e}")
|
58 |
+
return None
|
59 |
+
|
60 |
+
def download_image(url, filename):
|
61 |
+
response = requests.get(url)
|
62 |
+
with open(filename, 'wb') as f:
|
63 |
+
f.write(response.content)
|
64 |
+
|
65 |
+
def create_video(paragraphs, image_files, audio_files, output_file):
|
66 |
+
clips = []
|
67 |
+
for paragraph, image_file, audio_file in zip(paragraphs, image_files, audio_files):
|
68 |
+
audio_clip = AudioFileClip(audio_file)
|
69 |
+
duration = audio_clip.duration
|
70 |
+
|
71 |
+
image_clip = ImageClip(image_file).set_duration(duration)
|
72 |
+
|
73 |
+
text_clip = TextClip(paragraph, fontsize=30, color='white', bg_color='black',
|
74 |
+
size=(1024, 200), method='caption').set_position(('center', 'bottom')).set_duration(duration)
|
75 |
+
|
76 |
+
composite_clip = CompositeVideoClip([image_clip, text_clip])
|
77 |
+
composite_clip = composite_clip.set_audio(audio_clip)
|
78 |
+
clips.append(composite_clip)
|
79 |
+
|
80 |
+
final_clip = concatenate_videoclips(clips)
|
81 |
+
final_clip.write_videofile(output_file, fps=24)
|
82 |
+
|
83 |
+
def generate_story_with_video(topic):
|
84 |
# Generate the story
|
85 |
story = story_chain.run(topic)
|
86 |
|
|
|
92 |
while len(paragraphs) < 3:
|
93 |
paragraphs.append("...")
|
94 |
|
95 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
96 |
+
image_files = []
|
97 |
+
audio_files = []
|
98 |
+
|
99 |
+
for i, paragraph in enumerate(paragraphs):
|
100 |
+
# Generate and download image
|
101 |
+
image_url = generate_image(paragraph)
|
102 |
+
image_file = os.path.join(temp_dir, f"image_{i}.png")
|
103 |
+
download_image(image_url, image_file)
|
104 |
+
image_files.append(image_file)
|
105 |
+
|
106 |
+
# Generate audio
|
107 |
+
audio_file = os.path.join(temp_dir, f"audio_{i}.mp3")
|
108 |
+
generate_speech(paragraph, audio_file)
|
109 |
+
audio_files.append(audio_file)
|
110 |
+
|
111 |
+
# Create video
|
112 |
+
output_file = os.path.join(temp_dir, "story_video.mp4")
|
113 |
+
create_video(paragraphs, image_files, audio_files, output_file)
|
114 |
+
|
115 |
+
# Read the video file
|
116 |
+
with open(output_file, "rb") as f:
|
117 |
+
video_data = f.read()
|
118 |
|
119 |
+
return story, video_data
|
120 |
|
121 |
# Create the Gradio interface
|
122 |
def gradio_interface(topic):
|
123 |
try:
|
124 |
+
story, video_data = generate_story_with_video(topic)
|
125 |
+
return story, video_data
|
|
|
|
|
|
|
|
|
126 |
except Exception as e:
|
127 |
print(f"Error in gradio_interface: {e}")
|
128 |
+
return "An error occurred while generating the story and video.", None
|
129 |
|
130 |
iface = gr.Interface(
|
131 |
fn=gradio_interface,
|
132 |
inputs=gr.Textbox(lines=2, placeholder="Enter a topic for the story..."),
|
133 |
outputs=[
|
134 |
+
gr.Textbox(label="Generated Story"),
|
135 |
+
gr.Video(label="Story Video")
|
|
|
|
|
|
|
|
|
136 |
],
|
137 |
+
title="Story Generator with Video",
|
138 |
+
description="Enter a topic, and the AI will generate a short story with a video combining images, captions, and narration."
|
139 |
)
|
140 |
|
141 |
# Launch the Gradio app
|