import gradio as gr from youtube_transcript_api import YouTubeTranscriptApi as yta import re # Define the extract_video_id function def extract_video_id(youtube_url): if "youtube.com/watch" not in youtube_url: print("Invalid YouTube URL") return None index_v = youtube_url.find('v=') if index_v == -1: print("Video ID not found in the URL") return None video_id_start = index_v + 2 video_id_end = youtube_url.find('&', video_id_start) video_id = youtube_url[video_id_start:video_id_end] if video_id_end != -1 else youtube_url[video_id_start:] return video_id # Set the input and output components input_component = gr.Textbox(label="YouTube URL") output_component = gr.Textbox(label="Transcription") # Set the function def transcribe_video(youtube_url): video_id = extract_video_id(youtube_url) if video_id is None: return "Invalid YouTube URL" transcript = yta.get_transcript(video_id, languages=('us', 'en')) data1 = [t['text'] for t in transcript] data2 = [re.sub(r"[^a-zA-Z0-9-1sg��çiISGÖÜçï ]", "", line) for line in data1] return "\n".join(data2) # Create the app app = gr.Interface( fn=transcribe_video, inputs=input_component, outputs=output_component, theme={ "base_color": "#333333", "font": "Courier New", "font_size": 18, "font_weight": "bold", "font_style": "italic", "button_color": "#FFA500", "button_hover_color": "#FFC0CB", "button_active_color": "#FFD700", "button_border_color": "#333333", "button_border_width": 2, "button_border_radius": 5, "input_border_color": "#333333", "input_border_width": 2, "input_border_radius": 5, "input_background_color": "#FFFFFF", "input_text_color": "#333333", "output_border_color": "#333333", "output_border_width": 2, "output_border_radius": 5, "output_background_color": "#FFFFFF", "output_text_color": "#333333", "label_color": "#333333", "label_font_size": 16, "label_font_weight": "bold", "label_font_style": "italic", "label_background_color": "#FFFFFF", "label_border_color": "#333333", "label_border_width": 2, "label_border_radius": 5, "header_color": "#333333", "header_font_size": 24, "header_font_weight": "bold", "header_font_style": "italic", "header_background_color": "#FFFFFF", "header_border_color": "#333333", "header_border_width": 2, "header_border_radius": 5, "footer_color": "#333333", "footer_font_size": 14, "footer_font_weight": "bold", "footer_font_style": "italic", "footer_background_color": "#FFFFFF", "footer_border_color": "#333333", "footer_border_width": 2, "footer_border_radius": 5, }, ) # Launch the app app.launch()