File size: 3,770 Bytes
074b124 f73da86 074b124 433ceb3 074b124 433ceb3 074b124 433ceb3 074b124 433ceb3 074b124 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | import gradio as gr
from google import genai
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api._errors import NoTranscriptFound, VideoUnavailable, TranscriptsDisabled
def get_transcript(video_url, lang_sel, model_sel, api_key_input):
client = genai.Client(api_key=api_key_input)
try:
video_id = video_url.split("v=")[1].split("&")[0]
print(f"Extracted Video ID: {video_id}") # Debugging
transcript = YouTubeTranscriptApi.get_transcript(video_id)
raw_text = " ".join([entry["text"] for entry in transcript])
print(f"Retrieved Transcript: {raw_text[:500]}") # Print first 500 characters
if lang_sel != "English":
prompt = f"Translate this text to {lang_sel} language: {raw_text}"
response = client.models.generate_content(
model=model_sel,
contents=[prompt]
)
return response.text
return raw_text
except (NoTranscriptFound, VideoUnavailable, TranscriptsDisabled) as e:
print(f"Transcript Retrieval Error: {e}") # Debugging
return f"β οΈ Error: {e}"
def process_transcript(video_url, word_count, model_sel, lang_sel, api_key_input):
client = genai.Client(api_key=api_key_input)
transcript = get_transcript(video_url, lang_sel, model_sel, api_key_input)
prompt = f"Summarize this text in {lang_sel} language using {word_count} words: {transcript}"
response = client.models.generate_content(
model=model_sel,
contents=[prompt]
)
return response.text
with gr.Blocks(theme=gr.themes.Default()) as demo:
gr.Markdown("""
# π¬ YouTube Video Transcription & Summarization @SenasuDemir
**Paste a YouTube link, select options, and generate transcriptions or summaries easily!**
""")
with gr.Row():
api_key = gr.Textbox(
placeholder="Enter your Gemini API Key",
label="π API Key",
type="password",
interactive=True
)
gr.Markdown("### 1οΈβ£ Enter Video URL")
video_url = gr.Textbox(
placeholder="Paste YouTube video link here...",
label="πΊ YouTube Video URL",
interactive=True
)
gr.Markdown("### 2οΈβ£ Select Language & Model")
with gr.Row():
language_selection = gr.Radio(
choices=["Turkish", "English", "Italian", "German"],
value="English",
label="π Select Translation Language"
)
model_selection = gr.Dropdown(
choices=["gemini-2.0-flash", "gemini-2.0-flash-lite", "gemini-1.5-flash", "gemini-1.5-pro"],
value="gemini-2.0-flash",
label="π€ Choose AI Model"
)
word_count = gr.Slider(
50, 500, step=10, value=80,
label="π’ Summary Word Count",
info="Choose a length between 50 and 500 words"
)
gr.Markdown("### 3οΈβ£ Get Transcript & Summarization")
with gr.Row():
trs_btn = gr.Button("π Get Transcript", variant="primary")
sum_btn = gr.Button("βοΈ Summarize", variant="secondary")
with gr.Row():
transkript_text = gr.Textbox(label="π€ Video Transcription", lines=5, interactive=False)
sum_text = gr.Textbox(label="π Summarized Text", lines=3, interactive=False)
trs_btn.click(
fn=get_transcript,
inputs=[video_url, language_selection, model_selection, api_key],
outputs=transkript_text
)
sum_btn.click(
fn=process_transcript,
inputs=[video_url, word_count, model_selection, language_selection, api_key],
outputs=sum_text
)
if __name__ == "__main__":
demo.launch() |