Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
def text_to_srt(text):
|
| 5 |
+
lines = text.split('\n')
|
| 6 |
+
srt_content = ""
|
| 7 |
+
for i, line in enumerate(lines):
|
| 8 |
+
if line.strip() == "":
|
| 9 |
+
continue
|
| 10 |
+
try:
|
| 11 |
+
times, content = line.split(']', 1)
|
| 12 |
+
start, end = times[1:].split(' -> ')
|
| 13 |
+
# Vérifier si l'horodatage inclut les heures; sinon, préfixer par "00:"
|
| 14 |
+
if start.count(":") == 1:
|
| 15 |
+
start = "00:" + start
|
| 16 |
+
if end.count(":") == 1:
|
| 17 |
+
end = "00:" + end
|
| 18 |
+
# Remplacer '.' par ',' dans les horodatages pour le format SRT
|
| 19 |
+
srt_content += f"{i+1}\n{start.replace('.', ',')} --> {end.replace('.', ',')}\n{content.strip()}\n\n"
|
| 20 |
+
except ValueError:
|
| 21 |
+
continue # Ignorer les lignes qui ne correspondent pas au format attendu
|
| 22 |
+
# Enregistrer le contenu SRT dans un fichier temporaire
|
| 23 |
+
temp_file_path = 'output.srt'
|
| 24 |
+
with open(temp_file_path, 'w', encoding='utf-8') as file:
|
| 25 |
+
file.write(srt_content)
|
| 26 |
+
return temp_file_path
|
| 27 |
+
|
| 28 |
+
def save_temp_file(content, filename="output.srt"):
|
| 29 |
+
with open(filename, "w") as f:
|
| 30 |
+
f.write(content)
|
| 31 |
+
return filename
|
| 32 |
+
|
| 33 |
+
# Application Streamlit
|
| 34 |
+
st.title("Convertisseur de Texte en SRT")
|
| 35 |
+
|
| 36 |
+
text = st.text_area("🗯️ Entrez le texte de [Whisper](https://huggingface.co/spaces/sanchit-gandhi/whisper-jax)", height=300)
|
| 37 |
+
if st.button("📁 Convertir en SRT"):
|
| 38 |
+
if text:
|
| 39 |
+
srt_path = text_to_srt(text)
|
| 40 |
+
with open(srt_path, "r") as file:
|
| 41 |
+
st.download_button(label="⬇️ Télécharger le fichier SRT", data=file, file_name="output.srt", mime="text/plain")
|
| 42 |
+
else:
|
| 43 |
+
st.warning("🔴 Veuillez entrer du texte.")
|