Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ from pathlib import Path
|
|
9 |
from openai import OpenAI
|
10 |
import tempfile
|
11 |
import shutil
|
|
|
12 |
|
13 |
# Initialize OpenAI clients
|
14 |
llm = LangChainOpenAI(temperature=0.7)
|
@@ -103,6 +104,14 @@ def generate_speech(text, filename):
|
|
103 |
print(f"Error generating speech: {e}")
|
104 |
return None
|
105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
def generate_story_and_music(topic):
|
107 |
with tempfile.TemporaryDirectory() as temp_dir:
|
108 |
try:
|
@@ -136,9 +145,13 @@ def generate_story_and_music(topic):
|
|
136 |
try:
|
137 |
music_response = query(music_payload, config)
|
138 |
music_audio, music_sample_rate = process_audio(music_response)
|
|
|
|
|
|
|
|
|
139 |
except Exception as e:
|
140 |
-
print(f"Error generating music: {e}")
|
141 |
-
music_audio, music_sample_rate = None, None
|
142 |
|
143 |
# Generate speech for the entire story
|
144 |
speech_file = os.path.join(temp_dir, "speech.mp3")
|
@@ -151,21 +164,13 @@ def generate_story_and_music(topic):
|
|
151 |
else:
|
152 |
speech_file = None
|
153 |
|
154 |
-
return story, final_music_prompt,
|
155 |
|
156 |
# Create the Gradio interface
|
157 |
def gradio_interface(topic):
|
158 |
-
story, music_prompt,
|
159 |
-
|
160 |
-
outputs = [story, music_prompt]
|
161 |
-
|
162 |
-
if music_audio is not None and music_sample_rate is not None:
|
163 |
-
outputs.append((music_sample_rate, music_audio))
|
164 |
-
else:
|
165 |
-
outputs.append(None)
|
166 |
|
167 |
-
|
168 |
-
outputs.append(speech_file)
|
169 |
|
170 |
return outputs
|
171 |
|
@@ -175,11 +180,11 @@ iface = gr.Interface(
|
|
175 |
outputs=[
|
176 |
gr.Textbox(label="Generated African-based Story"),
|
177 |
gr.Textbox(label="Generated Music Prompt"),
|
178 |
-
gr.Audio(label="Generated Music", type="
|
179 |
gr.Audio(label="Story Speech")
|
180 |
],
|
181 |
title="African Story and Music Generator with Speech",
|
182 |
-
description="Enter a topic, and the AI will generate a short African-based story, matching background music, and speech for the entire story.
|
183 |
)
|
184 |
|
185 |
# Launch the Gradio app
|
|
|
9 |
from openai import OpenAI
|
10 |
import tempfile
|
11 |
import shutil
|
12 |
+
from scipy.io import wavfile
|
13 |
|
14 |
# Initialize OpenAI clients
|
15 |
llm = LangChainOpenAI(temperature=0.7)
|
|
|
104 |
print(f"Error generating speech: {e}")
|
105 |
return None
|
106 |
|
107 |
+
def save_wav(audio, sample_rate, filename):
|
108 |
+
try:
|
109 |
+
wavfile.write(filename, sample_rate, audio)
|
110 |
+
return filename
|
111 |
+
except Exception as e:
|
112 |
+
print(f"Error saving WAV file: {e}")
|
113 |
+
return None
|
114 |
+
|
115 |
def generate_story_and_music(topic):
|
116 |
with tempfile.TemporaryDirectory() as temp_dir:
|
117 |
try:
|
|
|
145 |
try:
|
146 |
music_response = query(music_payload, config)
|
147 |
music_audio, music_sample_rate = process_audio(music_response)
|
148 |
+
|
149 |
+
# Save music as WAV file
|
150 |
+
music_file = "generated_music.wav"
|
151 |
+
save_wav(music_audio, music_sample_rate, music_file)
|
152 |
except Exception as e:
|
153 |
+
print(f"Error generating or saving music: {e}")
|
154 |
+
music_audio, music_sample_rate, music_file = None, None, None
|
155 |
|
156 |
# Generate speech for the entire story
|
157 |
speech_file = os.path.join(temp_dir, "speech.mp3")
|
|
|
164 |
else:
|
165 |
speech_file = None
|
166 |
|
167 |
+
return story, final_music_prompt, music_file, speech_file
|
168 |
|
169 |
# Create the Gradio interface
|
170 |
def gradio_interface(topic):
|
171 |
+
story, music_prompt, music_file, speech_file = generate_story_and_music(topic)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
+
outputs = [story, music_prompt, music_file, speech_file]
|
|
|
174 |
|
175 |
return outputs
|
176 |
|
|
|
180 |
outputs=[
|
181 |
gr.Textbox(label="Generated African-based Story"),
|
182 |
gr.Textbox(label="Generated Music Prompt"),
|
183 |
+
gr.Audio(label="Generated Music", type="filepath"),
|
184 |
gr.Audio(label="Story Speech")
|
185 |
],
|
186 |
title="African Story and Music Generator with Speech",
|
187 |
+
description="Enter a topic, and the AI will generate a short African-based story, matching background music, and speech for the entire story. If any part fails, you'll still see the successful parts."
|
188 |
)
|
189 |
|
190 |
# Launch the Gradio app
|