Update app.py
Browse files
app.py
CHANGED
|
@@ -28,6 +28,10 @@ if 'translation' not in st.session_state:
|
|
| 28 |
st.session_state.translation = None
|
| 29 |
if 'generated_video' not in st.session_state:
|
| 30 |
st.session_state.generated_video = None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Function to convert the audio to MP3 using the external API
|
| 33 |
def convert_to_mp3(audio_file):
|
|
@@ -36,7 +40,7 @@ def convert_to_mp3(audio_file):
|
|
| 36 |
else:
|
| 37 |
# Send to the external converter API
|
| 38 |
url = constants.AUDIO_CONVERTER_ENDPOINT
|
| 39 |
-
files = {"file": (audio_file.name, audio_file, "audio/
|
| 40 |
|
| 41 |
with st.spinner("Converting audio to MP3... Please wait."):
|
| 42 |
response = requests.post(url, files=files)
|
|
@@ -45,9 +49,10 @@ def convert_to_mp3(audio_file):
|
|
| 45 |
# If conversion is successful, save and return the MP3 file
|
| 46 |
converted_file = io.BytesIO(response.content)
|
| 47 |
converted_file.name = "converted.mp3"
|
|
|
|
| 48 |
return converted_file, True # File was converted
|
| 49 |
else:
|
| 50 |
-
st.error("Conversion failed. Please try another format.")
|
| 51 |
return None, None
|
| 52 |
|
| 53 |
# Streamlit UI
|
|
@@ -60,15 +65,15 @@ st.markdown(
|
|
| 60 |
audio_file = st.file_uploader("🔼 Upload your audio file:", type=constants.SUPPORTED_FORMATS)
|
| 61 |
|
| 62 |
if audio_file:
|
| 63 |
-
# Reset states when a new file is uploaded
|
| 64 |
if st.session_state.uploaded_file_name != audio_file.name:
|
| 65 |
st.session_state.uploaded_file_name = audio_file.name
|
| 66 |
st.session_state.converted_audio, st.session_state.was_converted = convert_to_mp3(audio_file)
|
| 67 |
st.session_state.transcript = None
|
| 68 |
st.session_state.translation = None
|
| 69 |
-
st.session_state.
|
|
|
|
| 70 |
|
| 71 |
-
# Display uploaded file name
|
| 72 |
st.info(f"Uploaded file: **{audio_file.name}**")
|
| 73 |
|
| 74 |
if st.session_state.converted_audio:
|
|
@@ -77,77 +82,73 @@ if audio_file:
|
|
| 77 |
else:
|
| 78 |
st.success("✅ File successfully converted to MP3!")
|
| 79 |
|
| 80 |
-
#
|
| 81 |
if st.session_state.transcript is None:
|
| 82 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 83 |
tmp_file.write(st.session_state.converted_audio.read())
|
| 84 |
tmp_file_path = tmp_file.name
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
os.remove(tmp_file_path)
|
| 94 |
|
| 95 |
-
#
|
| 96 |
if st.session_state.transcript and st.session_state.translation is None:
|
| 97 |
-
with st.spinner("Generating translation..."):
|
| 98 |
st.session_state.translation = get_translation(st.session_state.transcript)
|
| 99 |
|
| 100 |
-
# Display and allow playback of the MP3 file
|
| 101 |
st.audio(st.session_state.converted_audio, format="audio/mp3")
|
| 102 |
|
| 103 |
-
# Toggle
|
| 104 |
toggle_transcript = st.checkbox("Show Transcript", value=st.session_state.transcript_visible)
|
|
|
|
| 105 |
|
| 106 |
-
if
|
| 107 |
-
st.session_state.transcript_visible = True
|
| 108 |
st.write("### Transcription:")
|
| 109 |
st.write(st.session_state.transcript)
|
| 110 |
-
else:
|
| 111 |
-
st.session_state.transcript_visible = False
|
| 112 |
|
| 113 |
-
# Toggle
|
| 114 |
toggle_translation = st.checkbox("Show Translation", value=st.session_state.translation_visible)
|
|
|
|
| 115 |
|
| 116 |
-
if
|
| 117 |
-
st.session_state.translation_visible = True
|
| 118 |
st.write("### Translation:")
|
| 119 |
st.write(st.session_state.translation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
else:
|
| 121 |
-
st.session_state.
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
if st.session_state.translation:
|
| 125 |
-
|
| 126 |
-
# Determine whether to use translation or transcription for image generation
|
| 127 |
-
prompts = []
|
| 128 |
-
if 'Already in English' in st.session_state.translation:
|
| 129 |
-
st.info("Audio is Already in English. Using Transcription to generate Image Prompts")
|
| 130 |
-
prompts = get_image_prompts(st.session_state.transcript)['image_prompts']
|
| 131 |
-
else:
|
| 132 |
-
prompts = get_image_prompts(st.session_state.translation)['image_prompts']
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
# Show spinner while generating images
|
| 136 |
-
with st.spinner("Generating images..."):
|
| 137 |
-
for prompt, image_path in generate_images(prompts):
|
| 138 |
-
st.image(image_path, caption=f"{prompt}", use_column_width=True)
|
| 139 |
-
|
| 140 |
-
st.info("Video Generation Feature Currently Under Development")
|
| 141 |
-
# # Generate the video based on the images and translation
|
| 142 |
-
# st.write("### Generating Video...")
|
| 143 |
-
# with st.spinner("Creating video..."):
|
| 144 |
-
# video_file = generate_video(images_folder, st.session_state.translation)
|
| 145 |
-
# if video_file:
|
| 146 |
-
# st.session_state.generated_video = video_file
|
| 147 |
-
# st.video(video_file) # Display the video
|
| 148 |
-
# else:
|
| 149 |
-
# st.error("Failed to generate the video.")
|
| 150 |
|
|
|
|
| 151 |
else:
|
| 152 |
-
# If no file is uploaded yet
|
| 153 |
st.warning("Please upload an audio file to proceed.")
|
|
|
|
|
|
| 28 |
st.session_state.translation = None
|
| 29 |
if 'generated_video' not in st.session_state:
|
| 30 |
st.session_state.generated_video = None
|
| 31 |
+
if 'image_prompts' not in st.session_state:
|
| 32 |
+
st.session_state.image_prompts = None
|
| 33 |
+
if 'generated_images' not in st.session_state:
|
| 34 |
+
st.session_state.generated_images = None
|
| 35 |
|
| 36 |
# Function to convert the audio to MP3 using the external API
|
| 37 |
def convert_to_mp3(audio_file):
|
|
|
|
| 40 |
else:
|
| 41 |
# Send to the external converter API
|
| 42 |
url = constants.AUDIO_CONVERTER_ENDPOINT
|
| 43 |
+
files = {"file": (audio_file.name, audio_file, "audio/mp3")}
|
| 44 |
|
| 45 |
with st.spinner("Converting audio to MP3... Please wait."):
|
| 46 |
response = requests.post(url, files=files)
|
|
|
|
| 49 |
# If conversion is successful, save and return the MP3 file
|
| 50 |
converted_file = io.BytesIO(response.content)
|
| 51 |
converted_file.name = "converted.mp3"
|
| 52 |
+
st.success("✅ File successfully converted to MP3!")
|
| 53 |
return converted_file, True # File was converted
|
| 54 |
else:
|
| 55 |
+
st.error("❌ Conversion failed. Please try another format.")
|
| 56 |
return None, None
|
| 57 |
|
| 58 |
# Streamlit UI
|
|
|
|
| 65 |
audio_file = st.file_uploader("🔼 Upload your audio file:", type=constants.SUPPORTED_FORMATS)
|
| 66 |
|
| 67 |
if audio_file:
|
| 68 |
+
# Reset states only when a new file is uploaded
|
| 69 |
if st.session_state.uploaded_file_name != audio_file.name:
|
| 70 |
st.session_state.uploaded_file_name = audio_file.name
|
| 71 |
st.session_state.converted_audio, st.session_state.was_converted = convert_to_mp3(audio_file)
|
| 72 |
st.session_state.transcript = None
|
| 73 |
st.session_state.translation = None
|
| 74 |
+
st.session_state.image_prompts = None
|
| 75 |
+
st.session_state.generated_images = None # Reset image generation state
|
| 76 |
|
|
|
|
| 77 |
st.info(f"Uploaded file: **{audio_file.name}**")
|
| 78 |
|
| 79 |
if st.session_state.converted_audio:
|
|
|
|
| 82 |
else:
|
| 83 |
st.success("✅ File successfully converted to MP3!")
|
| 84 |
|
| 85 |
+
# Transcription logic
|
| 86 |
if st.session_state.transcript is None:
|
| 87 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 88 |
tmp_file.write(st.session_state.converted_audio.read())
|
| 89 |
tmp_file_path = tmp_file.name
|
| 90 |
|
| 91 |
+
with st.spinner("Transcribing audio... Please wait."):
|
| 92 |
+
result = st.session_state.client.predict(
|
| 93 |
+
param_0=handle_file(tmp_file_path),
|
| 94 |
+
api_name="/predict"
|
| 95 |
+
)
|
| 96 |
+
st.session_state.transcript = clean_response(result)
|
| 97 |
+
os.remove(tmp_file_path)
|
|
|
|
| 98 |
|
| 99 |
+
# Translation logic
|
| 100 |
if st.session_state.transcript and st.session_state.translation is None:
|
| 101 |
+
with st.spinner("Generating translation... Please wait."):
|
| 102 |
st.session_state.translation = get_translation(st.session_state.transcript)
|
| 103 |
|
|
|
|
| 104 |
st.audio(st.session_state.converted_audio, format="audio/mp3")
|
| 105 |
|
| 106 |
+
# Toggle transcript visibility
|
| 107 |
toggle_transcript = st.checkbox("Show Transcript", value=st.session_state.transcript_visible)
|
| 108 |
+
st.session_state.transcript_visible = toggle_transcript
|
| 109 |
|
| 110 |
+
if st.session_state.transcript_visible:
|
|
|
|
| 111 |
st.write("### Transcription:")
|
| 112 |
st.write(st.session_state.transcript)
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
# Toggle translation visibility
|
| 115 |
toggle_translation = st.checkbox("Show Translation", value=st.session_state.translation_visible)
|
| 116 |
+
st.session_state.translation_visible = toggle_translation
|
| 117 |
|
| 118 |
+
if st.session_state.translation_visible:
|
|
|
|
| 119 |
st.write("### Translation:")
|
| 120 |
st.write(st.session_state.translation)
|
| 121 |
+
|
| 122 |
+
# Image generation logic
|
| 123 |
+
if st.session_state.translation and st.session_state.image_prompts is None:
|
| 124 |
+
with st.spinner("Generating image prompts... Please wait."):
|
| 125 |
+
if 'Already in English' in st.session_state.translation:
|
| 126 |
+
st.info("Audio is Already in English. Using Transcription to generate Image Prompts")
|
| 127 |
+
st.session_state.image_prompts = get_image_prompts(st.session_state.transcript)['image_prompts']
|
| 128 |
+
else:
|
| 129 |
+
st.session_state.image_prompts = get_image_prompts(st.session_state.translation)['image_prompts']
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
# Ensure that generated_images is always a list
|
| 133 |
+
if 'generated_images' not in st.session_state or st.session_state.generated_images is None:
|
| 134 |
+
st.session_state.generated_images = []
|
| 135 |
+
|
| 136 |
+
# Generate images only if they have not been generated already
|
| 137 |
+
if st.session_state.image_prompts and not st.session_state.generated_images:
|
| 138 |
+
with st.spinner("Generating images... Please wait."):
|
| 139 |
+
for prompt, image_path in generate_images(st.session_state.image_prompts):
|
| 140 |
+
# Display each image as soon as it's generated
|
| 141 |
+
st.image(image_path, caption=f"{prompt}", use_container_width=True)
|
| 142 |
+
# Append the generated image to the session state
|
| 143 |
+
st.session_state.generated_images.append((prompt, image_path))
|
| 144 |
+
|
| 145 |
+
# Display all previously generated images (including newly generated ones)
|
| 146 |
else:
|
| 147 |
+
for prompt, image_path in st.session_state.generated_images:
|
| 148 |
+
# Display each image
|
| 149 |
+
st.image(image_path, caption=f"{prompt}", use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
st.info("Video Generation Feature Currently Under Development")
|
| 152 |
else:
|
|
|
|
| 153 |
st.warning("Please upload an audio file to proceed.")
|
| 154 |
+
|