LuckyHappyFish commited on
Commit
19cc7dc
·
1 Parent(s): 2d57b09
Files changed (1) hide show
  1. app.py +39 -10
app.py CHANGED
@@ -11,41 +11,70 @@ def get_youtube_details(link):
11
 
12
  # Use pytube to get the video ID
13
  video_id = yt.video_id
14
- print(f"Video ID: {video_id}") # Debugging output for testing purposes
15
 
16
  # Attempt to fetch the transcript
17
  transcript_text = "No transcript available."
 
 
 
18
  try:
19
  # List available transcripts
20
  transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
21
- debug_info = "Available transcripts:\n"
22
  for transcript in transcript_list:
23
- debug_info += f"Language Code: {transcript.language_code}, Language: {transcript.language}, Is Generated: {transcript.is_generated}\n"
24
- print(debug_info) # Print debug info to the console for testing
25
 
26
  # Try fetching the manually created transcript or fall back to auto-generated
27
  try:
28
  transcript = transcript_list.find_transcript(['en', 'en-US', 'en-GB', 'en-AU'])
29
- print(f"Selected transcript language: {transcript.language_code} (manually created)") # Debugging
30
  except NoTranscriptFound:
31
  # If no manually created transcript is found, try fetching the auto-generated one
32
  transcript = transcript_list.find_generated_transcript(['en', 'en-US', 'en-GB', 'en-AU'])
33
- print(f"Selected transcript language: {transcript.language_code} (auto-generated)") # Debugging
34
 
35
  # Fetch the transcript and join the text pieces
36
  transcript_data = transcript.fetch()
37
  transcript_text = "\n".join([item['text'] for item in transcript_data])
 
38
  except TranscriptsDisabled:
39
  transcript_text = "Transcripts are disabled for this video."
 
40
  except NoTranscriptFound:
41
  transcript_text = "No transcript found for this video."
 
42
  except Exception as e:
43
  transcript_text = f"An error occurred while fetching the transcript: {str(e)}"
 
44
 
45
- return transcript_text, thumbnail_url
 
 
 
46
  except Exception as e:
47
- return f"Error processing the video: {str(e)}", None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- # Gradio interface using the refactored function
50
- demo = gr.Interface(fn=get_youtube_details, inputs="text", outputs=["text", "image"])
51
  demo.launch()
 
11
 
12
  # Use pytube to get the video ID
13
  video_id = yt.video_id
14
+ debug_info = f"Video ID: {video_id}\n\n"
15
 
16
  # Attempt to fetch the transcript
17
  transcript_text = "No transcript available."
18
+ available_transcripts_info = "Available transcripts:\n"
19
+ selected_transcript_info = "Selected transcript:\n"
20
+
21
  try:
22
  # List available transcripts
23
  transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
 
24
  for transcript in transcript_list:
25
+ available_transcripts_info += f"Language Code: {transcript.language_code}, Language: {transcript.language}, Is Generated: {transcript.is_generated}\n"
 
26
 
27
  # Try fetching the manually created transcript or fall back to auto-generated
28
  try:
29
  transcript = transcript_list.find_transcript(['en', 'en-US', 'en-GB', 'en-AU'])
30
+ selected_transcript_info += f"Language Code: {transcript.language_code} (manually created)\n"
31
  except NoTranscriptFound:
32
  # If no manually created transcript is found, try fetching the auto-generated one
33
  transcript = transcript_list.find_generated_transcript(['en', 'en-US', 'en-GB', 'en-AU'])
34
+ selected_transcript_info += f"Language Code: {transcript.language_code} (auto-generated)\n"
35
 
36
  # Fetch the transcript and join the text pieces
37
  transcript_data = transcript.fetch()
38
  transcript_text = "\n".join([item['text'] for item in transcript_data])
39
+
40
  except TranscriptsDisabled:
41
  transcript_text = "Transcripts are disabled for this video."
42
+ available_transcripts_info += "Transcripts are disabled for this video."
43
  except NoTranscriptFound:
44
  transcript_text = "No transcript found for this video."
45
+ available_transcripts_info += "No transcripts found for this video."
46
  except Exception as e:
47
  transcript_text = f"An error occurred while fetching the transcript: {str(e)}"
48
+ available_transcripts_info += f"An error occurred while fetching the transcripts: {str(e)}"
49
 
50
+ # Combine all debug information
51
+ debug_info += available_transcripts_info + "\n" + selected_transcript_info
52
+
53
+ return transcript_text, thumbnail_url, debug_info
54
  except Exception as e:
55
+ return f"Error processing the video: {str(e)}", None, f"Error processing the video: {str(e)}"
56
+
57
+ # Define Gradio interface with multiple outputs
58
+ with gr.Blocks() as demo:
59
+ gr.Markdown("## YouTube Transcript and Thumbnail Fetcher")
60
+
61
+ with gr.Row():
62
+ youtube_link = gr.Textbox(label="YouTube Video URL", placeholder="Enter YouTube video URL here")
63
+ submit_btn = gr.Button("Fetch Details")
64
+
65
+ with gr.Row():
66
+ transcript_output = gr.Textbox(label="Transcript", lines=20)
67
+
68
+ with gr.Row():
69
+ thumbnail_output = gr.Image(label="Thumbnail")
70
+
71
+ with gr.Row():
72
+ debug_output = gr.Textbox(label="Debug Information", lines=10)
73
+
74
+ submit_btn.click(
75
+ fn=get_youtube_details,
76
+ inputs=youtube_link,
77
+ outputs=[transcript_output, thumbnail_output, debug_output]
78
+ )
79
 
 
 
80
  demo.launch()