Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,10 +12,47 @@ def img2text(image_path):
|
|
| 12 |
return text
|
| 13 |
|
| 14 |
# text2story
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def text2audio(story_text):
|
| 16 |
try:
|
| 17 |
-
# Use
|
| 18 |
-
synthesizer = pipeline("text-to-speech", model="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Limit text length to avoid timeouts
|
| 21 |
max_chars = 500
|
|
@@ -26,19 +63,57 @@ def text2audio(story_text):
|
|
| 26 |
else:
|
| 27 |
story_text = story_text[:max_chars]
|
| 28 |
|
| 29 |
-
# Generate speech
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
# This works because Streamlit's st.audio() can take raw audio data
|
| 36 |
-
return speech
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
st.error(f"Error generating audio: {str(e)}")
|
| 40 |
-
|
| 41 |
-
st.error(traceback.format_exc())
|
| 42 |
return None
|
| 43 |
|
| 44 |
# Function to save temporary image file
|
|
|
|
| 12 |
return text
|
| 13 |
|
| 14 |
# text2story
|
| 15 |
+
def text2story(text):
|
| 16 |
+
# Using a smaller text generation model
|
| 17 |
+
generator = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
| 18 |
+
|
| 19 |
+
# Create a prompt for the story generation
|
| 20 |
+
prompt = f"Write a fun children's story based on this: {text}. Once upon a time, "
|
| 21 |
+
|
| 22 |
+
# Generate the story
|
| 23 |
+
story_result = generator(
|
| 24 |
+
prompt,
|
| 25 |
+
max_length=150,
|
| 26 |
+
num_return_sequences=1,
|
| 27 |
+
temperature=0.7,
|
| 28 |
+
top_k=50,
|
| 29 |
+
top_p=0.95,
|
| 30 |
+
do_sample=True
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Extract the generated text
|
| 34 |
+
story_text = story_result[0]['generated_text']
|
| 35 |
+
story_text = story_text.replace(prompt, "Once upon a time, ")
|
| 36 |
+
|
| 37 |
+
# Make sure the story is at least 100 words
|
| 38 |
+
words = story_text.split()
|
| 39 |
+
if len(words) > 100:
|
| 40 |
+
# Simply truncate to 100 words
|
| 41 |
+
story_text = " ".join(words[:100])
|
| 42 |
+
|
| 43 |
+
return story_text
|
| 44 |
+
|
| 45 |
+
# text2audio - REVISED to correctly handle the audio output
|
| 46 |
def text2audio(story_text):
|
| 47 |
try:
|
| 48 |
+
# Use a different TTS model that works reliably with pipeline
|
| 49 |
+
synthesizer = pipeline("text-to-speech", model="microsoft/speecht5_tts")
|
| 50 |
+
|
| 51 |
+
# Additional input required for this model
|
| 52 |
+
speaker_embeddings = pipeline(
|
| 53 |
+
"audio-classification",
|
| 54 |
+
model="microsoft/speecht5_speaker_embeddings"
|
| 55 |
+
)("some_audio_file.mp3")["logits"]
|
| 56 |
|
| 57 |
# Limit text length to avoid timeouts
|
| 58 |
max_chars = 500
|
|
|
|
| 63 |
else:
|
| 64 |
story_text = story_text[:max_chars]
|
| 65 |
|
| 66 |
+
# Generate speech with correct parameters
|
| 67 |
+
speech = synthesizer(
|
| 68 |
+
text=story_text,
|
| 69 |
+
forward_params={"speaker_embeddings": speaker_embeddings}
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Create a temporary WAV file
|
| 73 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.wav')
|
| 74 |
+
temp_filename = temp_file.name
|
| 75 |
+
temp_file.close()
|
| 76 |
+
|
| 77 |
+
# Display the structure of the speech output for debugging
|
| 78 |
+
st.write(f"Speech output keys: {speech.keys()}")
|
| 79 |
+
|
| 80 |
+
# Save the audio data to the temporary file
|
| 81 |
+
# Different models have different output formats, we'll try common keys
|
| 82 |
+
if 'audio' in speech:
|
| 83 |
+
# Convert numpy array to WAV file
|
| 84 |
+
try:
|
| 85 |
+
import scipy.io.wavfile as wavfile
|
| 86 |
+
wavfile.write(temp_filename, speech['sampling_rate'], speech['audio'])
|
| 87 |
+
except ImportError:
|
| 88 |
+
# If scipy is not available, try raw writing
|
| 89 |
+
with open(temp_filename, 'wb') as f:
|
| 90 |
+
# Convert numpy array to bytes in a simple way
|
| 91 |
+
if isinstance(speech['audio'], np.ndarray):
|
| 92 |
+
audio_bytes = speech['audio'].tobytes()
|
| 93 |
+
f.write(audio_bytes)
|
| 94 |
+
else:
|
| 95 |
+
f.write(speech['audio'])
|
| 96 |
+
elif 'numpy_array' in speech:
|
| 97 |
+
with open(temp_filename, 'wb') as f:
|
| 98 |
+
f.write(speech['numpy_array'].tobytes())
|
| 99 |
+
else:
|
| 100 |
+
# Fallback: try to write whatever is available
|
| 101 |
+
with open(temp_filename, 'wb') as f:
|
| 102 |
+
# Just write the first value that seems like it could be audio data
|
| 103 |
+
for key, value in speech.items():
|
| 104 |
+
if isinstance(value, (bytes, bytearray)) or (
|
| 105 |
+
isinstance(value, np.ndarray) and value.size > 1000):
|
| 106 |
+
if isinstance(value, np.ndarray):
|
| 107 |
+
f.write(value.tobytes())
|
| 108 |
+
else:
|
| 109 |
+
f.write(value)
|
| 110 |
+
break
|
| 111 |
|
| 112 |
+
return temp_filename
|
|
|
|
|
|
|
| 113 |
|
| 114 |
except Exception as e:
|
| 115 |
st.error(f"Error generating audio: {str(e)}")
|
| 116 |
+
# Print all available keys for debugging
|
|
|
|
| 117 |
return None
|
| 118 |
|
| 119 |
# Function to save temporary image file
|