Upload 4 files
Browse files- app.py +67 -0
- client_secret.json +17 -0
- manifest.json +0 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import logging
|
| 5 |
+
import pysrt
|
| 6 |
+
import subprocess
|
| 7 |
+
from pydub import AudioSegment
|
| 8 |
+
from faster_whisper import WhisperModel
|
| 9 |
+
from moviepy.editor import VideoFileClip
|
| 10 |
+
|
| 11 |
+
# Configura logging
|
| 12 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 13 |
+
|
| 14 |
+
# Funzione di utilità per timestamp SRT
|
| 15 |
+
def format_timestamp(seconds):
|
| 16 |
+
ms = int((seconds - int(seconds)) * 1000)
|
| 17 |
+
s = int(seconds)
|
| 18 |
+
h, s = divmod(s, 3600)
|
| 19 |
+
m, s = divmod(s, 60)
|
| 20 |
+
return f"{h:02}:{m:02}:{s:02},{ms:03}"
|
| 21 |
+
|
| 22 |
+
# Trascrizione con faster-whisper
|
| 23 |
+
def transcribe_video(video_path):
|
| 24 |
+
try:
|
| 25 |
+
# Estrai audio dal video
|
| 26 |
+
audio_path = "temp_audio.wav"
|
| 27 |
+
video = VideoFileClip(video_path)
|
| 28 |
+
video.audio.write_audiofile(audio_path, logger=None)
|
| 29 |
+
|
| 30 |
+
# Carica modello Whisper locale
|
| 31 |
+
model = WhisperModel("base")
|
| 32 |
+
segments, _ = model.transcribe(audio_path, beam_size=5)
|
| 33 |
+
|
| 34 |
+
# Scrive file SRT
|
| 35 |
+
srt_filename = "output.srt"
|
| 36 |
+
with open(srt_filename, "w", encoding="utf-8") as f:
|
| 37 |
+
for i, segment in enumerate(segments, 1):
|
| 38 |
+
f.write(f"{i}\n")
|
| 39 |
+
f.write(f"{format_timestamp(segment.start)} --> {format_timestamp(segment.end)}\n")
|
| 40 |
+
f.write(f"{segment.text.strip()}\n\n")
|
| 41 |
+
|
| 42 |
+
with open(srt_filename, "r", encoding="utf-8") as f:
|
| 43 |
+
srt_content = f.read()
|
| 44 |
+
|
| 45 |
+
return srt_content, srt_filename
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
logging.error(f"Errore durante la trascrizione: {e}")
|
| 49 |
+
return f"Errore: {e}", None
|
| 50 |
+
finally:
|
| 51 |
+
if os.path.exists("temp_audio.wav"):
|
| 52 |
+
os.remove("temp_audio.wav")
|
| 53 |
+
|
| 54 |
+
# Interfaccia Gradio
|
| 55 |
+
interface = gr.Interface(
|
| 56 |
+
fn=transcribe_video,
|
| 57 |
+
inputs=gr.File(label="Carica un video"),
|
| 58 |
+
outputs=[
|
| 59 |
+
gr.Textbox(label="Contenuto SRT", interactive=True, lines=20),
|
| 60 |
+
gr.File(label="Scarica il file SRT")
|
| 61 |
+
],
|
| 62 |
+
title="Estrattore Sottotitoli",
|
| 63 |
+
description="Carica un video per generare automaticamente sottotitoli in formato SRT con Whisper."
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
interface.launch(share=True)
|
client_secret.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"openai_api_key": "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",
|
| 3 |
+
"web": {
|
| 4 |
+
"client_id": "777046606431-40g5enf1s9bm6h6qf28505g23t8bgj3u.apps.googleusercontent.com",
|
| 5 |
+
"project_id": "dzextractaudio",
|
| 6 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
| 7 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
| 8 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
| 9 |
+
"client_secret": "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",
|
| 10 |
+
"redirect_uris": [
|
| 11 |
+
"http://localhost:8080","http://127.0.0.1:7861","https://5bee1832de22f43c3f.gradio.live"
|
| 12 |
+
],
|
| 13 |
+
"javascript_origins": [
|
| 14 |
+
"http://localhost:8080","http://127.0.0.1:7861","https://5bee1832de22f43c3f.gradio.live"
|
| 15 |
+
]
|
| 16 |
+
}
|
| 17 |
+
}
|
manifest.json
ADDED
|
File without changes
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
moviepy
|
| 3 |
+
pysrt
|
| 4 |
+
pydub
|
| 5 |
+
faster-whisper
|