File size: 2,173 Bytes
ac9b2c3
 
fb35ac4
e4d4883
b829c1f
0af678d
c20e63c
ac9b2c3
 
fb35ac4
5f40cee
0af678d
1de6508
 
fb35ac4
a8406c7
6ca81bc
 
fb35ac4
 
323553c
0af678d
aba4a75
f25d1de
 
 
 
 
ac9b2c3
5f40cee
f25d1de
5f40cee
f25d1de
 
5f40cee
 
e32a8a0
5f40cee
 
 
 
a8406c7
 
 
 
 
 
 
 
 
 
 
 
 
 
ac9b2c3
 
 
 
1ac7142
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
import gradio as gr
import re
import sys
import glob
import os
from io import StringIO
from openbb_terminal.forecast.whisper_model import transcribe_and_summarize

def get_video_id(url):

    video_id = re.findall(r"v=([\w]{11})", url)[0]
    old_stdin = sys.stdin
    # mkdir /home/user/.cache/whisper
    os.makedirs(f"/home/user/.cache/whisper", exist_ok=True)
    if not sys.stdin.isatty():
        # seems I only need 3 y's to get past the prompt, but 5 is safer
        y_strings = "\n".join(["y", "y", "y", "y", "y"])
        sys.stdin = StringIO(y_strings)
        transcribe_and_summarize(video=url, output_dir=video_id)
    else:
        return "Please enter a YouTube URL"
    sys.stdin = old_stdin
    print(f"Video ID: {video_id}")
    try:
        summary_file = glob.glob(f"{video_id}/*_summary.txt")[0]
    except Exception as e:
        # get latest file with *_summary.txt
        summary_file = max(glob.glob(f"**/*_summary.txt"), key=os.path.getctime)
    # file .srt file
    subtitle_file = None
    try:
        subtitle_file = glob.glob(f"{video_id}/*.vtt")[0]
    except Exception as e:
        # get latest file with .srt or .vtt
        subtitle_file = max(glob.glob(f"**/*.vtt"), key=os.path.getctime)

    if subtitle_file is None:
        try:
            subtitle_file = glob.glob(f"{video_id}/*.srt")[0]
        except Exception as e:
            subtitle_file = max(glob.glob(f"**/*.srt"), key=os.path.getctime)

    # returns contents of summary file and subtitle file
    try:
        with open(summary_file, "r") as f:
            summary_contents = f.read()
    except Exception as e:
        summary_contents = "No summary file found"

    try:
        with open(subtitle_file, "r") as f:
            subtitle_contents = f.read()
    except Exception as e:
        subtitle_contents = "No subtitle file found"
    return summary_contents, subtitle_contents

input_text = gr.inputs.Textbox(label="Enter a YouTube URL")
output_text = [gr.outputs.Textbox(label="Summary File"), gr.outputs.Textbox(label="Subtitle File")]

gr.Interface(fn=get_video_id, inputs=input_text, outputs=output_text, title="YouTube Video Summarization").launch()