File size: 5,062 Bytes
fac06d0
 
 
326258c
20fa434
e4297a8
de84263
 
4b85b27
 
200541c
fac06d0
326258c
fac06d0
 
8dffbd8
042a04d
200541c
 
02cd7fa
 
 
 
6319f19
4c56b36
 
de84263
4c56b36
 
 
20fa434
4c56b36
 
 
 
20fa434
4c56b36
 
 
de84263
4c56b36
 
 
4b85b27
4c56b36
 
 
 
 
 
 
 
de84263
4c56b36
 
 
02cd7fa
 
 
4c56b36
1936f1e
 
 
 
326258c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48d7752
326258c
 
 
 
 
 
 
262c6d5
 
 
 
 
 
 
 
 
 
270a894
 
262c6d5
326258c
 
 
 
 
 
 
 
4969c68
326258c
 
8dffbd8
326258c
 
8dffbd8
 
b44d1eb
8dffbd8
326258c
 
 
eecd1a3
8dffbd8
 
326258c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
534a7d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from transformers import pipeline
import gradio as gr
import time
from video_downloader import download_video, download_video1, download_youtube_video
from moviepy.editor import AudioFileClip, VideoFileClip
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
import datetime
import os
from pydub import AudioSegment
from pydub.silence import split_on_silence
import re

pipe = pipeline("automatic-speech-recognition", model="Artanis1551/whisper_romanian")


def process_video1(date):
    # If the date is not in YYYY-MM-DD format, return an error message
    date_pattern = re.compile(r"\b\d{4}-\d{2}-\d{2}\b")
    if not date_pattern.match(date):
        video_path = download_youtube_video(
            "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
        )
        transcription = "Please enter a date in the format YYYY-MM-DD."
        return video_path, transcription
    try:
        video_path = download_video1(date)

        # Get the duration of the video
        video = VideoFileClip(video_path)
        duration = video.duration

        # If the video is longer than 30 seconds, only take the first 30 seconds
        if duration > 30:
            video_path = f"short_{date}.mp4"
            ffmpeg_extract_subclip(video_path, 0, 30, targetname=video_path)

        # Extract audio from the video
        audio_path = f"audio_{date}.wav"
        AudioFileClip(video_path).write_audiofile(audio_path)

        # Split the audio into chunks
        audio = AudioSegment.from_wav(audio_path)
        chunks = split_on_silence(audio, min_silence_len=500, silence_thresh=-40)

        # Transcribe each chunk
        transcription = ""
        for i, chunk in enumerate(chunks):
            chunk.export(f"chunk{i}.wav", format="wav")
            with open(f"chunk{i}.wav", "rb") as audio_file:
                audio = audio_file.read()
            transcription += pipe(audio)["text"] + "\n "
            os.remove(f"chunk{i}.wav")

        # Remove the audio file
        os.remove(audio_path)
    except:
        video_path = download_youtube_video(
            "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
        )
        transcription = "No decision was made on this date."

    return video_path, transcription


def process_video(date):
    # If the date is not in YYYY-MM-DD format, return an error message
    date_pattern = re.compile(r"\b\d{4}\d{2}\d{2}\b")
    if not date_pattern.match(date):
        video_path = download_youtube_video(
            "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
        )
        transcription = "Please enter a date in the format YYYY-MM-DD."
        return video_path, transcription
    try:
        # Download the video
        video_path = download_video(date)

        # Extract the first 30 seconds of the video
        short_video_path = f"short_{date}.mp4"
        ffmpeg_extract_subclip(video_path, 0, 30, targetname=short_video_path)
        video_path = short_video_path

        # Extract audio from the short video
        audio_path = f"audio_{date}.wav"
        AudioFileClip(short_video_path).write_audiofile(audio_path)

        # Split the audio into chunks
        audio = AudioSegment.from_wav(audio_path)
        # chunks = split_on_silence(audio, min_silence_len=500, silence_thresh=-40)

        # # Transcribe each chunk
        # transcription = ""
        # for i, chunk in enumerate(chunks):
        #     chunk.export(f"chunk{i}.wav", format="wav")
        #     with open(f"chunk{i}.wav", "rb") as audio_file:
        #         audio = audio_file.read()
        #     transcription += pipe(audio)["text"] + " "
        #     os.remove(f"chunk{i}.wav")
        with open(audio_path, "rb") as audio_file:
            audio = audio_file.read()
        transcription = pipe(audio)["text"]
        # Remove the audio file
        os.remove(audio_path)
    except:
        video_path = download_youtube_video(
            "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
        )
        transcription = "No decision was made on this date."

    return video_path, transcription


iface = gr.Interface(
    fn=process_video,
    inputs=gr.inputs.Textbox(label="Date with format YYYYMMDD"),
    outputs=[
        gr.outputs.Video(),
        gr.Textbox(lines=100, max_lines=100, interactive=True),
    ],
    title="Romanian Transcription Test",
    description="This app transcribes videos from the Romanian Parliament"
    + " on a given date. Only the first 30 seconds of the "
    + "video will be used if it is longer than that.",
)


# iface = gr.Interface(
#     fn=process_video1,
#     inputs=[
#         gr.inputs.Textbox(label="Date with format YYYY-MM-DD"),
#     ],
#     outputs=[
#         gr.outputs.Video(),
#         gr.Textbox(lines=100, max_lines=100, interactive=True),
#     ],
#     title="Transcribe Swedish Parliament Decisions",
# description="This app transcribes the top Swedish Parliament decision"
# + " video from the given date. Only the first 30 seconds of the "
# + "video will be used if it is longer than that.",
# )

iface.launch()