tregu0458 commited on
Commit
c1a54fd
·
verified ·
1 Parent(s): 4235624

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -25
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  from youtube_transcript_api import (
3
  YouTubeTranscriptApi,
4
  NoTranscriptFound,
@@ -60,39 +59,48 @@ class YoutubeTranscript:
60
  print(f"予期せぬエラーが発生しました: {str(e)}")
61
  return None
62
 
63
-
64
- # The function 'get_transcript_for_gradio' should be outside the class
65
  def get_transcript_for_gradio(url: str, language: str) -> str:
66
- """Gradio UI 用の書き起こし取得関数"""
67
  try:
68
  transcript = YoutubeTranscript.get_transcript(url, language)
69
  if transcript:
70
- # formatted_transcript = "\n".join(
71
- # [f"[{entry['start']:.1f}s] {entry['text']}" for entry in transcript]
72
- # )
73
  formatted_transcript = "".join(
74
  [f"{entry['text']}" for entry in transcript]
75
  )
76
- return formatted_transcript,str(len(formatted_transcript))
77
  else:
78
- return "字幕の取得に失敗しました。","字幕の取得に失敗しました。"
79
  except NoTranscriptFound as e:
80
- return str(e),str(len(e)) # Return the error message from the exception
81
  except Exception as e:
82
- return f"予期せぬエラーが発生しました: {str(e)}",str(len(e))
83
 
84
- iface = gr.Interface(
85
- fn=get_transcript_for_gradio,
86
- inputs=[
87
- gr.Textbox(lines=1, placeholder="YouTube動画のURLを入力してください",show_copy_button=True),
88
- gr.Radio(["ja", "en", "en-US"], label="言語", value="ja") # ラジオボタンを追加
89
- ],
90
- outputs=[
91
- gr.Code(label="字幕", max_lines=10),
92
- gr.Textbox(label="文字数") # Add a Textbox for character count
93
- ],
94
- title="YouTube字幕取得アプリ",
95
- description="YouTube動画のURLを入力すると、字幕を取得して表示します。",
96
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
- iface.launch()
 
 
1
  from youtube_transcript_api import (
2
  YouTubeTranscriptApi,
3
  NoTranscriptFound,
 
59
  print(f"予期せぬエラーが発生しました: {str(e)}")
60
  return None
61
 
 
 
62
  def get_transcript_for_gradio(url: str, language: str) -> str:
 
63
  try:
64
  transcript = YoutubeTranscript.get_transcript(url, language)
65
  if transcript:
 
 
 
66
  formatted_transcript = "".join(
67
  [f"{entry['text']}" for entry in transcript]
68
  )
69
+ return formatted_transcript, str(len(formatted_transcript))
70
  else:
71
+ return "字幕の取得に失敗しました。", "字幕の取得に失敗しました。"
72
  except NoTranscriptFound as e:
73
+ return str(e), str(len(str(e)))
74
  except Exception as e:
75
+ return f"予期せぬエラーが発生しました: {str(e)}", str(len(str(e)))
76
 
77
+ with gr.Blocks(title="YouTube字幕取得アプリ") as demo:
78
+ gr.Markdown("# YouTube字幕取得アプリ")
79
+ gr.Markdown("YouTube動画のURLを入力すると、字幕を取得して表示します。")
80
+
81
+ with gr.Row():
82
+ url_input = gr.Textbox(
83
+ lines=1,
84
+ placeholder="YouTube動画のURLを入力してください",
85
+ show_copy_button=True,
86
+ label="YouTube URL"
87
+ )
88
+ language_input = gr.Radio(
89
+ ["ja", "en", "en-US"],
90
+ label="言語",
91
+ value="ja"
92
+ )
93
+
94
+ submit_button = gr.Button("字幕を取得")
95
+
96
+ with gr.Column():
97
+ transcript_output = gr.Code(label="字幕", max_lines=10)
98
+ char_count_output = gr.Textbox(label="文字数")
99
+
100
+ submit_button.click(
101
+ fn=get_transcript_for_gradio,
102
+ inputs=[url_input, language_input],
103
+ outputs=[transcript_output, char_count_output]
104
+ )
105
 
106
+ demo.launch()