youngtsai commited on
Commit
0d03f45
·
1 Parent(s): 9b38933

verify_password(password_secret, correct_password)

Browse files
Files changed (1) hide show
  1. app.py +33 -28
app.py CHANGED
@@ -6,6 +6,16 @@ import yt_dlp as ydlp
6
  from openai import OpenAI
7
  import re
8
 
 
 
 
 
 
 
 
 
 
 
9
  def ms_to_srt_time(ms):
10
  sec, ms = divmod(ms, 1000)
11
  min, sec = divmod(sec, 60)
@@ -30,20 +40,15 @@ def get_video_duration(url):
30
  info_dict = ydl.extract_info(url, download=False)
31
  return info_dict.get('duration', 0)
32
 
33
- def process_video(yt_id_or_url, openAI_key=None, password_secret=None):
34
  # Extract the yt_id from the URL if a full URL is given
35
  yt_id_match = re.search(r"(?<=v=)[a-zA-Z0-9_-]+", yt_id_or_url)
36
  yt_id = yt_id_match.group(0) if yt_id_match else yt_id_or_url
37
 
38
  # Check if openAI_key is provided or validate using secret
39
  if not openAI_key:
40
- default_key = os.environ.get("OPEN_AI_KEY") # 從環境變量中獲取默認的OpenAI key
41
- default_secret = os.environ.get("PASSWORD_SECRET")
42
-
43
- if password_secret == default_secret:
44
- openAI_key = default_key
45
- else:
46
- return "Invalid secret or no OpenAI key provided."
47
 
48
  # yt_id = "90BAlvlLvE0"
49
  url = f"https://www.youtube.com/watch?v={yt_id}"
@@ -253,26 +258,26 @@ def process_video(yt_id_or_url, openAI_key=None, password_secret=None):
253
 
254
 
255
 
256
- def main():
257
- interface = gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
  fn=process_video,
259
- inputs=[
260
- gr.Textbox(label="YouTube Video ID"),
261
- gr.Textbox(label="OpenAI Key (optional)"),
262
- gr.Textbox(label="Password Secret (optional)")
263
- ],
264
- outputs=[
265
- gr.File(label="Download SRT"),
266
- gr.Textbox(label="SRT Content", show_copy_button=True),
267
- gr.Textbox(label="Large Scope SRT", show_copy_button=True),
268
- gr.Textbox(label="Video Summary", show_copy_button=True),
269
- gr.Textbox(label="Mind Map", show_copy_button=True)
270
- ],
271
- live=False,
272
- description="Generate SRT, Summary and Mind Map from YouTube video(限額 60 min)",
273
- title="YouTube to SRT, Summary & Mind Map"
274
  )
275
- interface.launch()
276
 
277
- if __name__ == "__main__":
278
- main()
 
6
  from openai import OpenAI
7
  import re
8
 
9
+
10
+ OPEN_AI_KEY = os.environ.get("OPEN_AI_KEY") # 從環境變量中獲取默認的OpenAI key
11
+ PASSWORD = os.environ.get("PASSWORD_SECRET")
12
+
13
+ def verify_password(input_password, correct_password):
14
+ if input_password == correct_password:
15
+ return True
16
+ else:
17
+ raise gr.Error("密碼錯誤")
18
+
19
  def ms_to_srt_time(ms):
20
  sec, ms = divmod(ms, 1000)
21
  min, sec = divmod(sec, 60)
 
40
  info_dict = ydl.extract_info(url, download=False)
41
  return info_dict.get('duration', 0)
42
 
43
+ def process_video(yt_id_or_url, openAI_key=None, password_secret=None):
44
  # Extract the yt_id from the URL if a full URL is given
45
  yt_id_match = re.search(r"(?<=v=)[a-zA-Z0-9_-]+", yt_id_or_url)
46
  yt_id = yt_id_match.group(0) if yt_id_match else yt_id_or_url
47
 
48
  # Check if openAI_key is provided or validate using secret
49
  if not openAI_key:
50
+ correct_password = PASSWORD
51
+ verify_password(password_secret, correct_password)
 
 
 
 
 
52
 
53
  # yt_id = "90BAlvlLvE0"
54
  url = f"https://www.youtube.com/watch?v={yt_id}"
 
258
 
259
 
260
 
261
+ with gr.Blocks() as demo:
262
+ with gr.Row():
263
+ video_id = gr.Textbox(label="YouTube Video ID")
264
+ openai_key = gr.Textbox(label="OpenAI Key (optional)")
265
+ password_secret = gr.Textbox(label="Password Secret (optional)")
266
+ with gr.Row():
267
+ download_srt = gr.File(label="Download SRT")
268
+ srt_content = gr.Textbox(label="SRT Content", show_copy_button=True)
269
+ large_scope_srt = gr.Textbox(label="Large Scope SRT", show_copy_button=True)
270
+ video_summary = gr.Textbox(label="Video Summary", show_copy_button=True)
271
+ mind_map = gr.Textbox(label="Mind Map", show_copy_button=True)
272
+
273
+ gr.Markdown("Generate SRT, Summary and Mind Map from YouTube video(限額 60 min)")
274
+ gr.Markdown("YouTube to SRT, Summary & Mind Map")
275
+ submit_btn = gr.Button("Process Video")
276
+ submit_btn.click(
277
  fn=process_video,
278
+ inputs=[video_id, openai_key, password_secret],
279
+ outputs=[download_srt, srt_content, large_scope_srt, video_summary, mind_map]
 
 
 
 
 
 
 
 
 
 
 
 
 
280
  )
 
281
 
282
+
283
+ demo.launch()