mgokg commited on
Commit
c53abd7
·
verified ·
1 Parent(s): 32a2541

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -20
app.py CHANGED
@@ -1,31 +1,48 @@
1
  import gradio as gr
2
- from youtube_transcript_api import YouTubeTranscriptApi
3
 
 
 
 
4
 
5
- def transkribiere_youtube_video(video_url):
6
- #3.Get transcripts
7
- transcript = YouTubeTranscriptApi.get_transcript("7ySqrQVLIF8")
8
- return transcript
9
 
10
-
11
-
 
12
  try:
13
- video_id = video_url.split("v=")[1]
14
- if "&" in video_id:
15
- video_id = video_id.split("&")[0] # falls zusätzliche Parameter in URL
16
- transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['de', 'en']) # Transkripte in DE & EN (falls vorhanden)
17
-
18
- text = " ".join([entry['text'] for entry in transcript])
19
- return text
 
 
 
 
 
 
20
  except Exception as e:
21
- return f"Fehler: {str(e)}. Stelle sicher, dass ein Transkript für das Video verfügbar ist und der Link korrekt ist."
 
 
 
 
 
 
 
22
 
 
23
  iface = gr.Interface(
24
- fn=transkribiere_youtube_video,
25
- inputs=gr.Textbox(lines=1, placeholder="Gib hier den YouTube Video-Link ein"),
26
- outputs=gr.Textbox(lines=20, label="Transkript"),
27
- title="YouTube Transkription",
28
- description="Gib einen YouTube-Videolink ein und erhalte den transkribierten Text."
29
  )
30
 
 
31
  iface.launch()
 
1
  import gradio as gr
2
+ from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
3
 
4
+ def get_youtube_transcript(video_id):
5
+ """
6
+ Retrieves the transcript of a YouTube video.
7
 
8
+ Args:
9
+ video_id: The ID of the YouTube video (e.g., 'dQw4w9WgXcQ').
 
 
10
 
11
+ Returns:
12
+ A string containing the full transcript, or None if no transcript is found or an error occurs.
13
+ """
14
  try:
15
+ transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
16
+ transcript = transcript_list.find_generated_transcript(['en']) # Try English first
17
+ if not transcript:
18
+ transcript = transcript_list.find_manually_created_transcript(['en']) # Fallback to manual if no generated
19
+ if not transcript:
20
+ return None # No English transcript found
21
+ transcript_data = transcript.fetch()
22
+ full_transcript = " ".join([item['text'] for item in transcript_data])
23
+ return full_transcript
24
+ except TranscriptsDisabled:
25
+ return f"Transcripts are disabled for video ID: {video_id}"
26
+ except NoTranscriptFound:
27
+ return f"No transcript found for video ID: {video_id}"
28
  except Exception as e:
29
+ return f"An error occurred: {e}"
30
+
31
+ def fetch_transcript(video_id):
32
+ transcript = get_youtube_transcript(video_id)
33
+ if transcript:
34
+ return transcript
35
+ else:
36
+ return "Could not retrieve the transcript."
37
 
38
+ # Create the Gradio interface
39
  iface = gr.Interface(
40
+ fn=fetch_transcript,
41
+ inputs=gr.inputs.Textbox(lines=1, placeholder="Enter YouTube Video ID..."),
42
+ outputs=gr.outputs.Textbox(),
43
+ title="YouTube Transcript Fetcher",
44
+ description="Enter a YouTube video ID to fetch its transcript."
45
  )
46
 
47
+ # Launch the Gradio app
48
  iface.launch()