File size: 3,546 Bytes
b7f7ba0
 
 
 
 
71462c8
b7f7ba0
8f01796
ed861c2
 
 
 
 
 
 
 
 
 
 
b7f7ba0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc33328
 
b7f7ba0
 
fc33328
b7f7ba0
 
 
 
 
 
 
 
 
 
 
 
 
 
f720ca9
 
b7f7ba0
 
 
 
 
44698f3
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
import gradio as gr
import yt_dlp
from collections import defaultdict
import tempfile
import google.generativeai as genai
import os

prompt_filepath = "./system_instructions.txt"
def read_system_instruction_from_file(filepath):
  try:
    with open(filepath, 'r') as f:
      system_instruction = f.read().strip()
      return system_instruction
  except FileNotFoundError:
    return "Error: System instruction file not found."
  except Exception as e:
    return f"Error reading system instruction file: {str(e)}"

      
def download_and_convert_subtitles(url):
  """Downloads VTT subtitles from a YouTube URL, extracts text, and returns it.

  Args:
      url: The URL of the YouTube video.

  Returns:
      A string containing the extracted subtitles text or an error message.
  """

  # Download subtitles using yt-dlp with temporary directory
  with tempfile.TemporaryDirectory() as download_dir:
    ydl_opts = {
        'skip_download': True,
        'writeautomaticsub': True,
        'subtitlesformat': 'vtt',
        'outtmpl': f'{download_dir}/%(id)s.%(ext)s',
    }

    try:
      # Use context manager properly (Option 1)
      with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])  # Download only subtitles

      # Extract video ID and handle language code extension
      info = ydl.extract_info(url, download=False)
      video_id = info['id']

      # Extract text from downloaded VTT file (in temp directory)
      vtt_file_path = f"{download_dir}/{video_id}.{info.get('language', '')}.vtt"
      return extract_text_from_vtt(vtt_file_path)

    except Exception as e:
      return f"Error downloading and converting subtitles: {str(e)}"


def extract_text_from_vtt(vtt_file_path):
  """Extracts text content from a VTT file.

  Args:
      vtt_file_path: The path to the VTT file (in temporary directory).

  Returns:
      A string containing all text lines from the VTT file.
  """

  text_lines = []
  with open(vtt_file_path, 'r', encoding='utf-8') as f:
    for line in f:
      if not line.strip() or '-->' in line:
        continue
      text = line.strip().split("<", 1)[0]  # Extract text content
      text_lines.append(text)
  
  user_prompt = "\n".join(text_lines)
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
  generation_config = {
  "temperature": 1,
  "top_p": 0.95,
  "top_k": 0,
  "max_output_tokens": 8192,
}
  safety_settings = [
  {
    "category": "HARM_CATEGORY_HARASSMENT",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE"
  },
  {
    "category": "HARM_CATEGORY_HATE_SPEECH",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE"
  },
  {
    "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE"
  },
  {
    "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE"
  },
]
  a = read_system_instruction_from_file(prompt_filepath)
    
  model = genai.GenerativeModel(model_name="gemini-1.5-pro-latest",
                              generation_config=generation_config,
                              system_instruction=a,
                              safety_settings=safety_settings)
  convo = model.start_chat(history=[
])
  convo.send_message(user_prompt)
  ai_model_output = convo.last.text

  return ai_model_output


# Gradio interface definition
interface = gr.Interface(
  fn=download_and_convert_subtitles,
  inputs="text",
  outputs="text",
  title="Podcast Summary Generator",
  description="Enter a YouTube video URL of a podcast to get a short summary."
  
)

# Launch the Gradio interface
interface.launch()